Selenium¶
Selenium is a browser-automation project commonly used for functional and end-to-end testing of web applications. Selenium WebDriver controls a browser through the browser vendor's automation interface.
WebDriver¶
A WebDriver object represents a browser session. Selenium provides language bindings for creating and controlling sessions in browsers such as Chrome, Edge, Firefox, and Safari.
1 2 3 | |
Modern Selenium releases include Selenium Manager, which can discover, download, and cache a compatible browser driver when one has not been configured explicitly. Manual driver management or a third-party driver manager is therefore not normally required.
Browser and driver requirements
Driver management and browser installation are different concerns. A suitable browser must be available or managed by the environment, and downloading drivers or browsers may require network access. Controlled CI environments may instead use pinned browser images or explicitly configured drivers.
Locators¶
Locators identify elements in the Document Object Model (DOM). Common strategies include:
By.ID;By.NAME;By.CSS_SELECTOR;By.XPATH;By.LINK_TEXT;By.TAG_NAME.
Prefer stable identifiers or concise CSS selectors that describe the intended element. Long DOM-dependent XPath expressions tend to break when the page structure changes.
Interacting with Elements¶
Typical operations include:
click();send_keys("text");clear();- reading the
textproperty; get_attribute("name").
Tests should verify observable outcomes rather than only confirm that an action was executed.
Waiting Strategies¶
Modern pages often update asynchronously. A test must wait for the required state rather than pause for an arbitrary time.
An implicit wait applies a timeout to element-location calls:
1 | |
An explicit wait waits for a particular condition:
1 2 3 | |
Explicit waits usually express intent more clearly. Avoid mixing large implicit and explicit waits, because their interaction can produce unpredictable total wait times.
Page Object Model¶
The Page Object Model (POM) represents a page or reusable page component as an object that exposes meaningful services to tests. It separates locators and UI operations from business-level test steps.
Benefits include:
- reduced locator duplication;
- clearer test intent;
- localized maintenance after UI changes;
- reusable page operations.
Page objects normally should not contain complete test scenarios. Assertions about business outcomes usually remain in the test, except for checks that verify that the page object loaded successfully.
Selenium mini example
Create and activate a virtual environment:
1 2 | |
On Windows PowerShell, activate it with:
1 | |
Install Selenium:
1 | |
The following example uses Selenium's public demonstration page:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
This example starts a browser, fills and submits a form, waits for the result, verifies it, and closes the browser even if the test fails.
Selenium with Behave
This exercise uses Gherkin scenarios, Behave step definitions, and a page object.
Project structure:
1 2 3 4 5 6 7 8 | |
Install the dependencies:
1 | |
Create features/web_form.feature:
1 2 3 4 5 6 7 | |
Create pages/web_form_page.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
Create features/environment.py:
1 2 3 4 5 6 7 8 9 | |
Create features/steps/web_form_steps.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Run the scenarios:
1 | |
The browser is created and closed by Behave hooks, so cleanup also occurs when a scenario fails.