Jenkins integration in Selenium Python - Build an Automation Script
import unittest from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class LoginTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(5) def test_login_dashboard_url(self): driver = self.driver driver.get('https://example.com/login') email_input = driver.find_element(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, 'loginBtn') login_button.click() wait = WebDriverWait(driver, 10) wait.until(EC.url_to_be('https://example.com/dashboard')) current_url = driver.current_url self.assertEqual(current_url, 'https://example.com/dashboard', f'Expected URL to be https://example.com/dashboard but got {current_url}') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Python's unittest framework with Selenium WebDriver.
setUp() initializes the Chrome browser before each test.
The test test_login_dashboard_url opens the login page, enters the given email and password, clicks the login button, then waits explicitly for the URL to become the dashboard URL.
The assertion checks that the current URL matches the expected dashboard URL, with a clear message if it fails.
tearDown() closes the browser after the test to clean up resources.
This structure supports Jenkins integration by allowing Jenkins to run the test script headlessly or with a display, and report pass/fail results based on the assertion.
Now add data-driven testing with 3 different sets of login credentials to verify login success or failure.
Practice
Solution
Step 1: Understand Jenkins role
Jenkins automates tasks like running tests without manual effort.Step 2: Connect Selenium tests with Jenkins
Integrating Selenium tests with Jenkins allows automatic test execution and quick feedback on code changes.Final Answer:
To automate running tests and get quick feedback -> Option BQuick Check:
Automation + feedback = A [OK]
- Thinking Jenkins replaces Selenium
- Assuming Jenkins only helps write tests
- Confusing manual and automated test runs
test_login.py?Solution
Step 1: Identify correct Jenkins syntax
Jenkins Pipeline usesshstep to run shell commands.Step 2: Check command correctness
python3 test_login.pyis the correct shell command to run the Python script.Final Answer:
sh 'python3 test_login.py' -> Option CQuick Check:
Jenkins shell step = sh 'command' [OK]
- Using invalid Jenkins step names like run or execute
- Missing quotes around shell command
- Confusing bash keyword with Jenkins syntax
test_login.py fails?pipeline {
agent any
stages {
stage('Run Tests') {
steps {
sh 'python3 test_login.py'
}
}
}
}Solution
Step 1: Understand Jenkins shell step behavior
If the shell command exits with a non-zero code (test fails), Jenkins marks the build as failed.Step 2: Pipeline stops on failure by default
Without special handling, Jenkins stops the pipeline on failure and marks build failed.Final Answer:
Jenkins marks the build as failed and stops the pipeline -> Option AQuick Check:
Shell failure = build fail + stop [OK]
- Assuming Jenkins retries tests automatically
- Thinking Jenkins ignores failures without config
- Believing build passes despite test failure
sh 'python3 -m selenium test_suite.py'
But the build fails with "No module named selenium" error. What is the likely fix?
Solution
Step 1: Understand error cause
"No module named selenium" means Python can't find the selenium package installed.Step 2: Fix by installing selenium
Addingpip install seleniumbefore running tests ensures the package is available.Final Answer:
Add a step to install selenium with pip install selenium before running tests -> Option DQuick Check:
Missing package error = install package first [OK]
- Changing command syntax without installing package
- Removing -m which is unrelated here
- Trying to run selenium as a command
Solution
Step 1: Understand multi-browser testing needs
Each browser requires its own WebDriver setup and test run to isolate results.Step 2: Use separate Jenkins stages for clarity
Separate stages for Chrome and Firefox allow clear reporting and parallel or sequential runs.Final Answer:
Create separate Jenkins stages for each browser running tests with different WebDriver setups -> Option AQuick Check:
Separate stages = clear multi-browser testing [OK]
- Running tests once for all browsers without setup
- Switching browsers randomly causing flaky tests
- Manual result uploads instead of automation
Ready to prove it?
Pass the test and this pattern moves unknown
