-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added service worker registration to HTML files
fixed typo in API function name modified schedule data in generateTable.js added GitHub Actions to generate screenshots for manifest.json Updated docker GitHub Actions workflow to wait for image generation action to complete
- Loading branch information
Showing
10 changed files
with
298 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
name: Run Headless Browser Tests | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
services: | ||
chrome: | ||
image: selenium/standalone-chrome | ||
options: >- | ||
--shm-size 2g | ||
ports: | ||
- 4444:4444 | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.8' # Specify your desired Python version | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r /devtools/screenshotsrequirements.txt | ||
- name: Start web server | ||
run: | | ||
python -m http.server 5500 & # Start the HTTP server in the background | ||
- name: Wait for server to start | ||
run: sleep 5 # Wait for a few seconds to ensure the server is up | ||
- name: Run tests | ||
run: | | ||
cd ./devtools/screenshots | ||
python main.py | ||
- name: Add and commit screenshots | ||
run: | | ||
git config --local user.email "action@github.com" | ||
git config --local user.name "GitHub Action" | ||
git add public/screenshots/* | ||
git add manifest.json | ||
git commit -m "Add screenshots from headless browser tests" || echo "No changes to commit" | ||
git push | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Use the GitHub token for authentication |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import time | ||
import os | ||
import json | ||
from selenium import webdriver | ||
from selenium.webdriver.chrome.options import Options | ||
from selenium.webdriver.common.by import By | ||
from PIL import Image | ||
screenshots_dir = '../../public/screenshots' | ||
manifest_path = '../../manifest.json' | ||
screenshots_uri = '/public/screenshots/' | ||
# List of websites with optional interactions | ||
websites = [ | ||
{ | ||
"url": "https://localhost:5500/login.html", | ||
"name": "login", | ||
}, | ||
{ | ||
"url": "https://localhost:5500/onboarding.html", | ||
"name": "getting-started", | ||
}, | ||
{ | ||
"url": "https://localhost:5500/", | ||
"name": "example", | ||
}, | ||
{ | ||
"url": "https://localhost:5500/", | ||
"name": "example2", | ||
"interactions": lambda driver: interact_example(driver) | ||
} | ||
# Add more websites with specific interactions here | ||
] | ||
|
||
def interact_example(driver): | ||
"""Example interaction: Click a button, wait for element.""" | ||
try: | ||
time.sleep(1) | ||
driver.find_element(By.ID, "m1").click() | ||
time.sleep(1) | ||
except Exception as e: | ||
print(f"Interaction failed: {e}") | ||
|
||
|
||
def interact_example2(driver): | ||
"""Example interaction: Scroll to bottom.""" | ||
try: | ||
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") | ||
time.sleep(2) # Wait for scroll to complete | ||
except Exception as e: | ||
print(f"Interaction failed: {e}") | ||
|
||
|
||
|
||
manifest = {"screenshots": []} | ||
|
||
# Ensure the screenshots directory exists | ||
if not os.path.exists(screenshots_dir): | ||
os.makedirs(screenshots_dir) | ||
|
||
# Load existing manifest.json if it exists | ||
if os.path.exists(manifest_path): | ||
with open(manifest_path, 'r') as f: | ||
manifest = json.load(f) | ||
|
||
# Set up Chrome options for Selenium | ||
chrome_options = Options() | ||
# chrome_options.add_argument('--headless') # Run in headless mode | ||
chrome_options.add_argument('--disable-gpu') | ||
chrome_options.add_argument('--window-size=1280x800') | ||
|
||
# Initialize WebDriver | ||
driver = webdriver.Chrome(options=chrome_options) | ||
if not screenshots_uri.endswith('/'): | ||
screenshots_uri += '/' | ||
if not screenshots_dir.endswith('/'): | ||
screenshots_dir += '/' | ||
|
||
def capture_screenshot(site, viewport_size, form_factor): | ||
"""Capture screenshot for the given viewport size.""" | ||
driver.set_window_rect(0,0,*viewport_size) | ||
screenshot_path = f"{screenshots_dir}{site['name']}-{form_factor}.png" | ||
driver.save_screenshot(screenshot_path) | ||
|
||
# Use PIL to open the screenshot and get its size | ||
with Image.open(screenshot_path) as img: | ||
actual_width, actual_height = img.size | ||
# Check the aspect ratio condition | ||
if form_factor == "wide" and actual_width > 2.3 * actual_height: | ||
print(f"Error: Screenshot width ({actual_width}px) exceeds 2.3 times the height ({actual_height}px).") | ||
print(f"Captured screenshot for {site['name']} - {form_factor}: {actual_width}x{actual_height}") # Print actual sizes | ||
|
||
# Check if the screenshot src already exists in the manifest | ||
existing_entry = next((entry for entry in manifest["screenshots"] if entry["src"] == f"{screenshots_uri}{site['name']}-{form_factor}.png"), None) | ||
|
||
if existing_entry: | ||
# Update the existing entry | ||
existing_entry["sizes"] = f"{actual_width}x{actual_height}" # Update sizes | ||
existing_entry["type"] = "image/png" # Ensure type is set | ||
existing_entry["form_factor"] = form_factor # Update form factor | ||
else: | ||
# Add a new entry to the manifest | ||
manifest["screenshots"].append({ | ||
"src": f"{screenshots_uri}{site['name']}-{form_factor}.png", | ||
"sizes": f"{actual_width}x{actual_height}", # Use actual dimensions | ||
"type": "image/png", | ||
"form_factor": form_factor | ||
}) | ||
|
||
for site in websites: | ||
try: | ||
# Visit the website | ||
driver.get(site["url"]) | ||
|
||
# Perform any interactions defined for this website | ||
if "interactions" in site and callable(site["interactions"]): | ||
site["interactions"](driver) | ||
|
||
# Capture wide (desktop) screenshot | ||
capture_screenshot(site, (1000, 654), "wide") # Example width | ||
|
||
# Capture narrow (mobile) screenshot | ||
capture_screenshot(site, (469, 505), "narrow") # Maintain existing size or adjust slightly if necessary | ||
|
||
|
||
|
||
print(f"Captured screenshots for {site['url']}") | ||
|
||
except Exception as e: | ||
print(f"Failed to capture {site['url']}: {e}") | ||
|
||
# Close the WebDriver | ||
driver.quit() | ||
|
||
# Write the updated manifest.json file back | ||
with open(manifest_path, 'w') as f: | ||
json.dump(manifest, f, indent=2) | ||
|
||
print("Manifest updated at ../../manifest.json.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
selenium>=4.0.0 | ||
Pillow>=9.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters