0
0
Selenium Pythontesting~15 mins

Jenkins integration in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Automate login and verify dashboard URL with Jenkins integration
Preconditions (3)
Step 1: Open the browser using Selenium WebDriver
Step 2: Navigate to the login page URL 'https://example.com/login'
Step 3: Enter 'admin@test.com' in the email input field with id 'email'
Step 4: Enter 'Pass123!' in the password input field with id 'password'
Step 5: Click the login button with id 'loginBtn'
Step 6: Wait until the URL changes to the dashboard URL 'https://example.com/dashboard'
Step 7: Verify that the current URL is exactly 'https://example.com/dashboard'
✅ Expected Result: After login, the browser navigates to the dashboard URL and the test passes confirming successful login.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the current URL after login matches the expected dashboard URL
Best Practices:
Use explicit waits to wait for URL change
Use By.ID locator strategy for stable element selection
Structure code for Jenkins integration (e.g., no GUI blocking, proper teardown)
Include setup and teardown methods for WebDriver
Use clear and descriptive assertion messages
Automated Solution
Selenium Python
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.

Common Mistakes - 4 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'It causes flaky tests and unnecessary delays because it waits fixed time regardless of page state.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait only as long as needed."}
Using brittle XPath locators instead of stable IDs
Not closing the browser after test
Hardcoding URLs without verifying navigation
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials to verify login success or failure.

Show Hint