From cfe21a70fa3046c32a8c874df2a6b41ae7fe6412 Mon Sep 17 00:00:00 2001 From: Jesse Zoldak Date: Thu, 15 Oct 2015 14:03:28 -0400 Subject: [PATCH 1/2] Fix the helpers for Select objects by wrapping them in promises --- common/test/acceptance/tests/helpers.py | 29 ++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/common/test/acceptance/tests/helpers.py b/common/test/acceptance/tests/helpers.py index d2cf2f5488..922a0e9993 100644 --- a/common/test/acceptance/tests/helpers.py +++ b/common/test/acceptance/tests/helpers.py @@ -21,6 +21,7 @@ from pymongo import MongoClient, ASCENDING from openedx.core.lib.tests.assertions.events import assert_event_matches, is_matching_event, EventMatchTolerates from xmodule.partitions.partitions import UserPartition from xmodule.partitions.tests.test_partitions import MockUserPartitionScheme +from selenium.common.exceptions import StaleElementReferenceException from selenium.webdriver.support.select import Select from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC @@ -201,18 +202,36 @@ def enable_css_animations(page): def select_option_by_text(select_browser_query, option_text): """ Chooses an option within a select by text (helper method for Select's select_by_visible_text method). + + Wrap this in a Promise to prevent a StaleElementReferenceException + from being raised while the DOM is still being rewritten """ - select = Select(select_browser_query.first.results[0]) - select.select_by_visible_text(option_text) + def select_option(query, value): + try: + select = Select(query.first.results[0]) + select.select_by_visible_text(value) + return True + except StaleElementReferenceException: + return False + EmptyPromise(lambda: select_option(select_browser_query, option_text), 'Selected option {}'.format(option_text)).fulfill() def get_selected_option_text(select_browser_query): """ Returns the text value for the first selected option within a select. - """ - select = Select(select_browser_query.first.results[0]) - return select.first_selected_option.text + Wrap this in a Promise to prevent a StaleElementReferenceException + from being raised while the DOM is still being rewritten + """ + def get_option(query): + try: + select = Select(select_browser_query.first.results[0]) + return (True, select.first_selected_option.text) + except StaleElementReferenceException: + return (False, None) + + text = Promise(lambda: get_option(select_browser_query), 'Retrieved selected option text').fulfill() + return text def get_options(select_browser_query): """ From 6207de94c351705239b4e5b7da348b687704d079 Mon Sep 17 00:00:00 2001 From: Jesse Zoldak Date: Fri, 16 Oct 2015 14:49:21 -0400 Subject: [PATCH 2/2] [fixup] fix quality violations --- common/test/acceptance/tests/helpers.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/common/test/acceptance/tests/helpers.py b/common/test/acceptance/tests/helpers.py index 922a0e9993..12c4baa736 100644 --- a/common/test/acceptance/tests/helpers.py +++ b/common/test/acceptance/tests/helpers.py @@ -207,13 +207,16 @@ def select_option_by_text(select_browser_query, option_text): from being raised while the DOM is still being rewritten """ def select_option(query, value): + """ Get the first select element that matches the query and select the desired value. """ try: select = Select(query.first.results[0]) select.select_by_visible_text(value) return True except StaleElementReferenceException: return False - EmptyPromise(lambda: select_option(select_browser_query, option_text), 'Selected option {}'.format(option_text)).fulfill() + + msg = 'Selected option {}'.format(option_text) + EmptyPromise(lambda: select_option(select_browser_query, option_text), msg).fulfill() def get_selected_option_text(select_browser_query): @@ -224,8 +227,9 @@ def get_selected_option_text(select_browser_query): from being raised while the DOM is still being rewritten """ def get_option(query): + """ Get the first select element that matches the query and return its value. """ try: - select = Select(select_browser_query.first.results[0]) + select = Select(query.first.results[0]) return (True, select.first_selected_option.text) except StaleElementReferenceException: return (False, None) @@ -233,6 +237,7 @@ def get_selected_option_text(select_browser_query): text = Promise(lambda: get_option(select_browser_query), 'Retrieved selected option text').fulfill() return text + def get_options(select_browser_query): """ Returns all the options for the given select.