Framework to build UI test automation in Page Transactions pattern
Project description
Guará
Photo by Mateus Campos Felipe on Unsplash
The scarlet ibis, sometimes called red ibis (Eudocimus ruber), is a species of ibis in the bird family Threskiornithidae. It inhabits tropical South America and part of the Caribbean. In form, it resembles most of the other twenty-seven extant species of ibis, but its remarkably brilliant scarlet coloration makes it unmistakable. It is one of the two national birds of Trinidad and Tobago, and its Tupi–Guarani name, guará, is part of the name of several municipalities along the coast of Brazil.
Contents
- Syntax
- Introduction
- The pattern
- Framework in action
- Installation
- Execution
- Tutorial
- Using other Web Drivers
- Asynchronous execution
- ChatGPT assistance
- Non-testers usage
- Contributing
Syntax
Application.at(apage.DoSomething [,with_parameter=value, ...]).asserts(it.Matches, a_condition)
Introduction
[!IMPORTANT] Guará is the Python implementation of the design pattern
Page Transactions. It is more of a programming pattern than a tool. It can be bound to any web driver other than Selenium. Check the examples here
The intent of this pattern is to simplify UI test automation. It was inspired by Page Objects, App Actions, and Screenplay. Page Transactions focus on the operations (transactions) a user can perform on a web page, such as Login, Logout, or Submit Forms.
The pattern
AbstractTransaction: This is the class from which all transactions inherit. Thedomethod is implemented by each transaction. In this method, calls to WebDriver are placed. If the method returns something, like a string, the automation can use it for assertions.
IAssertion: This is the interface implemented by all assertion classes.- The
assertsmethod of each subclass contains the logic to perform validations. For example, theIsEqualTosubclass compares theresultwith the expected value provided by the tester. - Testers can inherit from this interface to add new sub-classes of validations that the framework does not natively support. More details here.
Application: This is the runner of the automation. It executes thedomethod of each transaction and validates the result using theassertsmethod.- The
assertsmethod receives a reference to anIAssertioninstance. It implements theStrategy Pattern (GoF)to allow its behavior to change at runtime. - Another important component of the
Applicationis theresultproperty. It holds the result of the transaction, which can be used byassertsor inspected by the test using the native built-inassertmethod.
Framework in action
The idea is to group blocks of interactions into classes. These classes inherit from AbstractTransaction and override the do method.
Each transaction is passed to the Application instance, which provides the methods at and asserts. These are the only two methods necessary to orchestrate the automation. While it is primarily bound to Selenium WebDriver, experience shows that it can also be used to test REST APIs, unit tests and can be executed in asynchronous mode (check the examples folder).
When the framework is in action, it follows a highly repetitive pattern. Notice the use of the at method to invoke transactions and the asserts method to apply assertion strategies. Also, the automation is described in plain English improving the comprehension of the code.
from selenium import webdriver
from pages import home, contact, info
from guara.transaction import Application
from guara import it, setup
def test_sample_web_page():
# Instantiates the Application with a driver
app = Application(webdriver.Chrome())
# At setup opens the web application
app.at(setup.OpenApp, url="https://anyhost.com/",)
# At Home page changes the language to Portuguese and asserts its content
app.at(home.ChangeToPortuguese).asserts(it.IsEqualTo, content_in_portuguese)
# Still at Home page changes the language
# to English and uses many assertions to validate the `result`
result = app.at(home.ChangeToEnglish).result
it.IsEqualto().asserts(result, content_in_english)
it.Contains().asserts(result, content_in_english)
# At Info page asserts the text is present
app.at(info.NavigateTo).asserts(
it.Contains, "This project was born"
)
# At setup closes the web application
app.at(setup.CloseApp)
setup.OpenAppandsetup.CloseAppare part of the framework and provide basic implementation to open and close the web application using Selenium Webdriver.itis the module which contains the concrete assertions.
The ugly code which calls the webdriver is like this:
class ChangeToPortuguese(AbstractTransaction):
def __init__(self, driver):
super().__init__(driver)
# Implements the `do` method and returns the `result`
def do(self, **kwargs):
self._driver.find_element(
By.CSS_SELECTOR, ".btn:nth-child(3) > button:nth-child(1) > img"
).click()
self._driver.find_element(By.CSS_SELECTOR, ".col-md-10").click()
return self._driver.find_element(By.CSS_SELECTOR, "label:nth-child(1)").text
Again, it is a very repetitive activity:
- Create a class representing the transaction, in this case, the transaction changes the language to Portuguese
- Inherits from
AbstractTransaction - Implements the
domethod- Optional: Returns the result of the transaction
Read more in Tutorial
Installation
Dependencies
- Python 3.11
- Selenium
This framework can be installed by
pip install guara
Execution
It is recommended to use pytest
# Executes reporting the complete log
python -m pytest -o log_cli=1 --log-cli-level=INFO --log-format="%(asctime)s %(levelname)s %(message)s" --log-date-format="%Y-%m-%d %H:%M:%S"
[!TIP] These options can also be customized through your
pytest.inifile. Refer to Pytest documentaion.
Outputs
examples/web_ui/selenium/simple/test_local_page.py::TestLocalTransaction::test_local_page
--------------------------------------------------------------- live log setup ---------------------------------------------------------------
2025-01-09 06:39:41 INFO Transaction 'OpenApp'
2025-01-09 06:39:41 INFO url: file:////...sample.html
2025-01-09 06:39:41 INFO window_width: 1094
2025-01-09 06:39:41 INFO window_height: 765
2025-01-09 06:39:41 INFO implicitly_wait: 0.5
2025-01-09 06:39:41 INFO Assertion 'IsEqualTo'
2025-01-09 06:39:41 INFO actual: 'Sample page'
2025-01-09 06:39:41 INFO expected: 'Sample page'
--------------------------------------------------------------- live log call ----------------------------------------------------------------
2025-01-09 06:39:41 INFO Transaction 'SubmitText'
2025-01-09 06:39:41 INFO text: cheese
2025-01-09 06:39:41 INFO Assertion 'IsEqualTo'
2025-01-09 06:39:41 INFO actual: 'It works! cheese!'
2025-01-09 06:39:41 INFO expected: 'It works! cheese!'
2025-01-09 06:39:41 INFO Transaction 'SubmitText'
2025-01-09 06:39:41 INFO text: cheese
2025-01-09 06:39:41 INFO Assertion 'IsNotEqualTo'
2025-01-09 06:39:41 INFO actual: 'It works! cheesecheese!'
2025-01-09 06:39:41 INFO expected: 'Any'
PASSED [100%]
------------------------------------------------------------- live log teardown --------------------------------------------------------------
2025-01-09 06:39:41 INFO Transaction 'CloseApp'
It also works well with other test frameworks. Check more details here
Tutorial
Read the step-by-step to build your first automation with this framework.
Using other Web Drivers
It is possible to run Guara using other Web Drivers like Caqui and Playwright. Check the requirements of each Web Driver before execute it. For example, Playwright requires the installation of browsers separately.
Asynchronous execution
The core code was extended to allow asynchronous executions. Get more details here
ChatGPT assistance
It is possible to use ChatGPT to help you organize your code in Page Transactions pattern. Check these simple steps.
Non-Testers Usage
Page Transactions is primarily based on the Command Pattern (GoF), making it suitable for product development as well, even though that is not its primary intent. This section is dedicated to showcasing other uses of the framework that are unrelated to automation testing.
Prototyping
Software engineers, UX designers with some knowledge of programming, and software students can leverage this project to build simple applications that are testable by default. For example, To-Do List web application was built with Guara and PyScript.
How you can help?
Here's how you can help with this:
- Star this project on GitHub.
- Tell your friends and colleagues about it.
- Share it on social media.
- Write a blog post about Guara.
- Take a look at the
good first issuehere, assign any to you and push the code.
Contributing
Read the Code of Conduct before push new Merge Requests.
Now, follow the steps in Contributing session.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file guara-0.0.9.tar.gz.
File metadata
- Download URL: guara-0.0.9.tar.gz
- Upload date:
- Size: 450.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0248c5b6217e95eb9c2e0ee3dc96201ce121fd960b5e94b1387beb35ee2b0914
|
|
| MD5 |
898081de38eedc3e918122d6f047de52
|
|
| BLAKE2b-256 |
03fbf5adb64663c4033f90b436e9f52eafbdd041853bba0c628df80f9a019835
|
File details
Details for the file guara-0.0.9-py3-none-any.whl.
File metadata
- Download URL: guara-0.0.9-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbb66af88f20f2b2df897a90b1f6efc7b37cbee5cfb0d17aca33fc55cfa7a975
|
|
| MD5 |
fe77553a87bbed5fb854efa992117221
|
|
| BLAKE2b-256 |
8467f72251a21577f6a9207582bc8469d30cf58895a6a28c2ab441181d02d6dd
|