Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Selenium test framework integration

Selenium test framework integration

Selenium can integrate with various test frameworks, such as unittest and pytest, enabling more structured test code management and test report generation.


The difference between unittest Integration

unittest is a testing framework in the Python standard library that provides rich assertion methods and test organization approaches.

We can integrate Selenium with unittest to write structured test cases.

Basic structure

Below is a simple example of Selenium integrated with unittest :

Example

import

unittest

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

by

import

By

class

TestGoogleSearch

(

unittest

.

TestCase

)

:

   

def

setUp

(

self

)

:

       

Set the correct driver path

        service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

        options

=

webdriver.

ChromeOptions

(

)

        driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

       

self

.

driver

.

get

(

"https://www.baidu.com"

)

   

def

test_search

(

self

)

:

        search_box

=

self

.

driver

.

find_element

(

By.

NAME

,

"wd"

)

        search_box.

send_keys

(

"Selenium"

)

        search_box.

submit

(

)

       

self

.

assertIn

(

"Selenium"

,

self

.

driver

.

title

)

   

def

tearDown

(

self

)

:

       

self

.

driver

.

quit

(

)

if

name

==

"main"

:

   

unittest

.

main

(

)

Code Analysis

Running tests

You can run the tests with the following command:



python -m unittest test_baidu_search.py


The difference between pytest Integration

pytest is another popular Python testing framework that offers more concise syntax and more powerful features. We can integrate Selenium withpytestto write more flexible test cases.

Basic structure

Below is a simple example of Selenium withpytest:

Example

import

pytest

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

by

import

By

@

pytest.

fixture

def

browser

(

)

:

   

Set the correct driver path

    service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

    options

=

webdriver.

ChromeOptions

(

)

    driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

   

yield

driver

    driver.

quit

(

)

def

test_baidu_search

(

browser

)

:

    browser.

get

(

"https://www.baidu.com"

)

    search_box

=

browser.

find_element

(

By.

NAME

,

"wd"

)

    search_box.

send_keys

(

"Selenium"

)

    search_box.

submit

(

)

   

assert

"Selenium"

in

browser.

title

Code Analysis

Running tests

You can run the tests with the following command:



pytest test_baidu_search.py


Generating test reports

In automated testing, generating test reports is very important. We can usepytest-htmlplugin to generate HTML-format test reports.

Installation pytest-html

You can install it with the following commandpytest-htmlplugin:

Example

pip

install

pytest-html

Generating test reports

When running tests, you can generate an HTML-format test report with the following command:



pytest --html=report.html

The generatedreport.htmlfile will contain detailed test results, including passed test cases, failed test cases, and error messages.

Test code

Selenium 4 introduced many new features, such as relative locators, improved window management, and better DevTools support:

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

by

import

By

from

selenium.

webdriver

.

common

.

keys

import

Keys

def

test_baidu_search

(

)

:

   

Set the correct driver path

    service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

    options

=

webdriver.

ChromeOptions

(

)

    driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

   

Open the Baidu home page

    driver.

get

(

"https://www.baidu.com"

)

   

Locate the Search Box and Enter a Keyword

    search_box

=

driver.

find_element

(

By.

NAME

,

"wd"

)

 

The name of Baidu's search box is "wd"

    search_box.

send_keys

(

"Selenium"

)

   

Locate the search button and click it

    search_button

=

driver.

find_element

(

By.

ID

,

"su"

)

 

The id of Baidu's search button is "su"

    search_button.

click

(

)

   

Wait for the page to finish loading (simple wait; in real projects, explicit waits are recommended)

    driver.

implicitly_wait

(

5

)

   

Assert whether the search results page contains the keyword

   

assert

"Selenium"

in

driver.

page_source

   

Close the browser

    driver.

quit

(

)

Running tests

if

name

==

"main"

:

    test_baidu_search

(

)