testinfra: refactor screenshot taking

Reduce the screenshots to a single utility function to avoid copying a
lot of boilerplate.

Change-Id: Iad1c7afa4e9ea9a4ddaca5e62751795e60bc2980
This commit is contained in:
Ian Wienand 2021-09-07 10:47:38 +10:00
parent 1dde7628e8
commit 9ba398dee9
5 changed files with 95 additions and 137 deletions

View File

@ -12,11 +12,10 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
import time import time
from util import take_screenshots
testinfra_hosts = [ testinfra_hosts = [
'review02.opendev.org', 'review02.opendev.org',
] ]
@ -33,46 +32,21 @@ def test_gerrit_x_project_clone(host):
assert cmd.succeeded assert cmd.succeeded
def test_gerrit_screenshot(host): def test_gerrit_screenshot(host):
driver = webdriver.Remote(
command_executor='http://%s:4444/wd/hub' % (host.backend.get_hostname()),
desired_capabilities=webdriver.DesiredCapabilities.FIREFOX)
try: # Click the gerrit results tab into view
driver.get("http://localhost:8081") script = ("document.querySelector('gr-app').shadowRoot"
WebDriverWait(driver, 30).until(lambda driver: driver.execute_script(
'return document.readyState') == 'complete')
# NOTE(ianw): The page doesn't really seem to be complete at
# this point, not sure what to listen for...
time.sleep(5)
driver.save_screenshot("/var/log/screenshots/gerrit-main-page.png")
for change in (1, 2):
driver.get("http://localhost:8081/c/x/test-project/+/%s" % change)
time.sleep(5)
driver.execute_script(
"document.querySelector('gr-app').shadowRoot"
".querySelector('gr-app-element').shadowRoot" ".querySelector('gr-app-element').shadowRoot"
".querySelector('main')" ".querySelector('main')"
".querySelector('gr-change-view').shadowRoot" ".querySelector('gr-change-view').shadowRoot"
".querySelector('paper-tab[data-name=\"change-view-tab-header-zuul-results-summary\"]')" ".querySelector('paper-tab[data-name=\"change-view-tab-header-zuul-results-summary\"]')"
".click()" ".click()")
shots = (
('http://localhost:8081', None, 'gerrit-main-page.png'),
('http://localhost:8081/c/x/test-project/+/1', script,
'gerrit-change-page-1.png'),
('http://localhost:8081/c/x/test-project/+/2', script,
'gerrit-change-page-2.png')
) )
time.sleep(5)
original_size = driver.get_window_size() take_screenshots(host, shots)
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/gerrit-change-page-%s.png" % change)
driver.set_window_size(
original_size['width'], original_size['height'])
except TimeoutException as e:
raise e
finally:
driver.quit()

View File

@ -12,11 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import time from util import take_screenshots
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
testinfra_hosts = ['gitea99.opendev.org'] testinfra_hosts = ['gitea99.opendev.org']
@ -91,46 +87,13 @@ def test_project_clone(host):
assert cmd.succeeded assert cmd.succeeded
def test_gitea_screenshots(host): 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 = ( shots = (
('https://localhost:3081', 'gitea-main.png'), ('https://localhost:3081', None, 'gitea-main.png'),
('https://localhost:3081/opendev/system-config', ('https://localhost:3081/opendev/system-config', None,
'gitea-project-system-config.png'), 'gitea-project-system-config.png'),
('https://localhost:3081/opendev/disk-image-builder', ('https://localhost:3081/opendev/disk-image-builder', None,
'gitea-project-dib.png') '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'). \ take_screenshots(host, shots)
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()

View File

@ -12,11 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from selenium import webdriver from util import take_screenshots
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
import time
testinfra_hosts = ['grafana01.opendev.org'] testinfra_hosts = ['grafana01.opendev.org']
@ -41,32 +37,8 @@ def test_grafana_api_denial(host):
assert '403 Forbidden' in cmd.stdout assert '403 Forbidden' in cmd.stdout
def test_grafana_screenshots(host): def test_grafana_screenshots(host):
driver = webdriver.Remote( shots = (
command_executor='http://%s:4444/wd/hub' % (host.backend.get_hostname()), ('https://localhost/', None, 'grafana-main-page.png'),
desired_capabilities=webdriver.DesiredCapabilities.FIREFOX) ('https://localhost/dashboards', None, 'grafana-dashboards-page.png')
)
try: take_screenshots(host, shots)
driver.get("https://localhost")
WebDriverWait(driver, 30).until(lambda driver: driver.execute_script(
'return document.readyState') == 'complete')
# NOTE(ianw): The page doesn't really seem to be complete at
# this point, not sure what to listen for...
time.sleep(5)
driver.save_screenshot("/var/log/screenshots/grafana-main-page.png")
driver.get("https://localhost/dashboards")
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/grafana-dashboards-page.png")
driver.set_window_size(
original_size['width'], original_size['height'])
except TimeoutException as e:
raise e
finally:
driver.quit()

View File

@ -13,10 +13,8 @@
# under the License. # under the License.
import requests import requests
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait from util import take_screenshots
from selenium.common.exceptions import TimeoutException
import time
testinfra_hosts = ['paste01.opendev.org'] testinfra_hosts = ['paste01.opendev.org']
@ -54,18 +52,8 @@ def test_paste_robots(host):
assert 'Disallow: /' in cmd.stdout assert 'Disallow: /' in cmd.stdout
def test_paste_screenshots(host): def test_paste_screenshots(host):
driver = webdriver.Remote( shots = (
command_executor='http://%s:4444/wd/hub' % (host.backend.get_hostname()), ('https://localhost', None, 'paste-main-page.png'),
desired_capabilities=webdriver.DesiredCapabilities.FIREFOX) )
try: take_screenshots(host, shots)
driver.get("https://localhost")
WebDriverWait(driver, 30).until(lambda driver: driver.execute_script(
'return document.readyState') == 'complete')
time.sleep(5)
driver.save_screenshot("/var/log/screenshots/paste-main-page.png")
except TimeoutException as e:
raise e
finally:
driver.quit()

View File

@ -13,6 +13,67 @@
# under the License. # under the License.
import socket import socket
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
def take_screenshots(host, shots):
"""Take screenshots
host: the testinfra host info of the remote host where selenium is
running
shots: a list where each element is a list consisting of
* (str) URL to screenshot
* (str) Javascript to execute before shot, None to skip
* (str) filename.png, will be placed in /var/log/screenshots for collection
"""
driver = webdriver.Remote(
command_executor='http://%s:4444/wd/hub' % (host.backend.get_hostname()),
desired_capabilities=webdriver.DesiredCapabilities.FIREFOX)
try:
for url, execute, png in shots:
driver.get(url)
WebDriverWait(driver, 30).until(
lambda driver: driver.execute_script(
'return document.readyState') == 'complete')
if execute:
time.sleep(5)
driver.execute_script(execute)
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()
def get_ips(value, family=None): def get_ips(value, family=None):
ret = set() ret = set()