5. E2E
This time we focus on E2E (End-to-End), the highest-level testing in web development. We no longer touch the code, but its results in the browser.
The goal is to emulate the visitor's experience, generating a virtual mouse and keyboard that navigate through every corner. We check whether certain HTML tags are visible, whether pressing a button does what it should, whether the correct messages appear, whether the form is submitted. Everything is automated to have a final layer where, without looking inside, we check that all the pieces work in harmony. If some step isn't the expected one, a report is generated with what happened.
:quality(85)/https://andros.dev/static/img/courses/testing/e2e.jpg)
Many E2E frameworks were born in the JavaScript world, but from Python we have a first-class option: Playwright. It controls Chromium, Firefox and WebKit, integrates with pytest and is quick to get running.
{: .advice } Another popular tool is Selenium. We don't use it here because its installation is more cumbersome (it requires a native driver and extra steps) and it wasn't originally designed for such a convenient testing layer.
Install
We install Playwright and its pytest integration, and download the browsers:
pip install pytest-playwright
playwright install
Our first test
Let's set ourselves some simple goals:
- Go to the official Emacs page.
- Check that there is a title with the text "GNU Emacs".
- Click on the Features link.
- Count that there are 6 features.
We create a file test_emacs.py:
from playwright.sync_api import Page, expect
def test_emacs_features(page: Page):
page.goto("https://www.gnu.org/software/emacs/")
expect(page.locator("h1")).to_contain_text("GNU Emacs")
page.click("a[href$='#features']")
expect(page.locator(".features li")).to_have_count(6)
We run it:
pytest test_emacs.py
Playwright will open a browser (by default without a window, in headless mode), will do all the instructions at lightning speed and, if everything matches, you'll have a lovely green. If something fails, it will warn you with the detail of what it expected and what it found, and it can even save a screenshot and a video of the moment.
Other interesting checks
Find a text inside a tag
If we had:
<h1 class="title">Welcome</h1>
We could locate it as if it were a CSS selector:
expect(page.locator("h1.title")).to_contain_text("Welcome")
Count elements
expect(page.locator("li")).to_have_count(12)
Fill in a form and submit it
page.fill("#input-name", "Julio the Tester")
page.fill("#input-email", "testing@prueba.com")
page.fill("#input-message", "A long and detailed test message.")
page.check(".disclaimer input[type='checkbox']")
page.click("input[type='submit']")
Wait for a class to appear after clicking
page.click("button[type='button']")
expect(page.locator(".alert")).to_have_class("alert alert--success")
Playwright automatically waits for the element to be ready, so you'll rarely have to add fixed waits by hand.
Activity 1
Check with E2E that JavaScript appeared in 1995.
1. Go to https://es.wikipedia.org/
2. Type JavaScript in the search box.
3. Press enter.
4. Check that it took you to the correct page (hint: id firstHeading).
5. Verify the year.
Activity 2
Create a page to sign in and enter a private site.
- email: lorem@ipsum.dolor
- password: 123
If the fields are correct, it takes you to another page with the message "Hello lorem@ipsum.dolor". If not, it shows the appropriate error.
Test it with E2E and answer:
- Do the error messages work?
- Are the texts correct?
- Does it enter when the correct fields are inserted?
This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.
Desafíos de programación atemporales y multiparadigmáticos
Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.
Buy the bookWill you buy me a coffee?
This is how I keep writing without ads or paywalls.
Sure, it's on me!
Comments
There are no comments yet.