Skip to content

Commit 373327c

Browse files
author
Dou Du
committed
add test workflow for the widget
1 parent 4687a51 commit 373327c

File tree

5 files changed

+153
-8
lines changed

5 files changed

+153
-8
lines changed

.github/workflows/voila-test.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Voila test
2+
3+
on:
4+
push:
5+
branches:
6+
- '*'
7+
pull_request:
8+
branches:
9+
- '*'
10+
11+
jobs:
12+
voila:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v2
17+
- name: Install node
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: '18.x'
21+
- name: Install Python
22+
uses: actions/setup-python@v2
23+
with:
24+
python-version: '3.9'
25+
architecture: 'x64'
26+
- name: Install dependencies
27+
run:
28+
pip install --upgrade pip
29+
pip install --upgrade jupyterlab
30+
pip install --upgrade voila
31+
pip install --upgrade voila-osscar-template
32+
pip install -e .
33+
34+
- name: Run the voila server
35+
run: |
36+
voila --template=osscar --enable_nbextensions=True example/ --port 8383 --no-browser &
37+
pip install --upgrade pytest
38+
pip install --upgrade selenium
39+
pip install --upgrade Pillow
40+
41+
- uses: nanasess/setup-chromedriver@master
42+
- run: |
43+
export DISPLAY=:99
44+
chromedriver --url-base=/wd/hub &
45+
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
46+
python3 $GITHUB_WORKSPACE/test/test.py
47+
48+
- uses: actions/upload-artifact@v2
49+
with:
50+
name: quantum-screenshot
51+
path: './*.png'
52+
53+
- name: Run the figures test
54+
run: |
55+
python3 $GITHUB_WORKSPACE/test/test_figures.py
56+

example/example.ipynb

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 1,
66
"metadata": {},
7-
"outputs": [],
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"env: ANYWIDGET_HMR=1\n"
13+
]
14+
}
15+
],
816
"source": [
917
"# This cell (autoloading & hot module replacement) is only needed for development!\n",
1018
"%load_ext autoreload\n",
@@ -14,9 +22,24 @@
1422
},
1523
{
1624
"cell_type": "code",
17-
"execution_count": null,
25+
"execution_count": 2,
1826
"metadata": {},
19-
"outputs": [],
27+
"outputs": [
28+
{
29+
"data": {
30+
"application/vnd.jupyter.widget-view+json": {
31+
"model_id": "941f07035d454d4f93aa142af33b0f1f",
32+
"version_major": 2,
33+
"version_minor": 1
34+
},
35+
"text/plain": [
36+
"BZVisualizer(disable_interact_overlay=True, seekpath_data={'faces_data': {'triangles_vertices': [[-0.628318530…"
37+
]
38+
},
39+
"metadata": {},
40+
"output_type": "display_data"
41+
}
42+
],
2043
"source": [
2144
"from widget_bzvisualizer import BZVisualizer\n",
2245
"\n",
@@ -43,7 +66,7 @@
4366
},
4467
{
4568
"cell_type": "code",
46-
"execution_count": null,
69+
"execution_count": 3,
4770
"metadata": {},
4871
"outputs": [],
4972
"source": [
@@ -57,9 +80,24 @@
5780
},
5881
{
5982
"cell_type": "code",
60-
"execution_count": null,
83+
"execution_count": 4,
6184
"metadata": {},
62-
"outputs": [],
85+
"outputs": [
86+
{
87+
"data": {
88+
"application/vnd.jupyter.widget-view+json": {
89+
"model_id": "a33b04116cee41cfabfec8fb33cc8af5",
90+
"version_major": 2,
91+
"version_minor": 1
92+
},
93+
"text/plain": [
94+
"BZVisualizer(seekpath_data={'faces_data': {'triangles_vertices': [[-1.1089190599406602, -2.220446049250313e-16…"
95+
]
96+
},
97+
"metadata": {},
98+
"output_type": "display_data"
99+
}
100+
],
63101
"source": [
64102
"# Load info with ASE\n",
65103
"# NOTE: (ASE is not included as a direct dependency, needs to be installed separately)\n",
@@ -99,7 +137,7 @@
99137
"name": "python",
100138
"nbconvert_exporter": "python",
101139
"pygments_lexer": "ipython3",
102-
"version": "3.11.9"
140+
"version": "3.11.7"
103141
}
104142
},
105143
"nbformat": 4,

test/test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Generated by Selenium IDE
2+
import pytest
3+
import time
4+
import json
5+
import os
6+
from selenium import webdriver
7+
from selenium.webdriver.chrome.options import Options
8+
from selenium.webdriver.common.by import By
9+
from selenium.webdriver.common.action_chains import ActionChains
10+
from selenium.webdriver.support import expected_conditions
11+
from selenium.webdriver.support.wait import WebDriverWait
12+
from selenium.webdriver.common.keys import Keys
13+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
14+
15+
class test_widget():
16+
def setup_method(self, method):
17+
options = Options()
18+
cwd = os.getcwd()
19+
options.add_experimental_option("prefs", {"download.default_directory": cwd})
20+
self.driver = webdriver.Chrome(options=options)
21+
self.vars = {}
22+
23+
def teardown_method(self, method):
24+
self.driver.quit()
25+
26+
def download_widget_image(self):
27+
self.driver.get("http://localhost:8383/voila/render/example.ipynb")
28+
self.driver.set_window_size(1280, 720)
29+
time.sleep(3)
30+
self.driver.save_screenshot("widget-01.png")
31+
self.driver.execute_script("window.scrollTo(0, 1000)")
32+
time.sleep(3)
33+
self.driver.save_screenshot("widget-02.png")
34+
35+
test = test_widget()
36+
test.setup_method('Chrome')
37+
test.download_widget_image()
38+
test.teardown_method('Chrome')
39+

test/test_figures.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from PIL import Image, ImageChops, ImageStat
2+
3+
image1 = Image.open('Figure 1.png')
4+
image2 = Image.open('test/widget-sample.png')
5+
6+
diff = ImageChops.difference(image1, image2)
7+
stat = ImageStat.Stat(diff)
8+
9+
if sum(stat.mean) == 0:
10+
print('images are the same')
11+
else:
12+
raise Exception("The result is NOT the same as expected. Please check matplotlib version.")

test/widget-sample.png

72.9 KB
Loading

0 commit comments

Comments
 (0)