Running Selenium in CI pipeline in Selenium Python - Build an Automation Script
import os import unittest from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class LoginTest(unittest.TestCase): def setUp(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) self.driver.implicitly_wait(5) self.base_url = os.getenv('BASE_URL', 'https://example.com/login') def test_login(self): driver = self.driver driver.get(self.base_url) email_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'email')) ) email_input.send_keys('admin@test.com') password_input = driver.find_element(By.ID, 'password') password_input.send_keys('Pass123!') login_button = driver.find_element(By.ID, 'login-btn') login_button.click() WebDriverWait(driver, 10).until( EC.url_contains('/dashboard') ) self.assertIn('/dashboard', driver.current_url) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This script uses Python's unittest framework with Selenium WebDriver.
In setUp, we configure Chrome to run headlessly for CI environments and set the base URL from an environment variable for flexibility.
The test_login method opens the login page, waits explicitly for the email input to appear, enters credentials, clicks login, then waits for the URL to contain '/dashboard' to confirm successful login.
Assertions check the URL to verify the test passed.
Finally, tearDown quits the browser to clean up.
This structure ensures the test runs reliably in a CI pipeline without opening a visible browser window.
Now add data-driven testing with 3 different sets of login credentials to verify both successful and unsuccessful login attempts.
Practice
Solution
Step 1: Understand the purpose of CI pipelines
CI pipelines automate testing and building to catch issues early.Step 2: Relate Selenium tests to CI benefits
Running Selenium tests in CI helps find UI bugs automatically before release.Final Answer:
To automatically catch bugs early during development -> Option BQuick Check:
CI + Selenium = Early bug detection [OK]
- Thinking CI slows deployment
- Believing tests run manually in CI
- Ignoring automated testing benefits
Solution
Step 1: Recall Selenium ChromeOptions usage
To run Chrome headless, add the argument '--headless' to options.Step 2: Verify correct syntax
Using options.add_argument('--headless') is the correct method in Selenium Python.Final Answer:
options.add_argument('--headless') -> Option AQuick Check:
Headless Chrome = add_argument('--headless') [OK]
- Using non-existent methods like set_headless
- Trying to set driver.headless directly
- Confusing driver and options methods
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get('https://example.com')
print(driver.title)
driver.quit()Solution
Step 1: Understand headless mode effect
Headless mode allows Chrome to run without a display server in CI.Step 2: Analyze code behavior
The code opens example.com headlessly and prints the page title successfully.Final Answer:
Prints the page title 'Example Domain' -> Option DQuick Check:
Headless mode avoids display errors, prints title [OK]
- Assuming display server is always needed
- Expecting empty output without display
- Confusing driver errors with headless mode
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. How do you fix this?Solution
Step 1: Understand the error message
The error says ChromeDriver is missing or not found in PATH.Step 2: Fix by installing ChromeDriver in CI
Install ChromeDriver and ensure its location is in the PATH environment variable.Final Answer:
Install ChromeDriver and add it to the system PATH in the CI environment -> Option AQuick Check:
Driver missing error = install and add to PATH [OK]
- Ignoring driver installation
- Switching browsers without fixing driver
- Disabling headless mode unrelated to driver error
Solution
Step 1: Identify requirements for Selenium in Docker
Tests need both Chrome browser and ChromeDriver installed inside the container.Step 2: Choose correct Docker setup
A Docker image with Chrome and ChromeDriver pre-installed and running Chrome headless is best practice.Final Answer:
Use a Docker image with Chrome and ChromeDriver installed, run Chrome with '--headless' option in Selenium -> Option CQuick Check:
Docker Selenium needs browser + driver + headless [OK]
- Installing driver without browser
- Running tests outside Docker when container needed
- Using non-headless mode in CI Docker
Ready to prove it?
Pass the test and this pattern moves unknown
