Skip to content

Commit

Permalink
encapsulate selenium driver setup into a class
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelbaker-cisa committed Aug 2, 2024
1 parent b3741d4 commit ec33d00
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_smoke_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
31 changes: 31 additions & 0 deletions Testing/Functional/SmokeTests/selenium_browser.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions Testing/Functional/SmokeTests/smoke_test.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions Testing/Functional/SmokeTests/smoke_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 4 additions & 10 deletions Testing/Functional/conftest.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -21,6 +15,6 @@ def domain(pytestconfig):

@pytest.fixture
def browser():
driver = webdriver.Chrome(options=chrome_options)
yield driver
driver.quit()
browser_instance = Browser()
yield browser_instance
browser_instance.quit()

0 comments on commit ec33d00

Please sign in to comment.