Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add screenshot teardown fixture - approach 1 #421

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def _selenium_driver(nb_path):
return _selenium_driver


@pytest.fixture
def final_screenshot(request, screenshot_dir, selenium):
"""Take screenshot at the end of the test.
Screenshot name is generated from the test function name
by stripping the 'test_' prefix
"""
screenshot_name = f"{request.function.__name__[5:]}.png"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, using request.function, didn't know that. This is brilliant!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
screenshot_name = f"{request.function.__name__[5:]}.png"
screenshot_name = f"{request.function.__name__.lstrip('test_')}.png"

Maybe this is more pointy to the purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, then just ignore my request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that in Python 3.9 one could use the removeprefix string method for exactly this, but we'd need to drop support for Python 3.8, at least in tests. Probably not worth it now.

screenshot_path = Path.joinpath(screenshot_dir, screenshot_name)
yield
selenium.get_screenshot_as_file(screenshot_path)


@pytest.fixture
def firefox_options(firefox_options):
firefox_options.add_argument("--headless")
Expand Down
32 changes: 9 additions & 23 deletions tests/test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,41 @@ def test_notebook_service_available(notebook_service):
assert response.status_code == 200


def test_process_list(selenium_driver, screenshot_dir):
def test_process_list(selenium_driver, final_screenshot):
driver = selenium_driver("notebooks/process_list.ipynb")
driver.find_element(By.XPATH, '//button[text()="Update now"]')
driver.get_screenshot_as_file(f"{screenshot_dir}/process-list.png")


def test_aiida_datatypes_viewers(selenium_driver, screenshot_dir):
def test_aiida_datatypes_viewers(selenium_driver, final_screenshot):
driver = selenium_driver("notebooks/aiida_datatypes_viewers.ipynb")
driver.set_window_size(1000, 2000)
driver.find_element(By.CLASS_NAME, "widget-label")
driver.find_element(By.XPATH, '//button[text()="Clear selection"]')
time.sleep(5)
driver.get_screenshot_as_file(f"{screenshot_dir}/datatypes-viewer.png")


def test_eln_configure(selenium_driver, screenshot_dir):
def test_eln_configure(selenium_driver, final_screenshot):
driver = selenium_driver("notebooks/eln_configure.ipynb")
driver.find_element(By.XPATH, '//button[text()="Set as default"]')
driver.get_screenshot_as_file(f"{screenshot_dir}/eln-configure.png")


def test_process(selenium_driver, screenshot_dir):
def test_process(selenium_driver, final_screenshot):
driver = selenium_driver("notebooks/process.ipynb")
driver.find_element(By.XPATH, '//label[@title="Select calculation:"]')
driver.get_screenshot_as_file(f"{screenshot_dir}/process.png")


def test_wizard_apps(selenium_driver, screenshot_dir):
def test_wizard_apps(selenium_driver, final_screenshot):
driver = selenium_driver("notebooks/wizard_apps.ipynb")
driver.find_element(By.XPATH, '//label[@title="Delivery progress:"]')
driver.get_screenshot_as_file(f"{screenshot_dir}/wizzard-apps.png")


def test_structures(selenium_driver, screenshot_dir):
def test_structures(selenium_driver, final_screenshot):
driver = selenium_driver("notebooks/structures.ipynb")
driver.set_window_size(1000, 900)
driver.find_element(By.XPATH, '//button[text()="Upload Structure (0)"]')
time.sleep(5)
driver.get_screenshot_as_file(f"{screenshot_dir}/structures.png")


def test_structures_generate_from_smiles(selenium_driver, screenshot_dir):
def test_structures_generate_from_smiles(selenium_driver, final_screenshot):
driver = selenium_driver("notebooks/structures.ipynb")
driver.set_window_size(1000, 900)
# Switch to SMILES tab in StructureManagerWidget
Expand All @@ -69,22 +62,18 @@ def test_structures_generate_from_smiles(selenium_driver, screenshot_dir):
).send_keys("1")
driver.find_element(By.XPATH, '//button[text()="Apply selection"]').click()
driver.find_element(By.XPATH, "//div[starts-with(text(),'Id: 1; Symbol: C;')]")
driver.get_screenshot_as_file(
f"{screenshot_dir}/structures_generate_from_smiles_2.png"
)


def test_eln_import(selenium_driver, screenshot_dir):
def test_eln_import(selenium_driver, final_screenshot):
driver = selenium_driver("notebooks/eln_import.ipynb")
# TODO: This find_element is not specific enough it seems,
# on the screenshot the page is still loading.
driver.find_element(By.ID, "tooltip")
time.sleep(5)
driver.get_screenshot_as_file(f"{screenshot_dir}/eln-import.png")


def test_computational_resources_code_setup(
selenium_driver, aiidalab_exec, screenshot_dir
selenium_driver, aiidalab_exec, final_screenshot
):
"""Test the quicksetup of the code"""
# check the code pw-7.0 is not in code list
Expand Down Expand Up @@ -147,6 +136,3 @@ def test_computational_resources_code_setup(
# check the new code pw-7.0@daint-mc is in code list
output = aiidalab_exec("verdi code list").decode().strip()
assert "dos-7.0@daint-mc" in output

# take screenshots
driver.get_screenshot_as_file(f"{screenshot_dir}/computational-resources.png")