From 869f0c0774749ff84cccabf72667a4fa2ae6541d Mon Sep 17 00:00:00 2001 From: Pavlo Shtohryn Date: Thu, 26 Dec 2024 19:11:19 +0200 Subject: [PATCH 1/2] fix/select_top_level_subject --- pages/preprints.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pages/preprints.py b/pages/preprints.py index d598173b..f113f063 100644 --- a/pages/preprints.py +++ b/pages/preprints.py @@ -1,3 +1,4 @@ +import time from urllib.parse import urljoin import pytest @@ -113,14 +114,12 @@ def select_from_dropdown_listbox(self, selection): ) def select_top_level_subject(self, selection): - for subject in self.top_level_subjects: - if subject.text == selection: - # Find the checkbox element and click it to select the subject - checkbox = subject.find_element_by_css_selector( - 'input.ember-checkbox.ember-view' - ) - checkbox.click() - break + subject = None + while subject is None: + time.sleep(1) + subject = next((subject for subject in self.top_level_subjects if subject.text == selection), None) + checkbox = subject.find_element(By.CSS_SELECTOR, 'input.ember-checkbox.ember-view') + checkbox.click() first_selected_subject = Locator(By.CSS_SELECTOR, 'li[data-test-selected-subject]') basics_tags_section = Locator(By.CSS_SELECTOR, '[data-test-no-tags]') From 5fda61548ed24b7e8123dae53821fb2f35262263 Mon Sep 17 00:00:00 2001 From: Shtohryn Date: Wed, 15 Jan 2025 18:18:54 +0200 Subject: [PATCH 2/2] Enhance select_top_level_subject implementation --- base/expected_conditions.py | 19 +++++++++++++++++++ pages/preprints.py | 16 ++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/base/expected_conditions.py b/base/expected_conditions.py index 337df656..ce76d34e 100644 --- a/base/expected_conditions.py +++ b/base/expected_conditions.py @@ -1,3 +1,5 @@ +from selenium.common.exceptions import StaleElementReferenceException +from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.support import expected_conditions as EC @@ -25,3 +27,20 @@ def __init__(self, page_index): def __call__(self, driver): return len(driver.window_handles) > self.page_index + + +class text_to_be_present_in_elements(object): + """ An expectation for checking if the given text is present in the + specified element. + locator, text + """ + def __init__(self, locator, text_): + self.locator = locator + self.text = text_ + + def __call__(self, driver: WebDriver): + try: + element_texts = [element.text for element in driver.find_elements(*self.locator)] + return any([self.text in element_text for element_text in element_texts]) + except StaleElementReferenceException: + return False \ No newline at end of file diff --git a/pages/preprints.py b/pages/preprints.py index f113f063..105b514e 100644 --- a/pages/preprints.py +++ b/pages/preprints.py @@ -1,10 +1,13 @@ import time from urllib.parse import urljoin +import ipdb import pytest from selenium.webdriver.common.by import By +from selenium.webdriver.support.wait import WebDriverWait import settings +from base.expected_conditions import text_to_be_present_in_elements from base.locators import ( ComponentLocator, GroupLocator, @@ -114,12 +117,13 @@ def select_from_dropdown_listbox(self, selection): ) def select_top_level_subject(self, selection): - subject = None - while subject is None: - time.sleep(1) - subject = next((subject for subject in self.top_level_subjects if subject.text == selection), None) - checkbox = subject.find_element(By.CSS_SELECTOR, 'input.ember-checkbox.ember-view') - checkbox.click() + subject_selector = 'div[data-analytics-scope="Browse"] > ul > li' + wait = WebDriverWait(self.driver, 20) + wait.until(text_to_be_present_in_elements((By.CSS_SELECTOR, subject_selector), selection)) + for subject in self.top_level_subjects: + if subject.text == selection: + subject.click() + break first_selected_subject = Locator(By.CSS_SELECTOR, 'li[data-test-selected-subject]') basics_tags_section = Locator(By.CSS_SELECTOR, '[data-test-no-tags]')