From ec33d00ec997b3c57a4cef8219cda8a1f9f6e401 Mon Sep 17 00:00:00 2001 From: Mitchel Baker Date: Fri, 2 Aug 2024 17:00:08 +0000 Subject: [PATCH] encapsulate selenium driver setup into a class --- .github/workflows/run_smoke_test.yml | 2 +- .../Functional/SmokeTests/selenium_browser.py | 31 +++++++++++++++++++ Testing/Functional/SmokeTests/smoke_test.py | 6 ++-- .../Functional/SmokeTests/smoke_test_utils.py | 10 +++--- Testing/Functional/conftest.py | 14 +++------ 5 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 Testing/Functional/SmokeTests/selenium_browser.py diff --git a/.github/workflows/run_smoke_test.yml b/.github/workflows/run_smoke_test.yml index f464ad64..6f337f9a 100644 --- a/.github/workflows/run_smoke_test.yml +++ b/.github/workflows/run_smoke_test.yml @@ -63,4 +63,4 @@ jobs: json: ${{ secrets.GWS_GITHUB_AUTOMATION_CREDS }} - name: Run ScubaGoggles and check for correct output - run: pytest -s ./Testing/Functional/SmokeTests/ --subjectemail="${{ secrets.GWS_SUBJECT_EMAIL }}" --domain="${{ secrets.GWS_DOMAIN }}" + run: pytest -s -vvv ./Testing/Functional/SmokeTests/ --subjectemail="${{ secrets.GWS_SUBJECT_EMAIL }}" --domain="${{ secrets.GWS_DOMAIN }}" diff --git a/Testing/Functional/SmokeTests/selenium_browser.py b/Testing/Functional/SmokeTests/selenium_browser.py new file mode 100644 index 00000000..746436ca --- /dev/null +++ b/Testing/Functional/SmokeTests/selenium_browser.py @@ -0,0 +1,31 @@ +""" +The Browser class encapsulates the setup, usage, and teardown of a +Selenium WebDriver instance for automated browser interactions. +""" + +from selenium import webdriver +from selenium.webdriver.chrome.options import Options + +class Browser: + def __init__(self): + chrome_options = Options() + chrome_options.add_argument("--headless") + chrome_options.add_argument("--no-sandbox") + chrome_options.add_argument("--disable-dev-shm-usage") + + self.driver = webdriver.Chrome(options=chrome_options) + + def get(self, url): + self.driver.get(url) + + def quit(self): + self.driver.quit() + + def find_element(self, by, value): + return self.driver.find_element(by, value) + + def find_elements(self, by, value): + return self.driver.find_elements(by, value) + + def current_url(self): + return self.driver.current_url \ No newline at end of file diff --git a/Testing/Functional/SmokeTests/smoke_test.py b/Testing/Functional/SmokeTests/smoke_test.py index 75d65a2d..8470456c 100644 --- a/Testing/Functional/SmokeTests/smoke_test.py +++ b/Testing/Functional/SmokeTests/smoke_test.py @@ -1,5 +1,5 @@ """ -smoke_test.py is a test script to verify `scubagoggles gws`. +smoke_test.py is a test script to verify `scubagoggles gws` output. It checks for the following cases: - Generate the correct output files (BaselineReports.html, ScubaResults.json, etc) @@ -39,7 +39,9 @@ def test_scubaresults(self): with open(scubaresults_path) as jsonfile: verify_scubaresults(jsonfile) except ValueError as e: - raise ValueError(f"{scubaresults_path} contains invalid json, {e}") + pytest.fail(f"{scubaresults_path} contains invalid json, {e}") + except Exception as e: + pytest.fail(f"An error occurred, {e}") def test_scubagoggles_report(self, browser, domain): try: diff --git a/Testing/Functional/SmokeTests/smoke_test_utils.py b/Testing/Functional/SmokeTests/smoke_test_utils.py index fc952c9e..2aa20884 100644 --- a/Testing/Functional/SmokeTests/smoke_test_utils.py +++ b/Testing/Functional/SmokeTests/smoke_test_utils.py @@ -61,9 +61,9 @@ def verify_all_outputs_exist(output: list, required_entries: list): def verify_scubaresults(jsonfile): scubaresults = json.load(jsonfile) - summary = scubaresults["Summary"] - for v in summary.values(): - assert v["Errors"] == 0 + summaries = scubaresults["Summary"] + for summary in summaries.values(): + assert summary["Errors"] == 0 def run_selenium(browser, domain): verify_navigation_links(browser) @@ -95,7 +95,7 @@ def run_selenium(browser, domain): individual_report_anchor = baseline_report.find_element(By.TAG_NAME, "a") individual_report_anchor_href = individual_report_anchor.get_attribute("href") individual_report_anchor.click() - current_url = browser.current_url + current_url = browser.current_url() assert individual_report_anchor_href == current_url # Check at the individual report level @@ -132,7 +132,7 @@ def run_selenium(browser, domain): ) parent_report_anchor_href = parent_report_anchor.get_attribute("href") parent_report_anchor.click() - current_url = browser.current_url + current_url = browser.current_url() assert parent_report_anchor_href == current_url WebDriverWait(browser, 10).until( diff --git a/Testing/Functional/conftest.py b/Testing/Functional/conftest.py index bb0ae9c5..b49a89fa 100644 --- a/Testing/Functional/conftest.py +++ b/Testing/Functional/conftest.py @@ -1,11 +1,5 @@ import pytest -from selenium import webdriver -from selenium.webdriver.chrome.options import Options - -chrome_options = Options() -chrome_options.add_argument("--headless") -chrome_options.add_argument("--no-sandbox") -chrome_options.add_argument("--disable-dev-shm-usage") +from SmokeTests.selenium_browser import Browser def pytest_addoption(parser): parser.addoption("--subjectemail", action="store") @@ -21,6 +15,6 @@ def domain(pytestconfig): @pytest.fixture def browser(): - driver = webdriver.Chrome(options=chrome_options) - yield driver - driver.quit() \ No newline at end of file + browser_instance = Browser() + yield browser_instance + browser_instance.quit() \ No newline at end of file