Introduction
In web automation, the base URL is the foundation of every web interaction. It points your tests to the right environment (dev, staging, production).
But static URLs, coded directly into tests, become brittle upon deployment changes, environment shifts, and URL updates, leading to your tests breaking and unnecessary code maintenance.
In this article, I will explain how to use dynamic base URLs with Selenium and Playwright Python using GitHub actions.
What are GitHub Actions?
according to this article:
GitHub Actions is an API for cause and effect on GitHub: orchestrate any workflow, based on any event, while GitHub manages the execution, provides rich feedback, and secures every step along the way. With GitHub Actions, workflows and steps are just code in a repository, so you can create, share, reuse, and fork your software development practices.
GitHub actions are free and support public repositories. The article will be demonstrated using my public repository, which contains a suite of E2E tests written in Python.
Implementing the Solution
We are working with pytest
which is a popular testing framework for Python.
And using the pytest-base-url plugin.
We define the base URL in pyproject.toml
under the section [tool.pytest.ini_options]
which is used to configure pytest options.
Selenium
Here’s a breakdown of the pytest Selenium code using the base URL plugin:
1. Fixture Hook:
- This defines a fixture function named .It’s a special hook function called automatically by pytest before each test is run. It receives an object, representing the test item currently being executed.
2. Retrieving Base URL:
- This line fetches the value of the option from the pytest configuration. The option is using pyproject.tomle base URL configuration.
3. Navigating to Base URL:
- This line instructs the Selenium WebDriver (assuming it’s already initialized and assigned to the variable ) to navigate to the retrieved .This effectively loads the web page under test before each test case execution.
link to the project - with complete code examples.
Playwright
The plugin injects the base URL value into the test environment or fixtures. Now use the plugin-provided base URL, ensuring navigation to the appropriate environment.
link to the project - with complete code examples.
GitHub Actions
We define a repository variable called BASE_URL in https://github.com/{owner}/{repo}/settings/variables/actions
: This represents the username that owns the GitHub repository where you’re defining the variable.
: This refers to the name of the repository itself.
the usage in GitHub actions workflow:
The provided command triggers tests within our Selenium and GitHub Actions workflows using pytest. Here’s a breakdown:
: This option leverages a dynamic base URL, stored in the workflow’s variable named. The syntax retrieves the variable value and injects it into the command.
we can alter the base URL in the GitHub UI - or we can use the GitHub API
this:
Getting the BASE_URL programmatically - example in Postman:
Updating the BASE_URL programmatically - example in Postman:
In Conclusion
We’ve demystified the concept of base URLs, their crucial role in test automation, and how to effectively manage them. We explored their usage in Selenium and Playwright Python, leveraging the plugin for streamlined configuration.
Furthermore, we delved into integrating dynamic base URLs within GitHub Actions workflows. By storing them as repository variables, you gain centralized control and enable environment-specific testing (dev/staging) through both the UI and the powerful GitHub API.
Remember, dynamic base URLs empower flexible, maintainable, and reliable test automation – a cornerstone of software quality. Embrace their potential and elevate your testing practices!
Further reading on GitHub action features can be found in this link.
Happy testing!