[Web Project Combat] Learning Web Automation Testing from Scratch: Using Python and Selenium to Realize Website Login Function

B station first push! The most detailed collection of automated tests in 2023 can be mastered by novices, making testing simple, fast and reliable https://www.bilibili.com/video/BV1ua4y1V7Db

Table of contents

1. Environment construction

2. Write test cases

3. Run the test case

3.1 Command line mode

3.2 Integration into CI/CD process

4 Conclusion


Web automation testing practical project: use Selenium and Python to complete the automated testing of website login function

This article will introduce how to use Selenium and Python to write automated test scripts to test the website login function. We will test whether the login is successful by simulating the user to enter the user name and password on the website and click the login button.

1. Environment construction

First, we need to install Python and Selenium. Python can be downloaded and installed from the official website, and Selenium can be installed through the pip command. After installing Python and Selenium, we also need to download the WebDriver driver suitable for the current browser and add it to the system PATH.

2. Write test cases

Next, we will write a test case where we define the test steps and expected results. Here is a simple example:

from selenium import webdriver


def test_login():
    # 启动浏览器
    driver = webdriver.Chrome()

    # 打开网站
    driver.get('http://localhost:8080')

    # 输入用户名和密码
    username = driver.find_element_by_name('username')
    password = driver.find_element_by_name('password')
    username.send_keys('testuser')
    password.send_keys('123456')

    # 点击登录按钮
    login_btn = driver.find_element_by_css_selector('.login-btn')
    login_btn.click()

    # 验证是否登录成功
    assert '欢迎您' in driver.page_source

    # 关闭浏览器
    driver.quit()

In this test case, we first start the Chrome browser and open the local website through the get method. We then simulated the user entering their username and password on the website and clicking the login button. Finally, we verified that the login was successful using an assert statement.

It should be noted here that when writing test cases, we should try to avoid hard coding. For example, user names and passwords can be defined as variables and read from external files or databases to facilitate reuse and maintenance of test cases.

3. Run the test case

After writing the test case, we need to run the test case to check whether the system meets expectations. Here are two ways to run test cases.

3.1 Command line mode

Testing frameworks such as pytest can be used to run test cases. For example, enter the directory where the test file is located in the command line window and execute the following command:

pytest test_login.py

This will run all test cases in the test_login.py file. The test framework will automatically load the test file and execute all the functions starting with test_.

3.2 Integration into CI/CD process

In actual software development, we usually integrate test cases into the CI/CD process to continuously check the health of the system. For example, a build task can be created in Jenkins to execute test cases as build steps.

4 Conclusion

This article describes how to use Selenium and Python to write automated test scripts to test the website login function. We demonstrate the process of writing, running and integrating test cases through a simple example. I hope that readers can further study and master Web automation testing technology based on this example, and make greater contributions to software development quality assurance.

 Structural diagram of automated test learning steps:

Guess you like

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