E2E tests (Selenium)

Table of contents

  1. Introduction
  2. Writing a test
  3. Running on the Selenium grid
  4. Running with a local browser
  5. Reference
    1. The selenium command

Introduction

End-to-end tests drive a real browser against your running product, for what functional tests cannot reach, such as JavaScript or flows across several pages. They live in each feature’s tests/e2e/ and run only when you ask.

splent feature:test --e2e
splent feature:test notes --e2e

The product must be selected (SPLENT_APP) and running (splent product:up).


Writing a test

import pytest
from splent_framework.environment.host import get_host_for_selenium_testing
from splent_framework.selenium.common import initialize_driver, close_driver

@pytest.fixture()
def browser():
    driver = initialize_driver()
    yield driver
    close_driver(driver)

def test_login_flow(browser):
    host = get_host_for_selenium_testing()
    browser.get(f"{host}/login")
    # ... interact with the page

initialize_driver() gives you a Chrome driver, or Firefox with initialize_driver("firefox"). Every driver waits up to 30 seconds for a page and 10 seconds for an element.


Running on the Selenium grid

The product’s dev Compose file starts a Selenium hub with a Chrome and a Firefox node. It sets SELENIUM_GRID_URL and SELENIUM_TARGET_URL on the web container, so the tests run on the grid with no setup.

The browser runs in a grid node, so localhost there is the node itself. SELENIUM_TARGET_URL points it at the web container instead, and get_host_for_selenium_testing() returns that.


Running with a local browser

Without SELENIUM_GRID_URL, the driver starts a browser on the machine that runs the tests. Chrome or Firefox must be installed there, and webdriver_manager downloads the matching driver.

get_host_for_selenium_testing() then returns http://localhost:5000 when WORKING_DIR is unset, /workspace or /vagrant. Any other value raises ValueError, so set SELENIUM_TARGET_URL.


Reference

Variable Default Purpose
SELENIUM_GRID_URL unset Use this Selenium Grid hub instead of a local browser
SELENIUM_BROWSER chrome chrome or firefox. Any other value raises ValueError
SELENIUM_TARGET_URL unset The base URL get_host_for_selenium_testing() returns. Needed with a grid
Situation What to do
ModuleNotFoundError: No module named 'selenium' Add selenium and webdriver-manager to [project.optional-dependencies].dev and run splent product:derive, or docker exec <product>_web pip install selenium webdriver-manager once

The selenium command

splent selenium notes

splent selenium [MODULE] is for a product with a modules/ folder. It runs each module’s tests/test_selenium.py with its own python process, on the machine itself, and refuses to run in the CLI container or under Vagrant. When one fails it goes on, prints Selenium tests failed in: <paths> and exits 1. A product composed from features has no modules/, so there it points to splent feature:test --e2e and exits 1.


Back to top

splent. Distributed by an LGPL license v3. Contact us: drorganvidez@us.es