From 9466d72923758aa5c3a93f184340895f32e43134 Mon Sep 17 00:00:00 2001
From: "robinwilliam.hundt" <robinwilliam.hundt@stud.uni-goettingen.de>
Date: Fri, 8 Mar 2019 15:08:38 +0100
Subject: [PATCH] Fix StaleElementException when waiting for loading of
 Subscriptions

---
 functional_tests/test_feedback_creation.py |  2 +-
 functional_tests/test_front_pages.py       |  2 +-
 functional_tests/util.py                   | 17 ++++++++++++-----
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/functional_tests/test_feedback_creation.py b/functional_tests/test_feedback_creation.py
index 667d7a48..042700d2 100644
--- a/functional_tests/test_feedback_creation.py
+++ b/functional_tests/test_feedback_creation.py
@@ -54,8 +54,8 @@ class UntestedParent:
 
         # stage can be 'initial', 'validate', or 'conflict'
         def _go_to_subscription(self, stage='initial', sub_type=None):
+            WebDriverWait(self.browser, 10).until(subscriptions_loaded_cond(self.browser))
             tasks = self.browser.find_element_by_name('subscription-list')
-            WebDriverWait(self.browser, 10).until(subscriptions_loaded_cond(tasks))
             tab = tasks.find_element_by_xpath(f'//*[contains(text(), "{stage}")]')
             tab.click()
             sub_type = sub_type if sub_type is not None else self.sub_type
diff --git a/functional_tests/test_front_pages.py b/functional_tests/test_front_pages.py
index c24400e2..742aacfd 100644
--- a/functional_tests/test_front_pages.py
+++ b/functional_tests/test_front_pages.py
@@ -47,10 +47,10 @@ class UntestedParent:
 
         def test_available_tasks_are_shown(self):
             self._login()
+            WebDriverWait(self.browser, 10).until(subscriptions_loaded_cond(self.browser))
             tasks = self.browser.find_element_by_name('subscription-list')
             title = tasks.find_element_by_class_name('v-toolbar__title')
             self.assertEqual('Tasks', title.text)
-            WebDriverWait(self.browser, 10).until(subscriptions_loaded_cond(tasks))
             subscription_links = extract_hrefs_hashes(
                 tasks.find_elements_by_tag_name('a')
             )
diff --git a/functional_tests/util.py b/functional_tests/util.py
index 7a45ec5a..2abaa248 100644
--- a/functional_tests/util.py
+++ b/functional_tests/util.py
@@ -8,6 +8,7 @@ from selenium.webdriver.firefox.options import Options
 from selenium.webdriver.remote.webelement import WebElement
 from selenium.webdriver.support import expected_conditions as ec
 from selenium.webdriver.support.ui import WebDriverWait
+from selenium.common.exceptions import StaleElementReferenceException
 
 
 def create_browser() -> webdriver.Firefox:
@@ -47,11 +48,17 @@ def extract_hrefs_hashes(web_elements: Sequence[WebElement]):
             for el in web_elements if el.get_attribute('href')]
 
 
-# A function that takes the element corresponding to the tasks
-# component and return a function that can be used as a condition for
+# A function that takes the a browser client
+# and returns a function that can be used as a condition for
 # WebDriverWait
-def subscriptions_loaded_cond(tasks_el):
+def subscriptions_loaded_cond(browser):
     def loaded(*args):
-        sub_links = tasks_el.find_elements_by_tag_name('a')
-        return any((link != '/home' for link in extract_hrefs_hashes(sub_links)))
+        for i in range(2):
+            try:
+                tasks_el = browser.find_element_by_name('subscription-list')
+                sub_links = tasks_el.find_elements_by_tag_name('a')
+                return any((link != '/home' for link in extract_hrefs_hashes(sub_links)))
+            except StaleElementReferenceException:
+                pass
+        return False
     return loaded
-- 
GitLab