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

Selenium Practical Project

Selenium Practical Project

In modern software development, automated testing has become one of the important means to ensure software quality.

Selenium is a widely used automation testing tool that supports multiple programming languages and browsers, can simulate user operations, and perform functional testing, regression testing, etc.

This article will detail how to use Selenium for automated testing and demonstrate, through a practical project, how to automate common functions such as login, registration, and search (using Baidu as an example).

1. Practical Project: Automated Testing Cases

1.1 Login Function Testing

Suppose we have a login page with username and password input boxes and a login button; we will write an automated test script to simulate the user login operation.

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

chrome_options

=

Options

(

)

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

chrome_options

)

Open the Login Page

driver.

get

(

"https://example.com/login"

)

Locate the Username Input Box and Enter the Username

username_input

=

driver.

find_element

(

By.

ID

,

"username"

)

username_input.

send_keys

(

"testuser"

)

Locate the Password Input Box and Enter the Password

password_input

=

driver.

find_element

(

By.

ID

,

"password"

)

password_input.

send_keys

(

"password123"

)

Locate the Login Button and Click It

login_button

=

driver.

find_element

(

By.

ID

,

"login-button"

)

login_button.

click

(

)

Verify Whether Login Succeeded

assert

"Welcome"

in

driver.

page_source

Close the browser

driver.

quit

(

)

1.2 Registration Function Testing

Next, we write an automated test script to simulate user registration.

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

by

import

By

Configure Chrome Options

chrome_options

=

Options

(

)

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

chrome_options

)

Open the Registration Page

driver.

get

(

"https://example.com/register"

)

Locate the Username Input Box and Enter the Username

username_input

=

driver.

find_element

(

By.

ID

,

"username"

)

username_input.

send_keys

(

"newuser"

)

Locate the Email Input Box and Enter the Email

email_input

=

driver.

find_element

(

By.

ID

,

"email"

)

email_input.

send_keys

(

"newuser@example.com"

)

Locate the Password Input Box and Enter the Password

password_input

=

driver.

find_element

(

By.

ID

,

"password"

)

password_input.

send_keys

(

"newpassword123"

)

Locate the Confirm Password Input Box and Enter the Password

confirm_password_input

=

driver.

find_element

(

By.

ID

,

"confirm-password"

)

confirm_password_input.

send_keys

(

"newpassword123"

)

Locate the Register Button and Click It

register_button

=

driver.

find_element

(

By.

ID

,

"register-button"

)

register_button.

click

(

)

Verify Whether Registration Succeeded

assert

"Registration successful"

in

driver.

page_source

Close the browser

driver.

quit

(

)

1.3 Search Function Testing (Baidu)

Finally, we write an automated test script to simulate the user entering a keyword in Baidu's search box and searching.

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

Configure Chrome Options

chrome_options

=

Options

(

)

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

chrome_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"

)

search_box.

send_keys

(

"runoob"

)

Simulate Pressing Enter to Search

search_box.

send_keys

(

Keys.

RETURN

)

Verify Whether the Search Results Page Contains the Keyword

assert

"runoob"

in

driver.

page_source

Close the browser

driver.

quit

(

)