Bird
Raised Fist0
Selenium Pythontesting~15 mins

Jenkins integration in Selenium Python - Build an Automation Script

Choose your learning style10 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

Practice

(1/5)
1. What is the main purpose of integrating Selenium Python tests with Jenkins?
easy
A. To write Selenium test scripts faster
B. To automate running tests and get quick feedback
C. To manually run tests on different browsers
D. To replace Selenium with Jenkins for testing

Solution

  1. Step 1: Understand Jenkins role

    Jenkins automates tasks like running tests without manual effort.
  2. Step 2: Connect Selenium tests with Jenkins

    Integrating Selenium tests with Jenkins allows automatic test execution and quick feedback on code changes.
  3. Final Answer:

    To automate running tests and get quick feedback -> Option B
  4. Quick Check:

    Automation + feedback = A [OK]
Hint: Jenkins automates tasks, so pick automation-related answer [OK]
Common Mistakes:
  • Thinking Jenkins replaces Selenium
  • Assuming Jenkins only helps write tests
  • Confusing manual and automated test runs
2. Which Jenkins Pipeline step correctly runs a Selenium Python test script named test_login.py?
easy
A. run 'test_login.py'
B. execute python test_login.py
C. sh 'python3 test_login.py'
D. bash python3 test_login.py

Solution

  1. Step 1: Identify correct Jenkins syntax

    Jenkins Pipeline uses sh step to run shell commands.
  2. Step 2: Check command correctness

    python3 test_login.py is the correct shell command to run the Python script.
  3. Final Answer:

    sh 'python3 test_login.py' -> Option C
  4. Quick Check:

    Jenkins shell step = sh 'command' [OK]
Hint: Jenkins shell commands use sh 'command' syntax [OK]
Common Mistakes:
  • Using invalid Jenkins step names like run or execute
  • Missing quotes around shell command
  • Confusing bash keyword with Jenkins syntax
3. Given this Jenkins Pipeline snippet, what will be the output if test_login.py fails?
pipeline {
  agent any
  stages {
    stage('Run Tests') {
      steps {
        sh 'python3 test_login.py'
      }
    }
  }
}
medium
A. Jenkins marks the build as failed and stops the pipeline
B. Jenkins ignores the failure and continues the pipeline
C. Jenkins retries the test automatically
D. Jenkins marks the build as successful regardless

Solution

  1. 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.
  2. Step 2: Pipeline stops on failure by default

    Without special handling, Jenkins stops the pipeline on failure and marks build failed.
  3. Final Answer:

    Jenkins marks the build as failed and stops the pipeline -> Option A
  4. Quick Check:

    Shell failure = build fail + stop [OK]
Hint: Shell command failure stops Jenkins pipeline by default [OK]
Common Mistakes:
  • Assuming Jenkins retries tests automatically
  • Thinking Jenkins ignores failures without config
  • Believing build passes despite test failure
4. You added this Jenkins Pipeline step to run Selenium tests:
sh 'python3 -m selenium test_suite.py'

But the build fails with "No module named selenium" error. What is the likely fix?
medium
A. Use sh 'selenium test_suite.py' instead
B. Change command to python3 selenium test_suite.py
C. Remove -m from the command
D. Add a step to install selenium with pip install selenium before running tests

Solution

  1. Step 1: Understand error cause

    "No module named selenium" means Python can't find the selenium package installed.
  2. Step 2: Fix by installing selenium

    Adding pip install selenium before running tests ensures the package is available.
  3. Final Answer:

    Add a step to install selenium with pip install selenium before running tests -> Option D
  4. Quick Check:

    Missing package error = install package first [OK]
Hint: Install missing Python packages before running tests [OK]
Common Mistakes:
  • Changing command syntax without installing package
  • Removing -m which is unrelated here
  • Trying to run selenium as a command
5. You want Jenkins to run Selenium Python tests on multiple browsers (Chrome and Firefox) and report results separately. Which approach is best?
hard
A. Create separate Jenkins stages for each browser running tests with different WebDriver setups
B. Run tests once and assume they work on all browsers
C. Use a single stage and switch browsers randomly during tests
D. Run tests locally and upload results manually to Jenkins

Solution

  1. Step 1: Understand multi-browser testing needs

    Each browser requires its own WebDriver setup and test run to isolate results.
  2. Step 2: Use separate Jenkins stages for clarity

    Separate stages for Chrome and Firefox allow clear reporting and parallel or sequential runs.
  3. Final Answer:

    Create separate Jenkins stages for each browser running tests with different WebDriver setups -> Option A
  4. Quick Check:

    Separate stages = clear multi-browser testing [OK]
Hint: Separate stages for each browser give clear test results [OK]
Common Mistakes:
  • Running tests once for all browsers without setup
  • Switching browsers randomly causing flaky tests
  • Manual result uploads instead of automation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard