Bird
Raised Fist0
Selenium Pythontesting~10 mins

Running Selenium in CI pipeline in Selenium Python - Test Execution Trace

Choose your learning style10 modes available
Test Overview

This test runs a simple Selenium script in a Continuous Integration (CI) pipeline. It opens a browser, navigates to a website, finds a search box, enters a query, submits it, and verifies the results page loaded correctly.

Test Code - unittest
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class TestSearchInCIPipeline(unittest.TestCase):
    def setUp(self):
        chrome_options = Options()
        chrome_options.add_argument('--headless')  # Run in headless mode for CI
        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(10)

    def test_search(self):
        self.driver.get('https://www.example.com')
        search_box = self.driver.find_element(By.NAME, 'q')
        search_box.send_keys('selenium testing')
        search_box.send_keys(Keys.RETURN)

        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.ID, 'results'))
        )

        results = self.driver.find_element(By.ID, 'results')
        self.assertTrue(results.is_displayed())

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser is launched in headless mode with CI optionsBrowser runs without UI, ready to navigate—PASS
2Browser navigates to https://www.example.comExample.com homepage is loaded—PASS
3Finds search input box by name 'q'Search box element is located on the pageElement with name 'q' is presentPASS
4Types 'selenium testing' into search box and presses EnterSearch query submitted, page starts loading results—PASS
5Waits up to 10 seconds for element with ID 'results' to appearResults section is loaded and visiblePresence of element with ID 'results'PASS
6Checks if the results element is displayedResults section is visible on the pageresults.is_displayed() returns TruePASS
7Browser quits and test endsBrowser closed, resources freed—PASS
Failure Scenario
Failing Condition: Search results element with ID 'results' does not appear within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
Why is the Chrome browser run in headless mode in this test?
ATo run the browser without opening a visible window in the CI pipeline
BTo enable debugging with a visible browser window
CTo slow down the test execution for stability
DTo disable JavaScript on the page
Key Result
Running Selenium tests in headless mode with proper waits ensures smooth automated testing in CI pipelines without UI interruptions.

Practice

(1/5)
1. Why is it important to run Selenium tests in a CI pipeline?
easy
A. To slow down the deployment process
B. To automatically catch bugs early during development
C. To manually check the UI after deployment
D. To avoid writing test scripts

Solution

  1. Step 1: Understand the purpose of CI pipelines

    CI pipelines automate testing and building to catch issues early.
  2. Step 2: Relate Selenium tests to CI benefits

    Running Selenium tests in CI helps find UI bugs automatically before release.
  3. Final Answer:

    To automatically catch bugs early during development -> Option B
  4. Quick Check:

    CI + Selenium = Early bug detection [OK]
Hint: CI runs tests automatically to catch bugs early [OK]
Common Mistakes:
  • Thinking CI slows deployment
  • Believing tests run manually in CI
  • Ignoring automated testing benefits
2. Which of the following is the correct way to run Chrome in headless mode in Selenium Python?
easy
A. options.add_argument('--headless')
B. options.set_headless(True)
C. driver.headless = True
D. driver.set_option('headless')

Solution

  1. Step 1: Recall Selenium ChromeOptions usage

    To run Chrome headless, add the argument '--headless' to options.
  2. Step 2: Verify correct syntax

    Using options.add_argument('--headless') is the correct method in Selenium Python.
  3. Final Answer:

    options.add_argument('--headless') -> Option A
  4. Quick Check:

    Headless Chrome = add_argument('--headless') [OK]
Hint: Use add_argument('--headless') to enable headless mode [OK]
Common Mistakes:
  • Using non-existent methods like set_headless
  • Trying to set driver.headless directly
  • Confusing driver and options methods
3. What will be the output when running this Selenium Python snippet in a CI environment without a display server?
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()
medium
A. Prints 'ChromeDriver not found' error
B. Throws an error about missing display server
C. Prints an empty string
D. Prints the page title 'Example Domain'

Solution

  1. Step 1: Understand headless mode effect

    Headless mode allows Chrome to run without a display server in CI.
  2. Step 2: Analyze code behavior

    The code opens example.com headlessly and prints the page title successfully.
  3. Final Answer:

    Prints the page title 'Example Domain' -> Option D
  4. Quick Check:

    Headless mode avoids display errors, prints title [OK]
Hint: Headless mode avoids display errors, so title prints [OK]
Common Mistakes:
  • Assuming display server is always needed
  • Expecting empty output without display
  • Confusing driver errors with headless mode
4. You added Selenium tests to your CI pipeline but they fail with the error: selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. How do you fix this?
medium
A. Install ChromeDriver and add it to the system PATH in the CI environment
B. Remove the ChromeDriver and use Firefox instead
C. Run tests without specifying any driver
D. Disable headless mode in Chrome options

Solution

  1. Step 1: Understand the error message

    The error says ChromeDriver is missing or not found in PATH.
  2. Step 2: Fix by installing ChromeDriver in CI

    Install ChromeDriver and ensure its location is in the PATH environment variable.
  3. Final Answer:

    Install ChromeDriver and add it to the system PATH in the CI environment -> Option A
  4. Quick Check:

    Driver missing error = install and add to PATH [OK]
Hint: Add ChromeDriver to PATH in CI to fix driver not found [OK]
Common Mistakes:
  • Ignoring driver installation
  • Switching browsers without fixing driver
  • Disabling headless mode unrelated to driver error
5. You want to run Selenium tests in a CI pipeline using Docker. Which setup ensures tests run headlessly and ChromeDriver is available inside the container?
hard
A. Run Selenium tests on host machine, not inside Docker
B. Use a plain Python image and install ChromeDriver only, no Chrome needed
C. Use a Docker image with Chrome and ChromeDriver installed, run Chrome with '--headless' option in Selenium
D. Use Firefox without headless mode inside Docker container

Solution

  1. Step 1: Identify requirements for Selenium in Docker

    Tests need both Chrome browser and ChromeDriver installed inside the container.
  2. Step 2: Choose correct Docker setup

    A Docker image with Chrome and ChromeDriver pre-installed and running Chrome headless is best practice.
  3. Final Answer:

    Use a Docker image with Chrome and ChromeDriver installed, run Chrome with '--headless' option in Selenium -> Option C
  4. Quick Check:

    Docker Selenium needs browser + driver + headless [OK]
Hint: Docker needs both Chrome and ChromeDriver installed [OK]
Common Mistakes:
  • Installing driver without browser
  • Running tests outside Docker when container needed
  • Using non-headless mode in CI Docker
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