In this project I demonstrate how to use Python with Pytest, Selenium and Selenoid or Selenium Grid for automation E2E tests parallelized or distributed.
The tests code is simple,for that reason I don't use PageObject, abstraction and others design patterns, the objective this project is show a configuration of Pytest and Selenoid for execution tests in distributed an parallelized ways.
I suggest using Selenoid instead of Selenium Grid, the selenium grid is heavy and slow, in addition there are other aspects that make the choice of Selenoid click here to see
This project includes:
-
Frameworks:
- Pystest
- Allure
- Selenium 4
-
Features:
- Distributed tests over different browsers
- Parallelized tests
- Screenshot on fail testes
- Visualization the execution of tests with Selenoid UI or Selenium Grid 4
- Record video of tests
- Pytest fixtures and parametrize
- Use docker for infrastructure
- Report with Allure
For these tests I use a Sauce Labs demo app which can be found here: Sauce Demo
- Python >= 3.9 - How install Python
- Pip >= 21.3.x - How install pip
- Docker >= 23.0 - How install Docker
- Allure Cliente >= 2.21 How install allure client
Creat a virtual environment:
$ python -m venv venv
$ source venv/bin/activate
Install dependencies:
$ pip3 install --no-cache-dir -r requirements.txt
$ docker-compose up
$ docker-compose -f docker-compose-selenium-grid.yml up
After started the Selenoid or Selenium Grid service, it is possible to manage and view the test sessions, just access the address in the browser:
Selenoid: http://localhost:8080/#/
Selenium Grid: http://localhost:4444/
$ pytest -vv -q --browser_name="chrome" -q --browser_name="firefox" --alluredir=results/allure_report
$ pytest -vv -q --browser_name="chrome" --alluredir=results/allure_report
$ pytest -vv -q --browser_name="firefox" --alluredir=results/allure_report
$ pytest -vv -q --browser_name="local" --alluredir=results/allure_report
To run more than one test simultaneously, just add the -n parameter informing the maximum number of tests to be run simultaneously, the maximum possible number is limited by the number of threads that the processor has to run the tests. For example to run 2 tests at the same time in chrome and firefox:
$ pytest -n2 -vv -q --browser_name="chrome" -q --browser_name="firefox" --alluredir=results/allure_report
In conftest.py change de value of enableVideo to True, after running the tests, the videos will be stored in the results/video folder:
selenoid_options = {"enableVNC": True,
"enableVideo": True,
"videoName": f'{request.node.nodeid}.mp4',
"videoCodec": "mpeg4",
"enableLog": False}
To add new browsers, just change the browsers.json file, you also need to add the browsers image at the end of the docker-compose.yml file, this page has more information Browsers Configuration File
You must have the allure client installed
Run the command below to generate the test report:
$ allure generate --clean results/allure_report -o results/allure_result
To view the report in the browser:
$ allure open results/allure_result