0
0
Selenium Pythontesting~10 mins

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

Choose your learning style9 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.