Automated test cases
Automated test cases can generally be transformed from manual test cases, but not all manual test cases should be transformed into automated test cases
- Considering the cost of script development, don't choose use cases with too complex process. If necessary, the process can be divided into multiple use cases to implement the script.
- The selected use cases can best be built into scenarios, such as a function module, which is divided into multiple use cases, and multiple use cases use the same scenario
- Repeated execution, cumbersome parts, such as field verification, prompt information verification, etc., are suitable for regression testing
- The selected use case can be the main process, which is suitable for smoke test
follow a principle
- A use case is a complete scenario, from the user logging in to the final exit and closing the browser
- A use case only verifies one function point. Do not verify all functions after the user logs in
- Write as few reverse test cases as possible, on the one hand, because there are many use cases of reverse logic, and on the other hand, it is complex to implement, troublesome and error prone
- Try to avoid dependencies between use cases
- After a use case is tested, the test scenario needs to be restored to avoid affecting the execution of other use cases
Automated test case design
Principle of turning test points into test cases
- Design a forward use case to cover enough valid equivalence data
- To design a reverse use case, you need to cover an invalid equivalent data, and all other data use forward data
Verification code automation method
- Develop temporary shielding verification? Use universal
- Machine learning training sample
- Call the OCR interface to parse the verification code in the picture
Automated test model
Linearity test
- The most basic code organization form, which simply simulates user steps or scenarios
- Poor maintainability
- There are many modules and complex operation, which can be written in one main operation module
- All use case steps are placed in one module, which is not readable
from selenium import webdriver import time #Get the url of qq mailbox driver=webdriver.Firefox() driver.get("http://mail.qq.com") time.sleep(1) #Switch to internal frame frm = driver.find_element_by_id("login_frame") driver.switch_to.frame(frm) #Enter the user name and password and click login driver.find_element_by_id("u").send_keys("919917003") driver.find_element_by_id("p").send_keys("**********") driver.find_element_by_xpath('//*[@id="login_button"]').click()
Modular drive test
The first mock exam is to separate the repetitive operation into a common module. When the use case is executed, it needs to be used.
from selenium import webdriver import time class mt(): #Initialize qq pwd def __init__(self,qq,password): self.driver = webdriver.Firefox() self.qq = qq self.password = password time.sleep(1)] #Sign in def login(self): self.driver.get("http://mail.qq.com") self.driver.switch_to.frame(self.driver.find_element_by_id("login_frame")) self.driver.find_element_by_id("u").send_keys(self.qq) self.driver.find_element_by_id("p").send_keys(self.password) time.sleep(1) self.driver.find_element_by_xpath('//*[@id="login_button"]').click() time.sleep(1) #Click write def write(self): self.driver.find_element_by_xpath('//*[@id="composebtn"]').click() time.sleep(1) #Click the upper right corner to exit the login def exit(self): self.driver.find_element_by_xpath('/html/body/div[3]/div[2]/div/div[1]/div[1]/div[1]/a[3]').click() time.sleep(1) #Close all pages def quit(self): self.driver.quit() if __name__ == "__main__": a = mt('1103007486','123456') a.login()
Other modularity:
Database operation
Mail module
Write log module
Write test log module
import os import time p_file = os.path.dirname(__file__)#Folder of current file path = p_file+'\logtext' def logText(filename,casename,info): file = '/test_'+time.strftime('%Y%m%d')+'.txt' print(file) with open(path+file,'a') as stream: t = time.strftime('%Y%m%d') stream.write(t+'--'+filename+'--'+casename+'--'+info+'\n') if __name__ == "__main__": logText('aaa','bbb','ccc')
Data driven test
The change of data drives the execution of automatic test, and finally leads to the change of test results and the parameterization of data
Parameterization method:
- List, dictionary
- It is more efficient when the amount of data is small and the number is small
- Define a list and dictionary, store data in it, and then call it.
- CSV,EXCEL
- The amount of data is large and the use frequency is not high
- Write in external file, call
- database
Directly obtained through the database read-write module - configuration file
Database connection, database host address, user name, password, port, etc
CSV file
- Fill in the data in EXCEL table and save it in CSV format
- import csv package import csv in python
- Open file with open
- Read data CSV reader()
- Traversal data for loop traversal
import csv from selenium import webdriver import os import time path = os.path.dirname(__file__)+r'\logtext\csvT.csv' with open(path,"r") as stream: data = csv.reader(stream) print(data)#<_csv.reader object at 0x000001FE00453E80> for d in data: driver = webdriver.Firefox() driver.get("http://mail.qq.com") time.sleep(1) frm = driver.find_element_by_id("login_frame") driver.switch_to.frame(frm) driver.find_element_by_id("u").send_keys(d[0]) driver.find_element_by_id("p").send_keys(d[1]) time.sleep(1) driver.find_element_by_xpath('//*[@id="login_button"]').click() time.sleep(1) driver.quit()
Excel file
- Install xlrd
- import xlrd of import package
- Open the file xlrd open_ workbook()
- Read a sheet page
- Date.sheets() reads all sheet s
- Date. sheet_ by_ Name is read by sheet name
- Read one row and one column
Table. row_ Value col_ Value (number of columns) - Number of rows and columns read
Table.nrows ncols - Read cell data
Table. Cell (row, column) value - Traversal data
import xlrd date = xlrd.open_workbook_xls(r'./logtext/excelt.xls') #Read 1st worksheet table = date.sheets()[0] #Read the table named sheet1 #table = date.sheet_by_name("Sheet1") #Read the first line rw1 = table.row_values(0) #Number of rows read rws = table.nrows #Read the first column col1 = table.col_values(0) #Number of read columns cols = table.ncols #Read the data in row 1 and column 2 celvlu = table.cell(0,1).value print(rw1,rws,col1,cols,celvlu) #Traverse all rows of data for i in range(rws): print(table.row_values(i))
Keyword Driven test
Change the test results by changing keywords (IDE, katalon, etc.)
Target commend value
You can write keyword driven methods and classes yourself
from selenium import webdriver import time class mkt(): def __init__(self): self.driver = webdriver.Firefox() # Open website def open_url(self): self.driver.get("http://mail.qq.com") time.sleep(1) # Input QQ def input_qq(self,qq): self.driver.switch_to.frame(self.driver.find_element_by_id("login_frame")) self.driver.find_element_by_id("u").send_keys(qq) time.sleep(1) #Input password def input_pwd(self,password): self.driver.find_element_by_id("p").send_keys(password) time.sleep(1) #Click login def click_btn(self): self.driver.find_element_by_xpath('//*[@id="login_button"]').click() time.sleep(1) def assert_url(self): expU = 'https://mail.qq.com/cgi-bin/frame_html?sid=_gQlbOL-JpiDAUAh&r=e6ae6c1ec470b2206c3c0dfdbd9a625f' actU =self.driver.current_url assert actU == expU if __name__ == "__main__": a = mkt() a.open_url() a.input_qq('3547840985') a.input_pwd('123456') a.click_btn() a.assert_url()
Checkpoint
It is realized by judging the assertion and verification methods in the language or framework, such as judging whether the login is successful or not