This document uses the popular pom design pattern to set up the test framework, write test cases, generate test reports, and finally integrate jenkins.
1, selenium
selenium is an open source automatic testing tool for web ui, which will not be introduced in detail.
2, Environment construction
The environment construction is very simple. Please refer to my previous blog. selenium beginner
3, po design
po(pom) mode: the page object model encapsulates the pages in the project into a class, and a page corresponds to a class. Then instantiate the object of this class to call the properties and methods in the class.
Benefits:
Improve code maintenance
Code reuse
3.1 po layering
3.1.1 foundation layer:
Basic layer: selenium basic method for packaging
Code implementation:
class BasePage: def __init__(self,driver): self.driver=driver # Open page def into_testin(self,url): self.driver.get(url) # Positioning element def locate_element(self,args): return self.driver.find_element(*args) #Locate a set of elements def locate_eles(self,args): return self.driver.find_elements(*args) #Input value def input_(self,args,text): self.locate_element(args).send_keys(text) #Click the button def click_button(self,args): self.locate_element(args).click()
3.1.2 page object layer
Page object layer: pageobject, which stores the element positioning and operation process of the page
Login module code implementation:
from time import sleep from basepage.base_page import BasePage from selenium.webdriver.common.by import By class LoginPage(BasePage): '''Locate page elements''' #url testin_url='https://www.testin.cn/account/login.htm' #Enter user name and password username_loc=(By.ID,'email') password_loc=(By.ID,'pwd') #Click the login button login_button=(By.ID,"submitBtn") #Login mailbox operation process def testin_login(self,username='18170710339',password='Quyun1230'): self.into_testin(self.testin_url) sleep(2) self.locate_element(self.username_loc).send_keys(username) sleep(1) self.locate_element(self.password_loc).send_keys(password) sleep(1) self.click_button(self.login_button)
Select the mobile code implementation:
from time import sleep from basepage.base_page import BasePage from selenium.webdriver.common.by import By class SelectPage(BasePage): '''Locate page elements''' #Positioning pop-up window win_loc=(By.XPATH,"//div[@class='modal-body']/i") #Locate iphone ip_12pro_loc=(By.XPATH,'//div[@id="collapse_1"]/div/dl[1]/dd/div/span[2]/label/span') # Locate iPhone 12 Pro Max ip_loc = (By.XPATH, "//button[@device-source='00008101-0004102C3468001E']") #Positioning Brand an_loc=(By.XPATH,'//div[@id="collapse_1"]/div/dl[2]/dd/div/span[3]/label/span') # Positioning Meizu Android mz_loc = (By.XPATH, "//button[@device-source='864968050654030']") #Positioning time to market onlin_loc=(By.XPATH,'//div[@id="collapse_1"]/div/dl[6]/dd/div/span[5]/label/span') # Huawei glory positioning time to market sj_loc = (By.XPATH, "//button[@device-source='7d22ebd8f913637d']") #Positioning text box text_detail=(By.XPATH,'//div[@id="modal_quota_alert"]/div/div/div[2]') #Positioning cancel box qx_but_loc=(By.XPATH,'//div[@id="modal_quota_alert"]/div/div/div[3]/button[1]') #Click the login button login_button=(By.ID,"submitBtn") #Login mailbox operation process def testin_select_01_iphone(self): sleep(1) self.locate_element(self.ip_12pro_loc).click() sleep(1) self.locate_element(self.ip_loc).click() sleep(1) as_text=self.locate_element(self.text_detail).text '''The assertion information is correct''' assert as_text==u'Your quota is insufficient. Do you want to buy it?' self.locate_element(self.qx_but_loc).click() sleep(2) def testin_select_02_android(self): sleep(1) self.locate_element(self.an_loc).click() sleep(1) self.locate_element(self.mz_loc).click() sleep(1) as_text = self.locate_element(self.text_detail).text assert as_text == u'Your quota is insufficient. Do you want to buy it?' self.locate_element(self.qx_but_loc).click() sleep(2) def testin_select_03_onlin_time(self): sleep(1) self.locate_element(self.onlin_loc).click() sleep(1) self.locate_element(self.sj_loc).click() sleep(1) as_text = self.locate_element(self.text_detail).text assert as_text == u'Your quota is insufficient. Do you want to buy it?' self.locate_element(self.qx_but_loc).click() sleep(2)
3.1.3 test case layer
Test case layer: store test cases and data
Code implementation:
import pytest from time import sleep from selenium import webdriver from common.excel import read_excel from pageobject.login_page import LoginPage from pageobject.select_phone import SelectPage class TestTestIn: #Open browser def setup(self) -> None: self.driver=webdriver.Chrome() driver=self.driver def teardown(self) -> None: sleep(1) self.driver.close() '''utilize excel Import login test data''' @pytest.mark.parametrize('case',read_excel('./data/data.xlsx','login')) def test_01_login(self,case): '''Test login module''' xh,case_name,username,password,is_exc,result,bz=case lp=LoginPage(self.driver) lp.testin_login(username,password) def test_02_select_iphone_12(self): '''The test selects the mobile phone according to the brand''' lp = LoginPage(self.driver) lp.testin_login() ps=SelectPage(self.driver) ps.testin_select_01_iphone() def test_03_select_androi_sys(self): '''The test selects the mobile phone according to the Android system''' lp = LoginPage(self.driver) lp.testin_login() ps=SelectPage(self.driver) ps.testin_select_02_android() def test_04_select_online_time(self): '''The test selects the mobile phone according to the time of listing''' lp = LoginPage(self.driver) lp.testin_login() ps=SelectPage(self.driver) ps.testin_select_03_onlin_time()
3.1.4 create test data
In the login module, we use data-driven. We first create a directory, such as data, in which we create an excel table and add corresponding data, such as:
The excel table is as follows:
3.1.5 create and read excel directory
Create a common directory and a read directory_ Excel files:
The codes are as follows:
import openpyxl def read_excel(excel_dir,sheet_name): '''read excel''' #load catalogs ex=openpyxl.load_workbook(excel_dir) #Get sheet page sheet=ex[sheet_name] #Print table maximum rows and columns # print(sheet.max_row,sheet.max_column) # print(sheet.cell(2,1).value) #Circular rows and columns sheet_list=[] for row in range(2,sheet.max_row+1): row_list=[] for col in range(1,sheet.max_column+1): row_list.append(sheet.cell(row,col).value) sheet_list.append(row_list) return sheet_list if __name__ == '__main__': read_excel('../data/data.xlsx','login')
3.2 generate test report
3.2.1 create report directory
This time we use the pytest testing framework, so of course, we use the allure test report. If you are not familiar with the allure report, you can refer to my last detailed introduction: Unit test framework - pytest from getting started to mastering the latest tutorial of 2021
First create two directories: a temporary directory: temp and reports for storing test reports:
3.2.2 create report profile
Create a new pytest INI file:
[pytest] # addopts=-vs --alluredir ./temp testpaths=./testcase python_files=test_*.py python_classes=Test* python_functions=test_*
3.3 operation test
According to the above operations, such a directory will eventually be formed:
3.3.1 operation case:
Create a run_test.py file:
import os import pytest from time import sleep if __name__ == '__main__': pytest.main() sleep(2) os.system('allure generate ./temp -o ./reports --clean')
3.3.2 viewing operation results
3.3.2.1 results in editor
It can be seen that all the results run through.
3.3.2.2 viewing test report
View the html test in the reports directory and open it with a browser:
All test cases passed.
4, jenkins integration
If you are not familiar with jenkins, you can refer to my previous blog, which has a detailed construction tutorial: Use of jenkins continuous integration (including RF automatic execution automation)
4.1 start jenkins
Enter the jenkins directory in cmd and execute the command:
java -jar jenkins.war
jenkins started successfully
4.2 create task
Enter jenkins and create a task:
Add project directory:
Set run command:
Save and create successfully.
After the task is created successfully, execute the run operation.
You can see that it is already running.
View run results:
Run successfully. The test report, as above, will not be read again.