Skip to content

Commit

Permalink
add test workflow for the widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Dou Du committed May 29, 2024
1 parent 369839b commit 9bfbcae
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/voila-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Voila test

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'

jobs:
voila:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install node
uses: actions/setup-node@v1
with:
node-version: '18.x'
- name: Install Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
architecture: 'x64'
- name: Install dependencies
run:
pip install --upgrade pip
pip install --upgrade jupyterlab
pip install --upgrade voila
pip install --upgrade voila-osscar-template
pip install -e .

- name: Run the voila server
run: |
voila --template=osscar --enable_nbextensions=True example/ --port 8383 --no-browser &
pip install --upgrade pytest
pip install --upgrade selenium
pip install --upgrade Pillow
- uses: nanasess/setup-chromedriver@master
- run: |
export DISPLAY=:99
chromedriver --url-base=/wd/hub &
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
python3 $GITHUB_WORKSPACE/test/test.py
- uses: actions/upload-artifact@v2
with:
name: quantum-screenshot
path: './*.png'

- name: Run the figures test
run: |
python3 $GITHUB_WORKSPACE/test/test_figures.py
39 changes: 39 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Selenium IDE
import pytest
import time
import json
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class test_widget():
def setup_method(self, method):
options = Options()
cwd = os.getcwd()
options.add_experimental_option("prefs", {"download.default_directory": cwd})
self.driver = webdriver.Chrome(options=options)
self.vars = {}

def teardown_method(self, method):
self.driver.quit()

def download_widget_image(self):
self.driver.get("http://localhost:8383/voila/render/example.ipynb")
self.driver.set_window_size(1280, 720)
time.sleep(3)
self.driver.save_screenshot("widget-01.png")
self.driver.execute_script("window.scrollTo(0, 1000)")
time.sleep(3)
self.driver.save_screenshot("widget-02.png")

test = test_widget()
test.setup_method('Chrome')
test.download_widget_image()
test.teardown_method('Chrome')

12 changes: 12 additions & 0 deletions test/test_figures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from PIL import Image, ImageChops, ImageStat

image1 = Image.open('widget-01.png')
image2 = Image.open('test/widget-sample.png')

diff = ImageChops.difference(image1, image2)
stat = ImageStat.Stat(diff)

if sum(stat.mean) == 0:
print('images are the same')
else:
raise Exception("The result is NOT the same as expected. Please check matplotlib version.")
Binary file added test/widget-sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9bfbcae

Please sign in to comment.