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.