From 4af305e97929b566773ede9bd12f7f97c8a47663 Mon Sep 17 00:00:00 2001
From: "robinwilliam.hundt" <robinwilliam.hundt@stud.uni-goettingen.de>
Date: Thu, 3 Jan 2019 16:05:41 +0100
Subject: [PATCH] Removed explicit waits in functional tests

---
 functional_tests/test_front_pages.py | 14 +++++++++++---
 functional_tests/test_login_page.py  | 11 ++++++-----
 functional_tests/util.py             |  3 +++
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/functional_tests/test_front_pages.py b/functional_tests/test_front_pages.py
index ec38b58f..adcc0975 100644
--- a/functional_tests/test_front_pages.py
+++ b/functional_tests/test_front_pages.py
@@ -1,7 +1,6 @@
-import time
-
 from django.test import LiveServerTestCase
 from selenium import webdriver
+from selenium.webdriver.support.ui import WebDriverWait
 
 from core import models
 from core.models import UserAccount
@@ -47,11 +46,20 @@ class UntestedParent:
             self.assertEqual('Statistics', title.text)
 
         def test_available_tasks_are_shown(self):
+            # A function that takes the element corresponding to the tasks
+            # component and return a function that can be used as a condition for
+            # WebDriverWait
+            def subscriptions_loaded_cond(tasks_el):
+                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)))
+                return loaded
+
             self._login()
             tasks = self.browser.find_element_by_name('subscription-list')
             title = tasks.find_element_by_class_name('v-toolbar__title')
             self.assertEqual('Tasks', title.text)
-            time.sleep(8)
+            WebDriverWait(self.browser, 6).until(subscriptions_loaded_cond(tasks))
             subscription_links = extract_hrefs_hashes(
                 tasks.find_elements_by_tag_name('a')
             )
diff --git a/functional_tests/test_login_page.py b/functional_tests/test_login_page.py
index 0849f0c6..23d5fb5d 100644
--- a/functional_tests/test_login_page.py
+++ b/functional_tests/test_login_page.py
@@ -1,8 +1,8 @@
-import time
-
 from django.test import LiveServerTestCase
 from selenium import webdriver
 from selenium.webdriver.common.keys import Keys
+from selenium.webdriver.support import expected_conditions as ec
+from selenium.webdriver.support.ui import WebDriverWait
 
 from core.models import UserAccount
 from util.factories import make_test_data
@@ -102,7 +102,7 @@ class LoginPageTest(LiveServerTestCase):
         password_input = self.browser.find_element_by_xpath('//input[@aria-label="Password"]')
         password_input.send_keys('p')
         self.browser.find_element_by_xpath('//button[@type="submit"]').send_keys(Keys.ENTER)
-        time.sleep(1)
+        WebDriverWait(self.browser, 3).until(ec.url_contains('/home'))
 
     def test_tutor_can_login(self):
         tutor = self.test_data['tutors'][0]
@@ -130,8 +130,9 @@ class LoginPageTest(LiveServerTestCase):
         username_input.send_keys(username)
         password_input = self.browser.find_element_by_id('input-register-password')
         password_input.send_keys(password)
-        self.browser.find_element_by_id('register-submit').click()
-        time.sleep(1)
+        register_submit_el = self.browser.find_element_by_id('register-submit')
+        register_submit_el.click()
+        WebDriverWait(self.browser, 3).until_not(ec.visibility_of(register_submit_el))
         tutor = UserAccount.objects.get(username=username)
         self.assertEqual(UserAccount.TUTOR, tutor.role)
         self.assertFalse(tutor.is_active, "Tutors should be inactive after registered")
diff --git a/functional_tests/util.py b/functional_tests/util.py
index 1333e36a..912d68a3 100644
--- a/functional_tests/util.py
+++ b/functional_tests/util.py
@@ -6,6 +6,8 @@ from selenium import webdriver
 from selenium.webdriver.common.keys import Keys
 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
 
 
 def create_browser() -> webdriver.Firefox:
@@ -23,6 +25,7 @@ def login(browser, live_server_url, username, password='p'):
     password_input = browser.find_element_by_xpath('//input[@aria-label="Password"]')
     password_input.send_keys(password)
     browser.find_element_by_xpath('//button[@type="submit"]').send_keys(Keys.ENTER)
+    WebDriverWait(browser, 3).until(ec.url_contains('/home'))
 
 
 def reset_browser_after_test(browser: webdriver.Firefox, live_server_url):
-- 
GitLab