01. UI automation test script for login function

In the software development process, the login function is a very important and common function. In order to ensure the stability and correctness of the login function, we can use UI automation testing to automatically verify the login process. This article will introduce how to write a UI automation test script for login function, and realize the automation test by using Selenium and pytest library.

technology stack

  • Python programming language
  • Selenium library
  • pytest testing framework

Preparation

Before writing automated test scripts, we need to do some preparatory work. Here are the steps to get ready:

  1. Install Python: Visit the Python official website ( https://www.python.org/), download and install the latest version of the Python interpreter.
  2. Install dependent libraries: Use the pip command to install Selenium and pytest libraries.
Copy code
pip install selenium pytest
  1. Download the browser driver: According to the type of browser you use, download the corresponding browser driver and add it to the environment variable of the system.

Automation Project Directory

ui_project/
  |-- tests/
  |     |-- test_module1.py
  |     |-- test_module2.py
  |     |-- ...
  |
  |-- pages/
  |     |-- page_module1.py
  |     |-- page_module2.py
  |     |-- ...
  |
  |-- utils/
  |     |-- utility_module1.py
  |     |-- utility_module2.py
  |     |-- ...
  |
  |-- data/
  |     |-- test_data1.json
  |     |-- test_data2.csv
  |     |-- ...
  |
  |-- reports/
  |     |-- report1.html
  |     |-- report2.html
  |     |-- ...
  |
  |-- config/
  |     |-- config.py
  |
	|-- error_images
	|     |--1.png
      	|--2.png
  |-- conftest.py
  |-- requirements.txt

A brief description:

  • tests/ : This directory is used to store test code, and each test module (or test suite) corresponds to a test file.
  • pages/ : This directory contains the implementation of the Page Object Pattern. Each page object corresponds to a module file, which is used to encapsulate the element positioning and operation of the page.
  • utils/ : This directory is used to store utilities, helper functions or custom test tool libraries used in the testing process.
  • data/ : This directory contains data files needed for testing, such as test data, configuration files, etc.
  • reports/ : This directory is used to store test reports, which can be HTML reports, JUnit XML reports or report files in other formats.
  • config/ : This directory contains test configuration files such as URLs, database connection information, log levels, etc.
  • The conftest.py file will be automatically recognized and loaded during the test run, so multiple fixtures can be defined in it, and these fixtures can be referenced and shared by test cases or other files. By defining fixtures, you can implement some common test preparation and cleanup operations, such as creating and destroying test environments, initializing and cleaning up test data, and so on.
  • requirements.txt : This file lists the external libraries and version numbers that the project depends on, and is used to create and manage virtual environments.
  • error_images: error screenshot storage address

This is my project directory
image.png

Write test scripts

Here the automatic Hu script uses PO design ideas:
namely:

  1. Take the web page as the object, and the operation in the page as the method
  2. Page properties + page behavior form page_object

analysis page

Take "Lemon Class" mall as an example
image.png

1. 用户名输入框
find_element_by_xpath('//input[@placeholder="用户名"]')
2. 密码输入框
find_element_by_xpath('//input[@placeholder="密码"]')
3. 验证码输入框
find_element_by_xpath('//input[@placeholder="验证码"]')
4.登录按钮
find_element_by_xpath('//input[@value="登录"]')

Write the login page

import time

from selenium import webdriver


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

    def manage_login(self, username, password):
        # 1. 用户名输入框
        self.driver.find_element_by_xpath('//input[@placeholder="用户名"]').send_keys(username)
        # 2. 密码输入框
        self.driver.find_element_by_xpath('//input[@placeholder="密码"]').send_keys(password)
        # 3. 验证码输入框
        code = self.driver.find_element_by_xpath('//input[@placeholder="验证码"]')
        code.send_keys('lemon')
        # 4. 登录按钮
        login_btn = self.driver.find_element_by_xpath('//input[@value="登录"]')
        login_btn.click()
        self.driver.implicitly_wait(5)
        user = self.driver.find_element_by_xpath('//div[@class="el-dropdown"]/span[text()="student"]')
        assert user.text == username
        print(user.text)


if __name__ == '__main__':
    dr = webdriver.Chrome()#创建了一个 Chrome WebDriver 实例,用于控制 Chrome 浏览器。
    dr.get("http://mall.lemonban.com/admin/#/login")#打开了指定的网页
    cl = LoginPage(dr)#创建了一个 LoginPage 的实例,将之前创建的 WebDriver 实例传递给它。
    cl.manage_login("student",'123456a')
    time.sleep(3)
    dr.quit()#关闭了 WebDriver 实例,即关闭 Chrome 浏览器。

Through the above code, we can implement UI login

Write test cases

import pytest
from selenium import webdriver

from page.manage.page3_add_product import LoginPage
class TestManageLogin:
    @pytest.mark.usefixtures('setup_tear_down')
    def test_manage_login(self,setup_tear_down):
        driver=setup_tear_down
        cl=LoginPage(driver)
        cl.manage_login(username='student',password='123456a')

A test class TestManageLogin is defined , and a test method test_manage_login is defined in it . This method specifies the setup_tear_down fixture defined earlier using the @pytest.mark.usefixtures decorator . In the test method, first obtain the WebDriver instance through the parameter setup_tear_down , and pass it to the constructor of LoginPage to create a LoginPage object. Then call the manage_login method, pass the user name "student" and password "123456a", and perform the login operation.

Use conftest.pyfiles to store fixture preconditions
Define a fixture for creating and closing WebDriver instances, and the test1_manage_login.py file contains test classes and test methods, and obtains WebDriver by using fixtures

@pytest.fixture(scope='class')
def setup_tear_down():
    driver = webdriver.Chrome()
    driver.get('http://mall.lemonban.com/admin/#/login')
    yield driver
    driver.quit()

Execute the test case

import pytest

if __name__ == '__main__':
    pytest.main(["-sv", "tests/test1_manage_login.py"]
)

"""
-s:表示输入出所有的print语句
-v:表示输入详细的测试结果信息

"""

Guess you like

Origin blog.csdn.net/AAIT11/article/details/130781934