From 4e7901e15dfa159e8ae47273053a49982e4f19ac Mon Sep 17 00:00:00 2001 From: Tobias Reiher <15232394+treiher@users.noreply.github.com> Date: Tue, 31 Oct 2023 22:36:27 +0100 Subject: [PATCH] Improve reliability of end-to-end tests --- tests/e2e/page.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/e2e/page.py b/tests/e2e/page.py index a9b0c20..cb66354 100644 --- a/tests/e2e/page.py +++ b/tests/e2e/page.py @@ -7,7 +7,7 @@ import pytest from selenium import webdriver -from selenium.common.exceptions import TimeoutException +from selenium.common.exceptions import StaleElementReferenceException, TimeoutException from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.alert import Alert from selenium.webdriver.common.by import By @@ -676,13 +676,20 @@ def _click_button(self, icon: str, index: int) -> None: sleep(0.01) def _set_input(self, icon: str, index: int, value: str) -> None: + def contains_icon(e: WebElement) -> bool: + try: + return e.find_elements( + by=By.XPATH, value=f".//i[@class='fas fa-{icon}']" + ) or e.find_elements(by=By.XPATH, value=f".//span[text()='{icon}']") + except StaleElementReferenceException: + return False + controls = [ e for e in self._driver.find_elements( by=By.XPATH, value="//div[contains(@class, 'control')]" ) - if e.find_elements(by=By.XPATH, value=f".//i[@class='fas fa-{icon}']") - or e.find_elements(by=By.XPATH, value=f".//span[text()='{icon}']") + if contains_icon(e) ] inp = controls[index].find_element(by=By.TAG_NAME, value="input") clear(inp)