diff --git a/base/expected_conditions.py b/base/expected_conditions.py index 337df656..ee9ffb44 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,23 @@ 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: + return any( + self.text in element.text + for element in driver.find_elements(*self.locator) + ) + except StaleElementReferenceException: + return False diff --git a/pages/counter.java b/pages/counter.java deleted file mode 100644 index aabcfbf2..00000000 --- a/pages/counter.java +++ /dev/null @@ -1,84 +0,0 @@ -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -/** -* @author evivehealth on 08/02/19. -*/ -// Java program depicting -// concurrent programming in action. - -// Runnable Class that defines the logic -// of run method of runnable interface -public class Counter implements Runnable -{ - private final MainApp mainApp; - private final int loopLimit; - private final String task; - - // Constructor to get a reference to the main class - public Counter - (MainApp mainApp, int loopLimit, String task) - { - this.mainApp = mainApp; - this.loopLimit = loopLimit; - this.task = task; - } - - // Prints the thread name, task number and - // the value of counter - // Calls pause method to allow multithreading to occur - @Override - public void run() - { - for (int i = 0; i < loopLimit; i++) - { - System.out.println("Thread: " + - Thread.currentThread().getName() + " Counter: " - + (i + 1) + " Task: " + task); - mainApp.pause(Math.random()); - } - } -} -class MainApp -{ - - // Starts the threads. Pool size 2 means at any time - // there can only be two simultaneous threads - public void startThread() - { - ExecutorService taskList = - Executors.newFixedThreadPool(2); - for (int i = 0; i < 5; i++) - { - // Makes tasks available for execution. - // At the appropriate time, calls run - // method of runnable interface - taskList.execute(new Counter(this, i + 1, - "task " + (i + 1))); - } - - // Shuts the thread that's watching to see if - // you have added new tasks. - taskList.shutdown(); - } - - // Pauses execution for a moment - // so that system switches back and forth - public void pause(double seconds) - { - try - { - Thread.sleep(Math.round(1000.0 * seconds)); - } - catch (InterruptedException e) - { - e.printStackTrace(); - } - } - - // Driver method - public static void main(String[] args) - { - new MainApp().startThread(); - } -} diff --git a/pages/institutions.py b/pages/institutions.py index 97397367..3e1bd76b 100644 --- a/pages/institutions.py +++ b/pages/institutions.py @@ -60,9 +60,7 @@ class InstitutionAdminDashboardPage(BaseInstitutionPage): url_addition = '/dashboard' identity = Locator(By.CSS_SELECTOR, 'img[alt="Center For Open Science [Test]"]') - loading_indicator = Locator( - By.CSS_SELECTOR, '.ball-scale', settings.LONG_TIMEOUT - ) + loading_indicator = Locator(By.CSS_SELECTOR, '.ball-scale', settings.LONG_TIMEOUT) title_containers = GroupLocator( By.CSS_SELECTOR, '._title-container_1d9vmx', @@ -84,28 +82,40 @@ class InstitutionAdminDashboardPage(BaseInstitutionPage): By.CSS_SELECTOR, 'div._table-wrapper_1w5vdt > div > div.ember-view > div > div > table > tbody > tr', ) + def get_expanded_total_by_expanded_name(self, department): - for element in self.department_options: - name_elem = element.find_element(By.CSS_SELECTOR, "[data-test-expanded-name]") - if name_elem.text.strip() == department: - total_elem = element.find_element(By.CSS_SELECTOR, "[data-test-expanded-total]") + for element in self.department_options: + name_elem = element.find_element( + By.CSS_SELECTOR, '[data-test-expanded-name]' + ) + if name_elem.text.strip() == department: + total_elem = element.find_element( + By.CSS_SELECTOR, '[data-test-expanded-total]' + ) return int(total_elem.text.strip()) def get_kpi_data_by_kpi_title(self, target_title): for container in self.kpi_container: - title_element = container.find_element(By.CSS_SELECTOR, "[data-test-kpi-title]") + title_element = container.find_element( + By.CSS_SELECTOR, '[data-test-kpi-title]' + ) if title_element.text.strip() == target_title: - value_element = container.find_element(By.CSS_SELECTOR, "[data-test-kpi-data]") + value_element = container.find_element( + By.CSS_SELECTOR, '[data-test-kpi-data]' + ) return value_element.text.strip() def click_on_listbox_trigger(self, section_title): for section in self.title_containers: - title_element = section.find_element(By.CSS_SELECTOR, "[data-test-chart-title]") + title_element = section.find_element( + By.CSS_SELECTOR, '[data-test-chart-title]' + ) if title_element.text.strip() == section_title: - button = section.find_element(By.CSS_SELECTOR, "[data-test-expand-additional-data]") - icon = section.find_element(By.CSS_SELECTOR, "[data-test-toggle-icon]") + button = section.find_element( + By.CSS_SELECTOR, '[data-test-expand-additional-data]' + ) + icon = section.find_element(By.CSS_SELECTOR, '[data-test-toggle-icon]') button.click() WebDriverWait(self.driver, 10).until( - lambda d: icon.get_attribute("data-icon") == "caret-up" + lambda d: icon.get_attribute('data-icon') == 'caret-up' ) - diff --git a/pages/preprints.py b/pages/preprints.py index d598173b..a2a3358c 100644 --- a/pages/preprints.py +++ b/pages/preprints.py @@ -2,8 +2,10 @@ 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, @@ -113,19 +115,22 @@ def select_from_dropdown_listbox(self, selection): ) def select_top_level_subject(self, selection): + subject_selector = 'div[data-analytics-scope="Browse"] > ul > li' + wait = WebDriverWait(self.driver, 5) + 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: - # 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() + 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]') basics_tags_input = Locator( - By.CSS_SELECTOR, 'input[aria-label="Add a tag to enhance discoverability"]' + By.CSS_SELECTOR, 'input[placeholder="Add a tag to enhance discoverability"]' ) # Author Assertions Page conflict_of_interest_yes = Locator( diff --git a/tests/test_institutions.py b/tests/test_institutions.py index 244dafd2..bb1ce30c 100644 --- a/tests/test_institutions.py +++ b/tests/test_institutions.py @@ -1,4 +1,5 @@ import pytest + import markers from api import osf_api from pages.institutions import ( @@ -58,7 +59,9 @@ def test_institution_admin_dashboard(self, driver, session, must_be_logged_in): api_public_project_count = metrics_data['attributes']['public_project_count'] api_private_project_count = metrics_data['attributes']['private_project_count'] - total_project_count = dashboard_page.get_kpi_data_by_kpi_title('OSF Public and Private Projects') + total_project_count = dashboard_page.get_kpi_data_by_kpi_title( + 'OSF Public and Private Projects' + ) # Verify Total User Count displayed_user_count = dashboard_page.get_kpi_data_by_kpi_title('Total Users') @@ -67,14 +70,19 @@ def test_institution_admin_dashboard(self, driver, session, must_be_logged_in): dashboard_page.click_on_listbox_trigger('Public vs Private Projects') # Verify Public Project Count - displayed_public_project_count = dashboard_page.get_expanded_total_by_expanded_name('Public Projects') + displayed_public_project_count = ( + dashboard_page.get_expanded_total_by_expanded_name('Public Projects') + ) assert int(displayed_public_project_count) == api_public_project_count # Verify Private Project Count - displayed_private_project_count = dashboard_page.get_expanded_total_by_expanded_name('Private Projects') + displayed_private_project_count = ( + dashboard_page.get_expanded_total_by_expanded_name('Private Projects') + ) assert int(displayed_private_project_count) == api_private_project_count # Verify Total Project Count assert ( - int(total_project_count) == api_public_project_count + api_private_project_count + int(total_project_count) + == api_public_project_count + api_private_project_count ) diff --git a/tests/test_preprints.py b/tests/test_preprints.py index f4c040af..2a31aec6 100644 --- a/tests/test_preprints.py +++ b/tests/test_preprints.py @@ -277,34 +277,30 @@ def test_edit_preprint(self, session, driver, preprint_detail_page): WebDriverWait(driver, 5).until( EC.visibility_of_element_located((By.CSS_SELECTOR, '[data-test-title]')) ) - edit_page.next_button.click() - - # Next add another subject in the Discipline section - WebDriverWait(driver, 5).until( - EC.visibility_of_element_located( - ( - By.CSS_SELECTOR, - 'div.ember-tabs__tab-panel.ember-tabs__tab-panel--selected > div >ul >li>label > input', - ) - ) - ) - edit_page.scroll_into_view(edit_page.basics_tags_input.element) edit_page.select_top_level_subject('Business') - # Add another Tag and click the Save and continue button edit_page.basics_tags_input.send_keys(os.environ['PYTEST_CURRENT_TEST']) edit_page.basics_tags_input.send_keys(Keys.RETURN) # Click Return to preprint button to go back to Preprint Detail page + + body = driver.find_element(By.TAG_NAME, 'body') + body.send_keys(Keys.HOME) + WebDriverWait(driver, 5).until( + EC.invisibility_of_element_located((By.CLASS_NAME, 'toast-success')) + ) + WebDriverWait(driver, 5).until( + EC.visibility_of_element_located( + (By.CSS_SELECTOR, '[data-test-next-button]') + ) + ) edit_page.next_button.click() WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-test-next-button]')) ) - edit_page.next_button.click() WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-test-next-button]')) ) - edit_page.next_button.click() WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-test-submit-button]'))