From 1dde7628e8a93e8b023ad923cc9ca7cf2031d3d4 Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Mon, 6 Sep 2021 15:14:46 +1000
Subject: [PATCH] gitea: add some screenshots to testing

Change-Id: Id13fdd8ffbca1b0cd19858419d68f012e33f3ba8
---
 playbooks/test-gitea.yaml |  4 ++++
 testinfra/test_gitea.py   | 50 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/playbooks/test-gitea.yaml b/playbooks/test-gitea.yaml
index 01c65f03a4..990794d16d 100644
--- a/playbooks/test-gitea.yaml
+++ b/playbooks/test-gitea.yaml
@@ -1,5 +1,9 @@
 - hosts: "gitea"
   tasks:
+    - name: Run selenium container
+      include_role:
+        name: run-selenium
+
     - name: Test base jobs project was created in gitea
       uri:
         url: "https://localhost:3000/opendev/base-jobs"
diff --git a/testinfra/test_gitea.py b/testinfra/test_gitea.py
index 1e893c76f6..265eb17395 100644
--- a/testinfra/test_gitea.py
+++ b/testinfra/test_gitea.py
@@ -12,6 +12,11 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
+import time
+
+from selenium import webdriver
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.common.exceptions import TimeoutException
 
 testinfra_hosts = ['gitea99.opendev.org']
 
@@ -84,3 +89,48 @@ def test_project_clone(host):
         '/tmp/disk-image-builder')
     assert "Cloning into '/tmp/disk-image-builder'..." in cmd.stderr
     assert cmd.succeeded
+
+def test_gitea_screenshots(host):
+        driver = webdriver.Remote(
+        command_executor='http://%s:4444/wd/hub' % (host.backend.get_hostname()),
+        desired_capabilities=webdriver.DesiredCapabilities.FIREFOX)
+
+        shots = (
+            ('https://localhost:3081', 'gitea-main.png'),
+            ('https://localhost:3081/opendev/system-config',
+             'gitea-project-system-config.png'),
+            ('https://localhost:3081/opendev/disk-image-builder',
+             'gitea-project-dib.png')
+        )
+        try:
+            for url, png in shots:
+                driver.get(url)
+                WebDriverWait(driver, 30).until(
+                    lambda driver: driver.execute_script(
+                        'return document.readyState') == 'complete')
+                time.sleep(5)
+                # NOTE(ianw) This is a mash-up of things I found on
+                # stackoverflow and other bits googling "full size
+                # screenshot".  You expand the viewport and take a
+                # shot of the <body> element so that you don't also
+                # get scrollbars in the shot, with some tweaking
+                # because the window size.  Apparently selinum 4
+                # will have getFullPageScreeshotAs, so we should switch
+                # to that when available.
+                original_size = driver.get_window_size()
+                required_width = driver.execute_script(
+                    'return document.body.parentNode.scrollWidth')
+                required_height = driver.execute_script(
+                    'return document.body.parentNode.scrollHeight') + 100
+                driver.set_window_size(required_width, required_height)
+
+                driver.find_element_by_tag_name('body'). \
+                    screenshot("/var/log/screenshots/%s" % png)
+
+                driver.set_window_size(
+                    original_size['width'], original_size['height'])
+
+        except TimeoutException as e:
+            raise e
+        finally:
+            driver.quit()