Detailed practical skills, let you easily become a master of WEB automation testing

Table of contents

1. What is WEB automated testing

2. WEB automated testing tools

3. Selenium+Python environment construction

1. Install the Python interpreter

2. Install the Selenium library

3. Download the browser driver

4. Configure environment variables

Fourth, WEB automated testing practice

1. Write test scripts

2. Use the Page Object pattern

3. Use data-driven testing

V. Summary


 The CSDN article on WEB automation testing aims to introduce basic knowledge, tools and practical skills to help readers better perform WEB automation testing. This article will take Selenium+Python as an example and provide detailed code examples and test cases so that readers can easily follow along.

1. What is WEB automated testing

WEB automated testing refers to the use of automated tools to simulate users performing various operations in the browser, such as clicking on links, filling in forms, submitting data, etc., and verifying whether the response and behavior of the page meet expectations. It can significantly improve testing efficiency, reduce errors and repetitive work, thereby saving teams time and money, and accelerating product release schedules.

2. WEB automated testing tools

At present, there are many excellent WEB automated testing tools to choose from on the market, including Selenium, Appium, Robot Framework, Cypress, etc. Among them, Selenium is one of the most popular and widely used WEB automated testing tools. It supports multiple programming languages, such as Java, Python, Ruby, etc., and can run on multiple browsers and platforms. In this article, we will use Selenium+Python as a tool for WEB automated testing.

3. Selenium+Python environment construction

To use Selenium+Python for WEB automated testing, we need to install the Python interpreter and Selenium library, and configure the browser driver. Here we take setting up the environment on the Windows system as an example.

1. Install the Python interpreter

First, we need to download and install the Python interpreter. You can get the latest Python version on the official website, here we choose version 3.x to install.

2. Install the Selenium library

Open a terminal (or PowerShell) and enter the following command to install the Selenium library:

pip install selenium

3. Download the browser driver

Selenium needs a browser driver to interact with the browser, so we need to download the corresponding browser driver. Here we take the Chrome browser as an example, download the corresponding version of the driver from the ChromeDriver official website , and decompress it to a certain directory, such as D:/WebDrivers/.

4. Configure environment variables

In order for the operating system to find the browser driver, we need to add its directory to the system environment variable. On the Windows system, you can open "Control Panel" -> "System and Security" -> "System" -> "Advanced System Settings", click the "Environment Variables" button, and find "Path" in the "System Variables" area variable and click the "Edit" button, and add the directory path where the browser driver is located at the end of the edit window, such as D:/WebDrivers/. After the configuration is complete, you need to restart the command line terminal to take effect.

Fourth, WEB automated testing practice

Next, we will use a simple example to introduce how to use Selenium+Python for WEB automated testing. This example is a simple login page, which contains two input boxes for user name and password and a login button. We will write a test script to simulate the user to perform the following operations on this page:

  1. Open the Chrome browser and visit the login page;
  2. Enter the correct user name and password, and click the login button;
  3. Jump to the homepage after successful login verification.

1. Write test scripts

Create a new Python file named test_login.py and enter the following test script code:

from selenium import webdriver
创建Chrome浏览器实例
browser = webdriver.Chrome()

打开登录页面
browser.get('https://www.example.com/login')

输入用户名和密码
username_input = browser.find_element_by_name('username')
password_input = browser.find_element_by_name('password')
username_input.send_keys('test_user')
password_input.send_keys('test_password')

点击登录按钮
login_button = browser.find_element_by_xpath('//button[text()="Login"]')
login_button.click()

验证是否成功跳转到主页
assert browser.current_url == 'https://www.example.com/home'

关闭浏览器
browser.quit()

### 2. 运行测试脚本

保存并运行test_login.py文件,如果一切正常的话,我们应该可以看到Chrome浏览器自动打开,并访问指定的登录页面。然后自动输入用户名和密码、点击登录按钮,并验证是否成功跳转到主页。最后,自动关闭浏览器。

## 五、WEB自动化测试技巧

为了提高WEB自动化测试的效率和准确性,下面介绍几个实用的技巧:

### 1. 使用等待机制

由于网络延迟和页面加载时间等原因,我们不能保证每个操作都能立即生效。因此,在使用Selenium自动化工具时,需要适当地添加等待机制。例如,可以使用sleep函数来强制等待一段固定的时间,或者使用WebDriverWait类设置显示等待条件(如元素可见、元素存在等)。

```python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# 使用显示等待,等待10秒钟直到元素可见
wait = WebDriverWait(browser, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, 'some_id')))

2. Use the Page Object pattern

To make test scripts clearer and easier to maintain, the Page Object pattern can be used. This mode encapsulates the page object as a class or module, so that the test script only needs to call the corresponding method or property to complete the operation without caring about specific HTML tags and CSS selectors. For example:

class LoginPage:
    def __init__(self, browser):
        self.browser = browser

    def open(self):
        self.browser.get('https://www.example.com/login')

    def login(self, username, password):
        username_input = self.browser.find_element_by_name('username')
        password_input = self.browser.find_element_by_name('password')
        username_input.send_keys(username)
        password_input.send_keys(password)
        login_button = self.browser.find_element_by_xpath('//button[text()="Login"]')
        login_button.click()

    def is_login_success(self):
        return self.browser.current_url == 'https://www.example.com/home'

# 测试脚本中调用
login_page = LoginPage(browser)
login_page.open()
login_page.login('test_user', 'test_password')
assert login_page.is_login_success()

3. Use data-driven testing

If we need to test multiple sets of data on the same page, we can use data-driven testing. For example, we can store test data in CSV or Excel files, then use Python's pandas library to read the data and execute test cases one by one. In this way, test efficiency and coverage can be greatly improved.

import pandas as pd

# 从CSV文件中读取测试数据
data = pd.read_csv('test_data.csv')

# 遍历每一行数据,并执行测试用例
for index, row in data.iterrows():
    username = row['username']
    password = row['password']

    login_page = LoginPage(browser)
    login_page.open()
    login_page.login(username, password)
    assert login_page.is_login_success()

V. Summary

This article introduces the basic knowledge, tools and practical skills of WEB automated testing, and how to use Selenium+Python for testing. We also provide detailed code examples and test cases, hoping to help readers better understand WEB automation testing and master the practical skills. At the same time, this paper also introduces the methods of improving test efficiency and accuracy, such as waiting mechanism, Page Object mode and data-driven test. In actual WEB development, WEB automated testing has become an essential link. Through DevOps practices such as continuous integration and continuous delivery, it can effectively accelerate the product release process and improve product quality and customer satisfaction.

 Structural diagram of automated test learning steps:

automated test:

 

Guess you like

Origin blog.csdn.net/Free355/article/details/130463356