Skip to content

Commit d62b8bd

Browse files
chore: enhance Python Selenium CI workflow with Xvfb setup and improved screenshot handling
1 parent 4d10fff commit d62b8bd

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

.github/workflows/python-selenium-ci-workflow.yaml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@ jobs:
4343
APP_URL: http://localhost:3000
4444
DISPLAY: :99
4545

46-
services:
47-
xvfb:
48-
image: xvfb-run
49-
options: --entrypoint /usr/bin/Xvfb -- :99 -screen 0 1280x1024x24 -ac
50-
5146
steps:
5247
- name: Checkout code
5348
uses: actions/checkout@v3
5449

50+
- name: Setup Xvfb
51+
run: |
52+
sudo apt-get update
53+
sudo apt-get install -y xvfb
54+
Xvfb :99 -screen 0 1280x1024x24 > /dev/null 2>&1 &
55+
echo "DISPLAY=:99" >> $GITHUB_ENV
56+
5557
- name: Set up Node.js
5658
uses: actions/setup-node@v3
5759
with:
@@ -100,8 +102,14 @@ jobs:
100102
101103
- name: Install Python dependencies
102104
run: |
105+
# Check if tests directory exists, if not create it
106+
mkdir -p tests
103107
cd tests
104108
python -m pip install --upgrade pip
109+
# Check if requirements.txt exists, if not create one
110+
if [ ! -f requirements.txt ]; then
111+
echo "pytest\nselenium\nwebdriver-manager" > requirements.txt
112+
fi
105113
pip install -r requirements.txt
106114
107115
- name: Run Selenium tests
@@ -117,4 +125,4 @@ jobs:
117125
with:
118126
name: test-results
119127
path: tests/screenshots/
120-
retention-days: 7
128+
retention-days: 5

tests/conftest.py

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
1-
# General imports
21
import pytest
2+
import os
33
from selenium import webdriver
4-
5-
# Imports to get chrome driver working
4+
from selenium.webdriver.chrome.options import Options
65
from selenium.webdriver.chrome.service import Service
76
from webdriver_manager.chrome import ChromeDriverManager
87

9-
# Imports to get firefox driver working
10-
from selenium.webdriver.firefox.service import Service as FirefoxService
11-
from webdriver_manager.firefox import GeckoDriverManager
12-
13-
# Import options for headless mode
14-
from selenium.webdriver.chrome.options import Options
15-
16-
17-
def pytest_addoption(parser):
18-
parser.addoption(
19-
"--browser", action="store", default="chrome", help="Send 'chrome' or 'firefox' as parameter for execution"
8+
@pytest.fixture(scope="function")
9+
def driver():
10+
# Setup Chrome options
11+
chrome_options = Options()
12+
13+
# Check if running in GitHub Actions
14+
if os.environ.get('GITHUB_ACTIONS') == 'true':
15+
chrome_options.add_argument('--headless')
16+
chrome_options.add_argument('--no-sandbox')
17+
chrome_options.add_argument('--disable-dev-shm-usage')
18+
19+
chrome_options.add_argument('--window-size=1920,1080')
20+
21+
# Initialize Chrome driver
22+
driver = webdriver.Chrome(
23+
service=Service(ChromeDriverManager().install()),
24+
options=chrome_options
2025
)
21-
22-
23-
@pytest.fixture()
24-
def driver(request):
25-
browser = request.config.getoption("--browser")
26-
# Default driver value
27-
driver = ""
28-
# Option setup to run in headless mode (in order to run this in GH Actions)
29-
options = Options()
30-
options.add_argument('--headless')
31-
# Setup
32-
print(f"\nSetting up: {browser} driver")
33-
if browser == "chrome":
34-
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
35-
elif browser == "firefox":
36-
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
37-
# Implicit wait setup for our framework
38-
driver.implicitly_wait(10)
26+
27+
# Create screenshots directory if it doesn't exist
28+
os.makedirs("screenshots", exist_ok=True)
29+
3930
yield driver
40-
# Tear down
41-
print(f"\nTear down: {browser} driver")
31+
32+
# Take screenshot on test failure
33+
if pytest.item and hasattr(pytest, '_funcargs'):
34+
test_name = pytest._funcargs.get('request', pytest._funcargs.get('fspath', pytest)).node.name
35+
driver.save_screenshot(f"screenshots/{test_name}.png")
36+
4237
driver.quit()

0 commit comments

Comments
 (0)