From 9239933d2a9baf2f92e53d37413c7aba61e6883c Mon Sep 17 00:00:00 2001
From: William Desportes <williamdes@wdes.fr>
Date: Tue, 31 Dec 2024 11:54:26 +0100
Subject: [PATCH] Add debug for when the page fails

---
 debian/tests/test_phpldapadmin_web.py | 88 ++++++++++++++-------------
 1 file changed, 46 insertions(+), 42 deletions(-)

diff --git a/debian/tests/test_phpldapadmin_web.py b/debian/tests/test_phpldapadmin_web.py
index 6e6038f..eb05f21 100644
--- a/debian/tests/test_phpldapadmin_web.py
+++ b/debian/tests/test_phpldapadmin_web.py
@@ -1,6 +1,7 @@
 import unittest
 
 from selenium import webdriver
+from selenium.common.exceptions import TimeoutException
 from selenium.webdriver.chrome.service import Service
 from selenium.webdriver.support.wait import WebDriverWait
 from selenium.webdriver.common.by import By
@@ -11,6 +12,14 @@ import time
 
 class TestphpLDAPadminWeb(unittest.TestCase):
 
+    def wait_and_print_on_failure(self, by, value, timeout=10):
+        try:
+            return WebDriverWait(self.driver, timeout).until(lambda x: x.find_element(by, value))
+        except TimeoutException as e:
+            print("Page source on failure:")
+            print(self.driver.page_source)
+            raise e
+
     def setUp(self):
 
         options = webdriver.ChromeOptions()
@@ -28,52 +37,52 @@ class TestphpLDAPadminWeb(unittest.TestCase):
         self.driver.get("http://localhost:8888/phpldapadmin/")
         self.assertIn("phpLDAPadmin", self.driver.title)
 
-        login_btn = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//a[@title='Login to My LDAP Server']"))
+        login_btn = self.wait_and_print_on_failure(By.XPATH, "//a[@title='Login to My LDAP Server']")
         login_btn.click()
 
         # Anonymous checkbox on login page
-        WebDriverWait(self.driver, 60).until(lambda x: x.find_element(By.NAME, "anonymous_bind"))
+        self.wait_and_print_on_failure(By.NAME, "anonymous_bind", timeout=60)
 
-        title = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.CLASS_NAME, "title"))
+        title = self.wait_and_print_on_failure(By.CLASS_NAME, "title")
         self.assertIn("Authenticate to server My LDAP Server", title.text)
 
-        user = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "login"))
+        user = self.wait_and_print_on_failure(By.ID, "login")
         user.send_keys("cn=admin,dc=testing,dc=local")
 
-        password = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "password"))
+        password = self.wait_and_print_on_failure(By.ID, "password")
         password.send_keys("public")
 
         password.submit()
 
-        tree_menu = WebDriverWait(self.driver, 60).until(lambda x: x.find_element(By.CLASS_NAME, "treemenudiv"))
+        tree_menu = self.wait_and_print_on_failure(By.CLASS_NAME, "treemenudiv", timeout=60)
 
     def do_expand(self, img_name):
-        expander = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, f"//img[@src='images/default/{img_name}']"))
+        expander = self.wait_and_print_on_failure(By.XPATH, f"//img[@src='images/default/{img_name}']")
         expander.click()
 
     def test_title_and_footer(self):
         self.driver.get("http://localhost:8888/phpldapadmin/")
         self.assertIn("phpLDAPadmin", self.driver.title)
 
-        version = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "ajFOOT"))
+        version = self.wait_and_print_on_failure(By.ID, "ajFOOT")
         self.assertIn("1.2.", version.text)
 
-        login_btn = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.CLASS_NAME, "logged_in"))
+        login_btn = self.wait_and_print_on_failure(By.CLASS_NAME, "logged_in")
         self.assertIn("login", login_btn.text)
 
     def test_login_and_export(self):
         self.do_login()
 
-        tree_item = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//a[@title='dc=testing,dc=local']"))
+        tree_item = self.wait_and_print_on_failure(By.XPATH, "//a[@title='dc=testing,dc=local']")
         tree_item.click()
 
-        export_btn = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//a[@title='Save a dump of this object']"))
+        export_btn = self.wait_and_print_on_failure(By.XPATH, "//a[@title='Save a dump of this object']")
         export_btn.click()
 
-        export_form = WebDriverWait(self.driver, 60).until(lambda x: x.find_element(By.ID, "export_form"))
+        export_form = self.wait_and_print_on_failure(By.ID, "export_form", timeout=60)
         export_form.submit()
 
-        export_result = WebDriverWait(self.driver, 60).until(lambda x: x.find_element(By.ID, "ajBODY"))
+        export_result = self.wait_and_print_on_failure(By.ID, "ajBODY", timeout=60)
 
         self.assertIn("# LDIF Export for dc=testing,dc=local", export_result.text)
         self.assertIn("# Server: My LDAP Server (127.0.0.1)", export_result.text)
@@ -92,7 +101,6 @@ class TestphpLDAPadminWeb(unittest.TestCase):
         self.assertIn("objectclass: organization", export_result.text)
 
     def test_login_and_import(self):
-
         Simpsons = open("/usr/share/doc/phpldapadmin/Simpsons.ldif", "r")
         Simpsons = Simpsons.read()
         self.assertIn("o=Simpsons", Simpsons)
@@ -100,19 +108,19 @@ class TestphpLDAPadminWeb(unittest.TestCase):
 
         self.do_login()
 
-        import_btn = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//a[@title='Import My LDAP Server']"))
+        import_btn = self.wait_and_print_on_failure(By.XPATH, "//a[@title='Import My LDAP Server']")
         import_btn.click()
 
-        import_form = WebDriverWait(self.driver, 60).until(lambda x: x.find_element(By.CLASS_NAME, "new_value"))
+        import_form = self.wait_and_print_on_failure(By.CLASS_NAME, "new_value", timeout=60)
 
-        import_area = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//textarea[@name='ldif']"))
+        import_area = self.wait_and_print_on_failure(By.XPATH, "//textarea[@name='ldif']")
 
         for line in Simpsons.split('\n'):
             import_area.send_keys(line + "\n")
 
         import_form.submit()
 
-        import_result = WebDriverWait(self.driver, 60).until(lambda x: x.find_element(By.ID, "ajBODY"))
+        import_result = self.wait_and_print_on_failure(By.ID, "ajBODY", timeout=60)
 
         self.assertIn("STDIN 30,367 bytes (LDIF Import)", import_result.text)
         self.assertIn("Adding", import_result.text)
@@ -129,7 +137,7 @@ class TestphpLDAPadminWeb(unittest.TestCase):
         self.assertIn("ou=Pets,o=Simpsons,dc=testing,dc=local", import_result.text)
         self.assertIn("cn=Santas Little Helper,ou=Pets,o=Simpsons,dc=testing,dc=local", import_result.text)
 
-        WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//a[@title='Refresh My LDAP Server']")).click()
+        self.wait_and_print_on_failure(By.XPATH, "//a[@title='Refresh My LDAP Server']").click()
 
         # Expand dc=testing,dc=local
         self.do_expand("tree_expand_corner_first.png")
@@ -140,57 +148,53 @@ class TestphpLDAPadminWeb(unittest.TestCase):
         # Expand ou=Pets
         self.do_expand("tree_expand.png")
 
+        self.wait_and_print_on_failure(By.XPATH, "//a[@title='cn=Santas Little Helper,ou=Pets,o=Simpsons,dc=testing,dc=local']").click()
 
-        WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//a[@title='cn=Santas Little Helper,ou=Pets,o=Simpsons,dc=testing,dc=local']")).click()
-
-        title = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//h3[@class='title']"))
+        title = self.wait_and_print_on_failure(By.XPATH, "//h3[@class='title']")
         self.assertIn("cn=Santas Little Helper", title.text)
 
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_objectclass_0"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_objectclass_0")
         self.assertIn("inetOrgPerson", object_class.get_attribute("value"))
 
-        WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//a[@title='cn=Marge Simpson,ou=People,o=Simpsons,dc=testing,dc=local']")).click()
+        self.wait_and_print_on_failure(By.XPATH, "//a[@title='cn=Marge Simpson,ou=People,o=Simpsons,dc=testing,dc=local']").click()
 
-        title = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//h3[@class='title']"))
+        title = self.wait_and_print_on_failure(By.XPATH, "//h3[@class='title']")
         self.assertIn("cn=Marge Simpson", title.text)
 
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_objectclass_0"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_objectclass_0")
         self.assertIn("inetOrgPerson", object_class.get_attribute("value"))
 
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_objectclass_3"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_objectclass_3")
         self.assertIn("shadowAccount", object_class.get_attribute("value"))
 
         # Check cn=simpsons-pg
-        WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//a[@title='cn=simpsons-pg,o=Simpsons,dc=testing,dc=local']")).click()
+        self.wait_and_print_on_failure(By.XPATH, "//a[@title='cn=simpsons-pg,o=Simpsons,dc=testing,dc=local']").click()
 
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_memberuid_0"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_memberuid_0")
         self.assertIn("maggie", object_class.get_attribute("value"))
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_memberuid_1"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_memberuid_1")
         self.assertIn("marg", object_class.get_attribute("value"))
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_memberuid_2"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_memberuid_2")
         self.assertIn("lisa", object_class.get_attribute("value"))
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_memberuid_3"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_memberuid_3")
         self.assertIn("homer", object_class.get_attribute("value"))
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_memberuid_4"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_memberuid_4")
         self.assertIn("bart", object_class.get_attribute("value"))
 
         # Check cn=simpsons-goun
-        WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.XPATH, "//a[@title='cn=simpsons-goun,o=Simpsons,dc=testing,dc=local']")).click()
+        self.wait_and_print_on_failure(By.XPATH, "//a[@title='cn=simpsons-goun,o=Simpsons,dc=testing,dc=local']").click()
 
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_uniquemember_0"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_uniquemember_0")
         self.assertIn("cn=Bart Simpson,ou=People,o=Simpsons,dc=testing,dc=local", object_class.get_attribute("value"))
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_uniquemember_1"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_uniquemember_1")
         self.assertIn("cn=Homer Simpson,ou=People,o=Simpsons,dc=testing,dc=local", object_class.get_attribute("value"))
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_uniquemember_2"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_uniquemember_2")
         self.assertIn("cn=Lisa Simpson,ou=People,o=Simpsons,dc=testing,dc=local", object_class.get_attribute("value"))
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_uniquemember_3"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_uniquemember_3")
         self.assertIn("cn=Maggie Simpson,ou=People,o=Simpsons,dc=testing,dc=local", object_class.get_attribute("value"))
-        object_class = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(By.ID, "new_values_uniquemember_4"))
+        object_class = self.wait_and_print_on_failure(By.ID, "new_values_uniquemember_4")
         self.assertIn("cn=Marge Simpson,ou=People,o=Simpsons,dc=testing,dc=local", object_class.get_attribute("value"))
 
-        #print(self.driver.page_source)
-
-
 if __name__ == '__main__':
     suite = unittest.TestLoader().loadTestsFromTestCase(TestphpLDAPadminWeb)
     result = unittest.TextTestRunner(verbosity=2).run(suite)
-- 
GitLab