Kihagyás

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
from selenium import webdriver

driver = webdriver.Chrome()

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 text property;
  • 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
driver.implicitly_wait(2)

An explicit wait waits for a particular condition:

1
2
3
message = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "message"))
)

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
python3 -m venv venv
source venv/bin/activate

On Windows PowerShell, activate it with:

1
.\venv\Scripts\Activate.ps1

Install Selenium:

1
python -m pip install --upgrade selenium

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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


def main():
    driver = webdriver.Chrome()

    try:
        driver.get("https://www.selenium.dev/selenium/web/web-form.html")

        assert driver.title == "Web form"

        driver.find_element(By.NAME, "my-text").send_keys("Selenium")
        driver.find_element(By.CSS_SELECTOR, "button").click()

        message = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, "message"))
        )
        assert message.text == "Received!"
        print(message.text)
    finally:
        driver.quit()


if __name__ == "__main__":
    main()

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
selenium-behave-demo/
├── features/
│   ├── environment.py
│   ├── web_form.feature
│   └── steps/
│       └── web_form_steps.py
└── pages/
    └── web_form_page.py

Install the dependencies:

1
python -m pip install --upgrade behave selenium

Create features/web_form.feature:

1
2
3
4
5
6
7
Feature: Selenium web form

  Scenario: Submit text through the form
    Given I open the Selenium web form
    When I enter "Selenium" in the text field
    And I submit the form
    Then the confirmation message should be "Received!"

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
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


class WebFormPage:
    URL = "https://www.selenium.dev/selenium/web/web-form.html"
    TEXT_FIELD = (By.NAME, "my-text")
    SUBMIT_BUTTON = (By.CSS_SELECTOR, "button")
    MESSAGE = (By.ID, "message")

    def __init__(self, driver):
        self.driver = driver

    def open(self):
        self.driver.get(self.URL)

    def enter_text(self, value):
        self.driver.find_element(*self.TEXT_FIELD).send_keys(value)

    def submit(self):
        self.driver.find_element(*self.SUBMIT_BUTTON).click()

    def wait_for_message(self, timeout=10):
        return WebDriverWait(self.driver, timeout).until(
            EC.visibility_of_element_located(self.MESSAGE)
        ).text

Create features/environment.py:

1
2
3
4
5
6
7
8
9
from selenium import webdriver


def before_scenario(context, scenario):
    context.driver = webdriver.Chrome()


def after_scenario(context, scenario):
    context.driver.quit()

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
from behave import given, then, when

from pages.web_form_page import WebFormPage


@given("I open the Selenium web form")
def step_open_form(context):
    context.page = WebFormPage(context.driver)
    context.page.open()


@when('I enter "{value}" in the text field')
def step_enter_text(context, value):
    context.page.enter_text(value)


@when("I submit the form")
def step_submit(context):
    context.page.submit()


@then('the confirmation message should be "{expected}"')
def step_verify_message(context, expected):
    assert context.page.wait_for_message() == expected

Run the scenarios:

1
behave

The browser is created and closed by Behave hooks, so cleanup also occurs when a scenario fails.


Utolsó frissítés: 2026-09-04 08:34:02