Fixed a few flaky selenium tests.

Many thanks go to David Burns (@AutomatedTester) for helping me understand
css selectors and pointing me towards the correct selenium methods.
This commit is contained in:
Florian Apolloner 2014-01-03 00:31:53 +01:00
parent 756a6accdb
commit ed2828f0a0
2 changed files with 28 additions and 17 deletions

View File

@ -51,8 +51,29 @@ class AdminSeleniumWebDriverTestCase(StaticLiveServerCase):
Helper function that blocks until the element with the given tag name Helper function that blocks until the element with the given tag name
is found on the page. is found on the page.
""" """
self.wait_for(tag_name, timeout)
def wait_for(self, css_selector, timeout=10):
"""
Helper function that blocks until an css selector is found on the page.
"""
from selenium.common.exceptions import NoSuchElementException, WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
self.wait_until( self.wait_until(
lambda driver: driver.find_element_by_tag_name(tag_name), ec.presence_of_element_located((By.CSS_SELECTOR, css_selector)),
timeout
)
def wait_for_text(self, css_selector, text, timeout=10):
"""
Helper function that blocks until the text is found in the css selector.
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
self.wait_until(
ec.text_to_be_present_in_element_value(
(By.CSS_SELECTOR, css_selector), text),
timeout timeout
) )

View File

@ -993,9 +993,7 @@ class AdminRawIdWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase):
# The field now contains the selected band's id # The field now contains the selected band's id
self.selenium.switch_to_window(main_window) self.selenium.switch_to_window(main_window)
self.assertEqual( self.wait_for_text('#id_main_band', '42')
self.selenium.find_element_by_id('id_main_band').get_attribute('value'),
'42')
# Reopen the popup window and click on another band # Reopen the popup window and click on another band
self.selenium.find_element_by_id('lookup_id_main_band').click() self.selenium.find_element_by_id('lookup_id_main_band').click()
@ -1007,9 +1005,7 @@ class AdminRawIdWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase):
# The field now contains the other selected band's id # The field now contains the other selected band's id
self.selenium.switch_to_window(main_window) self.selenium.switch_to_window(main_window)
self.assertEqual( self.wait_for_text('#id_main_band', '98')
self.selenium.find_element_by_id('id_main_band').get_attribute('value'),
'98')
def test_many_to_many(self): def test_many_to_many(self):
self.admin_login(username='super', password='secret', login_url='/') self.admin_login(username='super', password='secret', login_url='/')
@ -1032,9 +1028,7 @@ class AdminRawIdWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase):
# The field now contains the selected band's id # The field now contains the selected band's id
self.selenium.switch_to_window(main_window) self.selenium.switch_to_window(main_window)
self.assertEqual( self.wait_for_text('#id_supporting_bands', '42')
self.selenium.find_element_by_id('id_supporting_bands').get_attribute('value'),
'42')
# Reopen the popup window and click on another band # Reopen the popup window and click on another band
self.selenium.find_element_by_id('lookup_id_supporting_bands').click() self.selenium.find_element_by_id('lookup_id_supporting_bands').click()
@ -1046,9 +1040,7 @@ class AdminRawIdWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase):
# The field now contains the two selected bands' ids # The field now contains the two selected bands' ids
self.selenium.switch_to_window(main_window) self.selenium.switch_to_window(main_window)
self.assertEqual( self.wait_for_text('#id_supporting_bands', '42,98')
self.selenium.find_element_by_id('id_supporting_bands').get_attribute('value'),
'42,98')
class AdminRawIdWidgetSeleniumChromeTests(AdminRawIdWidgetSeleniumFirefoxTests): class AdminRawIdWidgetSeleniumChromeTests(AdminRawIdWidgetSeleniumFirefoxTests):
@ -1087,10 +1079,8 @@ class RelatedFieldWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase):
save_button_css_selector = '.submit-row > input[type=submit]' save_button_css_selector = '.submit-row > input[type=submit]'
self.selenium.find_element_by_css_selector(save_button_css_selector).click() self.selenium.find_element_by_css_selector(save_button_css_selector).click()
self.selenium.switch_to_window(main_window) self.selenium.switch_to_window(main_window)
# Wait up to 2 seconds for the new option to show up after clicking save in the popup. # The field now contains the new user
self.selenium.implicitly_wait(2) self.wait_for('#id_user option[value="newuser"]')
self.selenium.find_element_by_css_selector('#id_user option[value=newuser]')
self.selenium.implicitly_wait(0)
# Go ahead and submit the form to make sure it works # Go ahead and submit the form to make sure it works
self.selenium.find_element_by_css_selector(save_button_css_selector).click() self.selenium.find_element_by_css_selector(save_button_css_selector).click()