Directory Structure Building
1 test_framework2 |--config (config file)3 |--data (data file)4 |--drivers (drive)5 |--log (log)6 |--report (test report)7 |--test (program code)8 |--case (test case)9 |--common (abstract generic code)Ten |--page (Page class code) One |--suit (test set) A |--utils (public method) -|--README.MD (Add a descriptive file that tells the team that the framework needs the environment and how to use it)
1.config layer, put the configuration file, all the project related configurations are put here, with Python to support a good configuration file format such as INI or YAML configuration. Implement configuration and code separation.
2.data layer, put the data file, you can put all the testcase parametric related files here, generally can be used in xlsx, CSV, XML and other formats. Implement data separation from code.
3.drivers layer, put the required drivers, such as Chromedriver, Iedriverserver and so on.
4.log layer, all generated logs are stored here, you can classify the log, such as Run time Log test log, error log and so on.
5.report layer, put the program run generated reports, generally can have HTML reports, Excel reports and so on.
6.test source layer, put all the test related files:
case--Test Cases
common--items, page-independent packages, such as browser encapsulation
page--Page related Operations encapsulation
Test Suites for the suite--organization
7.utils layer , all the support codes are here, including classes and methods that read the Config class, write the log class, read the Excel, XML class, generate the report class (such as Htmltestrunner), and so on.
8.README.MD, descriptive files that tell team members what the framework requires and how to use it
Ii. encapsulated Public Method display
1. Incoming browser type open browser, initial page for homepage
From test.page.main_page import MainPage #导包
Self.page = MainPage (browser_type='firefox'). Get (self. URL, Maximize_window=false)
Browser_type: Browser type, Firefox, ie, Chrome
Self. URL: The browser address to be accessed, such as: url = "Http://www.baidu.com"
2. Get Excel file Data
From Utils.file_reader import Excelreader #导包
Datas = Excelreader (Self.excel, title_line=false). Data
Self.excel: Need to get the Excel file address, such as: Excel = ' E:\Software\sichuantest\data\baidu.xlsx '
Title_line: If there is a header row, the default is true, as shown in the Excel file:
title_line=true,datas={' search ', ' Selenium gray blue ', ' Python Selenium '}
title_line=false,datas={{' search ': ' Selenium gray blue '},{' search ': ' Python Selenium '}}
3. Generate Test Reports
From Utils. Htmltestrunner Import Htmltestrunner #导包
file_name = Os.path.split (__file__) [ -1].split ('.') [0] #获取用例文件名report= Report_path +'\\'+ file_name +'_report.html'#通过用例文件名拼接测试报告名
With open (report,'WB') as F:
Runner= Htmltestrunner (f, verbosity=2, title='HTML Report Test', description='HTML Report Content') #设置测试报告的title, description
Runner.run (Testbaidu ('Test_search')) #设置测试报告显示的用例内容
Report_path: Test report generation Path, such as: E:\Software\sichuantest\report
Testbaidu (' Test_search '): A use case function to be performed, i.e. the test report shows the use case content
4.log log printing, unified in the E:\Software\sichuantest\log\test.log file
From Utils.log import Logger #导包
Logger.info (Link.text)
Link.text: Need to print content
The Print log content format is as follows and can be positioned by file name and number of lines
2018-06-14 16:03:38,866-client.py[line:47]-Debug-get http://www.baidu.com
5. Get the value of the CONFIG.YML configuration variable via config.py
As below, config.yml has config URL variable
Can be obtained directly from the following code
From Utils.config import config #导包
url = Config (). Get ('url')
6. Assertion class: Assertion.py
The content is a public assertion operation, and the assertion is the basis for judging the success of the use case, and the canonical use case should be added
7. Page Operation class: Page/main_page.py,page/result_page.py.
Content is a common operation for some pages, such as locating an element by ID and assigning it to it
Python automation use case framework building--Directory structure planning