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.