How to do web automation testing? Detailed process and steps of web automation testing

1. What is web automation testing

Automation (Automation) refers to the machine equipment, system or process (production, management process) without the direct participation of people or fewer people, according to people's requirements, through automatic detection, information processing, analysis and judgment, manipulation and control, to achieve the expected target process.

This is the definition of automation in textbooks. Returning to automated testing, what is automated testing?

It refers to the test that the test process is carried out without human or less human intervention. To put it simply, it is to use a program or script to test the program. In web automated testing, it is mainly used to save testers from tedious tasks. It is liberated from the content, mainly to do some things that require multiple inputs and multiple runs. For example, we use boundary values ​​and equivalence classes to design a lot of test data that needs to be executed. For example, when the business process needs to be executed many times, we can use it. web automation testing

2. Tools for web automation testing

Now the mainstream web testing tools we commonly use are the set of selenium tools including

  • The browser generally chooses chrome
  • The driver corresponding to the browser (chromedriver)
  • Python
  • Selenium library

3. Environment installation for web automated testing

  • Browser installation, download the browser, follow the next step
  • chromedriver download can go to  npmmirror.com/
  • Note: the large version of 100 is OK

 

insert image description here

insert image description here

Fourth, the method of web automated testing

Web automated testing generally uses the same method of designing test cases as functional testing, using equivalence class division, boundary values, cause-and-effect diagrams, scenario methods, etc.

We generally use the po mode design for the implementation of web automation testing

PO is the abbreviation of page object. The core idea is to reduce redundant codes by encapsulating interface elements. At the same time, in later maintenance, if element positioning changes, you only need to adjust the code encapsulated by page elements to improve the maintainability of test cases. readability.

The PO mode can divide a page into three layers, object library layer, operation layer, and business layer.

Object library layer: Encapsulates methods for locating elements.

Operation layer: Encapsulates operations on elements.

Business layer: combine one or more operations to complete a business function.

For example, to log in: you need to enter the account number, password, and click to log in. Three operations.

The test script only needs to call the business layer code to complete

When the page needs to appear, you only need to test the code, you don’t need to modify it at all, you only need to modify the operation layer.

5. Process implementation of web automation testing

The web automation test process is basically the same as the functional test:

Below we have logged in as an example

(1) Demand analysis

Please add a picture description

Here we take mobile phone login as an example

Please add a picture description

(2) Design test cases and test data

Automated test test cases do not need to be as detailed as manual test design

ID test module title Preconditions test input
login_001 Log in Wrong phone number login The network is normal and the function is normal Mobile phone number is wrong Other input items are normal

[Below is the test data]

[
 {
    "username": "21888888888",
    
    "code": "8888",
    "ast_msg": "账号格式不匹配",
    "desc": "用户名错误"
 },
  {
    "username": "12888888888",
    "pwd": "123456",
    "code": "8888",
    "ast_msg": "账号格式不匹配",
    "desc": "用户名错误"
  },
  {
    "username": "1088888888",
    "pwd": "123456",
    "code": "8888",
    "ast_msg": "账号格式不匹配",
    "desc": "用户名错误"
  },
  {
    "username": "138888888889",
    "pwd": "123456",
    "code": "8888",
    "ast_msg": "账号格式不匹配",
    "desc": "用户名错误"
  },
  {
    "username": "32888888888",
    "pwd": "123456",
    "code": "8888",
    "ast_msg": "账号格式不匹配",
    "desc": "用户名错误"
  },
  {
    "username": " ",
    "pwd": "123456",
    "code": "8888",
    "ast_msg": "用户名不能为空",
    "desc": "用户名错误"
  }
]

 

(3) Build a web automation test environment

It has been built in the above environment installation

(4) Design web automation test framework

The content of the general test po mode includes

insert image description here

(5) Write code

Create page_login in po folder

	from selenium.webdriver.common.by import By

	from utils import UtilsDriver
	from base.page_base import BasePage

	# 界面对象层

  	class PageLogin(BasePage):

  	# 账号元素
  	def find_username(self):
     	return self.driver.find_element_by_id("username")
       	# return self.driver.find_element(*self.username)
      	# return self.get_element(self.username)

   	# 密码元素
   	def find_pwd(self):
      return self.driver.find_element(By.ID,"password")

  	# 验证码元素
    def find_vcode(self):
      return self.driver.find_element_by_id("verify_code")

    # 按钮开始登录元素
    def find_login_btn(self):
	   # return self.driver.find_element_by_name("sbtbutton")
       return self.driver.find_element(By.NAME,"sbtbutton")

	# 操作层
class HandleLogin(object):
    def __init__(self):
       self.page_login=PageLogin()

    def input_username(self,username):
        self.page_login.find_username().send_keys(username)

    def input_pwd(self,pwd):
      self.page_login.find_pwd().send_keys(pwd)

    def input_vcode(self,code):
       self.page_login.find_vcode().send_keys(code)
	
    def click_login_btn(self):
        self.page_login.find_login_btn().click()

	# 业务层
	# 输入用户名密码验证码 点击登录
class LoginProxy(object):
   def __init__(self):
     self.handle_login = HandleLogin()

  def login(self,username,pwd,code):
    self.handle_login.input_username(username)
    self.handle_login.input_pwd(pwd)
    self.handle_login.input_vcode(code)
    self.handle_login.click_login_btn()

 

 Script execution code to create test_login

# from selenium import webdriver
	from po.page_home import HomeProxy
	from po.page_login import LoginProxy
	from utils import UtilsDriver,get_data
	import time
	import pytest
	import allure
	
	@allure.feature("登录功能")
	class Test_login:
	    def setup_class(self):
	        self.login_p=LoginProxy()
	        self.home_p=HomeProxy()
	    def setup(self):
	        
	        # 进入首页
	        UtilsDriver.get_driver().get("http://127.0.0.1/")
	      
	        # 进入到login界面
	        self.home_p.go_login_page()
	    def teardown_class(self):
	        time.sleep(2)
	        UtilsDriver.quit_driver()
	
	    @pytest.mark.parametrize(["username","pwd","code","asrt_msg"],get_data())
        @allure.story("登录用户名错误")
	    def test_login_username_error(self,username,pwd,code,asrt_msg):
	        self.login_p.login(username,pwd,code)
	        time.sleep(1)
	        # 登录是否成功的预期结果
	        res = UtilsDriver.get_msg()
	        assert asrt_msg in res

 

(6) Execute test cases

You can use pytest to control the execution of test cases

@pytest.mark.parametrize([“username”,“pwd”,“code”,“asrt_msg”],get_data())

[pytest]
addopts = -s --alluredir report
testpaths = ./script
python_files = test_*.py *test.py
python_classes = Test_*
python_functions = test_*

 

7) Generate test report

You can use allure to generate test reports

insert image description here

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive  

 

Guess you like

Origin blog.csdn.net/kk_lzvvkpj/article/details/130174584