Automate a login test with Selenium, BrowserStack and GitHub Actions
A hands-on tutorial that takes a tester from a local Selenium script to cross-browser runs on BrowserStack, then to automatic runs on every push with GitHub Actions.
Manual login testing is slow and easy to get wrong. This tutorial automates it with Selenium, runs it on real browsers in the cloud with BrowserStack, and triggers it on every push with GitHub Actions.
We test the login page of The Internet, a public site built for automation practice. Its test credentials are shown on the page itself.
What you need
- Python 3.9+
- Google Chrome
- An editor such as VS Code
- A BrowserStack account for the cloud part (a free trial works)
No driver download needed. Since Selenium 4.6, Selenium Manager downloads the right ChromeDriver automatically.
1. Install Selenium
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install selenium
pip show selenium # confirm the installed version
2. The test steps
A person testing the login would:
- Open the login page.
- Enter the username and password.
- Click Login.
- Check that the "You logged into a secure area!" message appears.
We automate exactly those steps.
3. Write the script
Create test_login.py:
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
URL = "https://the-internet.herokuapp.com/login"
USERNAME = "tomsmith" # public demo credentials from the page
PASSWORD = "SuperSecretPassword!"
def run(driver):
wait = WebDriverWait(driver, 10)
driver.get(URL)
wait.until(EC.visibility_of_element_located((By.ID, "username"))).send_keys(USERNAME)
driver.find_element(By.ID, "password").send_keys(PASSWORD)
driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
flash = wait.until(EC.visibility_of_element_located((By.ID, "flash")))
assert "You logged into a secure area!" in flash.text, flash.text
print("PASS: login works")
if __name__ == "__main__":
driver = webdriver.Chrome()
try:
run(driver)
finally:
driver.quit()
Run it:
python test_login.py
Chrome opens, logs in, and the terminal prints PASS: login works.
4. How the script works
| Line | What it does |
|---|---|
webdriver.Chrome() |
Starts Chrome. Selenium Manager finds or downloads the matching driver. |
driver.get(URL) |
Opens the login page. |
WebDriverWait(driver, 10) |
Waits up to 10 seconds for elements, instead of using fixed sleep() calls that make tests slow and flaky. |
find_element(By.ID, ...) |
Finds the username and password fields by their id. |
By.CSS_SELECTOR |
Finds the Login button by its type. |
assert ... |
Fails the test if the success message is missing. |
driver.quit() |
Always closes the browser, even if the test fails. |
Tip: prefer stable locators (
id,data-testattributes) over long XPaths that break when the page layout changes.
5. Run it on BrowserStack
BrowserStack Automate runs the same test on real browsers and operating systems in the cloud, which is great for cross-browser coverage.
Set your credentials as environment variables. Never hard-code them:
export BROWSERSTACK_USERNAME="your_username"
export BROWSERSTACK_ACCESS_KEY="your_access_key"
Create test_login_browserstack.py:
import os
from selenium import webdriver
from test_login import run
options = webdriver.ChromeOptions()
options.set_capability("bstack:options", {
"os": "Windows",
"osVersion": "11",
"browserVersion": "latest",
"userName": os.environ["BROWSERSTACK_USERNAME"],
"accessKey": os.environ["BROWSERSTACK_ACCESS_KEY"],
"sessionName": "Login test",
"buildName": "Login suite",
"debug": True, # visual logs (screenshots)
"video": True, # set False to speed up runs
})
driver = webdriver.Remote(
command_executor="https://hub.browserstack.com/wd/hub",
options=options,
)
try:
run(driver)
finally:
driver.quit() # always quit, or the session runs until it times out
Read the results
Open the Automate dashboard. Each run is a session with a unique ID and:
- Status: Running, Completed (green), Error, or Timeout (yellow) if the script never called
quit(). - Text logs: every command and how long it took.
- Visual logs: screenshots of each step (
debug: True). - Video: a recording of the whole session (
video: True), which you can watch, download and share.
Video recording makes runs a little slower. Turn it off once a test is stable.
To test other browsers, change os, osVersion and the options class (for example webdriver.FirefoxOptions()). BrowserStack's capability generator writes these settings for you.
6. Run it automatically with GitHub Actions
Add your credentials as repository secrets (Settings → Secrets and variables → Actions): BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY.
Then create .github/workflows/browserstack.yml:
name: BrowserStack login test
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install selenium
- name: Set up BrowserStack environment
uses: browserstack/github-actions/setup-env@master
with:
username: ${{ secrets.BROWSERSTACK_USERNAME }}
access-key: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
- name: Run the test
run: python test_login_browserstack.py
Now every push and pull request runs the login test on BrowserStack, and the result shows up on the commit.
Testing an app that only runs inside the workflow (for example on
localhost)? Add BrowserStack'ssetup-localaction to open a secure tunnel before the test, and call it again withlocal-testing: stopafterwards. See BrowserStack's GitHub Actions guide.
Troubleshooting
| Problem | Fix |
|---|---|
NoSuchElementException |
The element wasn't there yet. Use WebDriverWait, and check the locator in DevTools. |
| Chrome version mismatch | Update Selenium (pip install -U selenium). Selenium Manager will fetch the matching driver. |
BrowserStack 401 Unauthorized |
Check the username and access key environment variables. |
| Session ends with Timeout | Make sure driver.quit() runs in a finally block. |
Further reading
Want documentation like this for your product?