0
0
Selenium Pythontesting~15 mins

GitHub Actions integration in Selenium Python - Build an Automation Script

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