Bird
Raised Fist0
Selenium Pythontesting~15 mins

GitHub Actions integration in Selenium Python - Build an Automation Script

Choose your learning style10 modes available
Automate login test and run it on GitHub Actions
Preconditions (3)
Step 1: Open the login page at 'https://example.com/login'
Step 2: Enter 'testuser@example.com' in the email input field with id 'email'
Step 3: Enter 'TestPass123!' in the password input field with id 'password'
Step 4: Click the login button with id 'login-button'
Step 5: Wait until the URL changes to 'https://example.com/dashboard'
Step 6: Verify that the page contains an element with id 'welcome-message' that is visible
✅ Expected Result: User is successfully logged in, dashboard page loads, and welcome message is visible
Automation Requirements - Selenium with Python
Assertions Needed:
Assert current URL is 'https://example.com/dashboard'
Assert element with id 'welcome-message' is displayed
Best Practices:
Use explicit waits instead of sleep
Use By.ID locator strategy for elements
Structure test with setup and teardown methods
Use GitHub Actions workflow to run tests on push
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):
        options = webdriver.ChromeOptions()
        options.add_argument('--headless')
        self.driver = webdriver.Chrome(options=options)
        self.driver.implicitly_wait(5)

    def test_login(self):
        driver = self.driver
        driver.get('https://example.com/login')

        email_input = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, 'email'))
        )
        email_input.send_keys('testuser@example.com')

        password_input = driver.find_element(By.ID, 'password')
        password_input.send_keys('TestPass123!')

        login_button = driver.find_element(By.ID, 'login-button')
        login_button.click()

        WebDriverWait(driver, 10).until(
            EC.url_to_be('https://example.com/dashboard')
        )

        self.assertEqual(driver.current_url, 'https://example.com/dashboard')

        welcome_message = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, 'welcome-message'))
        )
        self.assertTrue(welcome_message.is_displayed())

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

if __name__ == '__main__':
    unittest.main()

This test script uses Python's unittest framework and Selenium WebDriver.

In setUp, we start Chrome in headless mode for GitHub Actions compatibility.

The test opens the login page, waits explicitly for the email input to be visible, then enters the email and password.

It clicks the login button and waits until the URL changes to the dashboard URL.

Assertions check that the URL is correct and the welcome message is visible.

Finally, tearDown closes the browser.

This structure allows easy integration with GitHub Actions by running python -m unittest in the workflow.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators instead of stable IDs
Not running browser in headless mode in CI environment
Not quitting the browser after tests
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid).

Show Hint

Practice

(1/5)
1. What is the main purpose of integrating Selenium Python tests with GitHub Actions?
easy
A. To manually trigger tests only when needed
B. To write Selenium tests faster using GitHub's editor
C. To deploy the application to production automatically
D. To automatically run tests on code changes and catch bugs early

Solution

  1. Step 1: Understand GitHub Actions role

    GitHub Actions automates workflows like running tests when code changes.
  2. Step 2: Identify Selenium test integration benefit

    Running Selenium tests automatically helps find bugs early without manual effort.
  3. Final Answer:

    To automatically run tests on code changes and catch bugs early -> Option D
  4. Quick Check:

    Automation for early bug detection = B [OK]
Hint: GitHub Actions runs tests automatically on code changes [OK]
Common Mistakes:
  • Thinking tests run only manually
  • Confusing testing with deployment
  • Assuming GitHub edits test code automatically
2. Which YAML file location is correct for defining GitHub Actions workflows that run Selenium Python tests?
easy
A. .github/workflows/selenium_tests.yml
B. selenium_tests.yml
C. .github/actions/selenium_tests.yml
D. workflows/selenium_tests.yml

Solution

  1. Step 1: Recall GitHub Actions workflow folder

    Workflows must be inside the .github/workflows directory.
  2. Step 2: Match correct file path

    Only .github/workflows/selenium_tests.yml places the YAML file correctly inside .github/workflows.
  3. Final Answer:

    .github/workflows/selenium_tests.yml -> Option A
  4. Quick Check:

    Workflow YAML location = A [OK]
