Functional tests: fix waiting for redirects

wait_for_page_update has an 'expected_url' argument that you can use
to make the browser wait until it reaches an expected URL after a
redirect.

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Michael Pimmer 2018-10-31 17:14:04 +01:00 committed by James Valleroy
parent 33dde221d1
commit bcd84ab92f
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 23 additions and 9 deletions

View File

@ -62,19 +62,32 @@ def eventually(function, args=[], timeout=30):
@contextmanager
def wait_for_page_update(browser, timeout=300):
current_page = browser.find_by_tag('body').first
def wait_for_page_update(browser, timeout=300, expected_url=None):
page_body = browser.find_by_tag('body').first
yield
WebDriverWait(browser, timeout).until(is_stale(current_page))
WebDriverWait(browser, timeout).until(page_loaded(page_body, expected_url))
class is_stale():
def __init__(self, element):
class page_loaded():
"""
Wait until a page (re)loaded.
- element: Wait until this element gets stale
- expected_url (optional): Wait for the URL to become <expected_url>.
This can be necessary to wait for a redirect to finish.
"""
def __init__(self, element, expected_url=None):
self.element = element
self.expected_url = expected_url
def __call__(self, driver):
is_stale = False
try:
self.element.has_class('whatever_class')
return False
except StaleElementReferenceException:
return True
if self.expected_url is None:
is_stale = True
else:
if driver.url.endswith(self.expected_url):
is_stale = True
return is_stale

View File

@ -210,7 +210,8 @@ def backup_restore(browser, app_name):
browser.find_link_by_href(
'/plinth/sys/backups/restore-archive/_functional_test_' +
app_name + '/').first.click()
submit(browser)
with wait_for_page_update(browser, expected_url='/plinth/sys/backups/'):
submit(browser)
def backup_upload_and_restore(browser, app_name, downloaded_file_path):
@ -222,7 +223,7 @@ def backup_upload_and_restore(browser, app_name, downloaded_file_path):
# submit upload form
submit(browser)
# submit restore form
with wait_for_page_update(browser):
with wait_for_page_update(browser, expected_url='/plinth/sys/backups/'):
submit(browser)