Hint: Workflows go inside .github/workflows folder [OK]
Common Mistakes:
  • Placing workflow files outside .github/workflows
  • Using .github/actions folder incorrectly
  • Missing the .github prefix
3. Given this GitHub Actions YAML snippet, what will happen when code is pushed to the main branch?
name: Selenium Tests
on:
  push:
    branches: [main]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: pip install selenium
      - name: Run tests
        run: python test_suite.py
medium
A. Tests will run on every push to any branch
B. Selenium tests will run automatically on every push to main branch
C. Tests will run only if manually triggered
D. The workflow will fail due to missing Python setup

Solution

  1. Step 1: Analyze trigger event

    The workflow triggers on push events to the main branch only.
  2. Step 2: Check job steps

    Steps include checkout, Python setup, installing selenium, and running tests, so tests run automatically.
  3. Final Answer:

    Selenium tests will run automatically on every push to main branch -> Option B
  4. Quick Check:

    Push to main triggers tests = C [OK]
Hint: Check 'on' section for event triggers in YAML [OK]
Common Mistakes:
  • Assuming tests run on all branches
  • Thinking manual trigger is needed
  • Missing Python setup step
4. You wrote this GitHub Actions workflow to run Selenium tests but it fails with "command not found: python":
name: Selenium Tests
on: [push]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Run tests
        run: python test_suite.py
What is the likely fix?
medium
A. Add a step to set up Python using actions/setup-python
B. Change 'python' to 'python3' in the run command
C. Remove the checkout step
D. Add 'pip install selenium' before running tests

Solution

  1. Step 1: Identify missing Python setup

    The error "command not found: python" means Python is not installed or set up in the runner.
  2. Step 2: Add Python setup step

    Using actions/setup-python installs and configures Python before running tests.
  3. Final Answer:

    Add a step to set up Python using actions/setup-python -> Option A
  4. Quick Check:

    Python setup missing causes error = D [OK]
Hint: Always set up Python before running Python commands [OK]
Common Mistakes:
  • Assuming python3 is available without setup
  • Skipping Python setup step
  • Removing checkout step unnecessarily
5. You want to run Selenium Python tests on multiple Python versions (3.10, 3.11, 3.12) using GitHub Actions. Which YAML snippet correctly sets up this matrix testing?
hard
A. jobs: test: runs-on: ubuntu-latest strategy: matrix: python-version: 3.10, 3.11, 3.12 steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: pip install selenium - name: Run tests run: python test_suite.py
B. jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: [3.10, 3.11, 3.12] - name: Install dependencies run: pip install selenium - name: Run tests run: python test_suite.py
C. jobs: test: runs-on: ubuntu-latest strategy: matrix: python-version: [3.10, 3.11, 3.12] steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: pip install selenium - name: Run tests run: python test_suite.py
D. jobs: test: runs-on: ubuntu-latest strategy: matrix: python-version: [3.10 3.11 3.12] steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: pip install selenium - name: Run tests run: python test_suite.py

Solution

  1. Step 1: Understand matrix syntax

    The matrix must be a YAML list with square brackets and commas: [3.10, 3.11, 3.12].
  2. Step 2: Use matrix variable correctly

    The Python version in setup-python must use ${{ matrix.python-version }} to pick each version.
  3. Final Answer:

    The YAML snippet with strategy.matrix.python-version: [3.10, 3.11, 3.12] -> Option C
  4. Quick Check:

    Matrix list and variable syntax = A [OK]
Hint: Use matrix with brackets and commas, reference with ${{ matrix.var }} [OK]
Common Mistakes:
  • Using commas missing or wrong brackets in matrix
  • Passing list directly to python-version input
  • Not referencing matrix variable in setup-python
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