Automated testing using Python Selenium
- Supports running on PC, H5, agent obtains interface data, headless mode, generates reports (including screenshots), sends emails, etc.
Quick start
-
Copy and modify the global configuration,
cp config.example.yaml config.yaml
this file is the global configuration, the file in the config directory can overwrite this configuration -
Use:,
python test.py filename
filename is the file name in the config directory of the project. For examplepython3 test.py default
, if no file name
is entered, it will be used by defaultdefault.yaml
-
Test cases are written in YAML general format, see YAML example for details
-
Perform existing use cases:
python3 test.py default,test1
Open Baidu and search for "stingbo pystest", then jump to the github directory, and finally open the specified project
Other commands
-
View all use cases in the config directory:
python3 test.py ls
-
Execute multiple test cases at the same time:
python3 test.py test1,test2,test3
-
Execute all test cases in the config directory:
python3 test.py all_test
rely
-
Python requires version 3.7 or above
-
Generate test reports according to HTMLTestRunner , the report directory is reports
-
Install Selenium, documentation
-
Install WebDriver, driver download address.
Another: Chromium/Chrome driver needs to go over the wall and attach the mirror address -
Install BrowserMob Proxy to monitor and use the proxy to facilitate the use of interface data
Instructions for use
-
Framework operation logic:
- Get configuration, merge configuration items, start browser
- wait_disappear Whether to configure waiting to disappear
- iframe is configured to switch iframe
- wait whether to configure waiting for loading
- Whether the listener is configured to listen, the configuration is to open the listener
- Find elements according to type, content, index
- Whether javascript is configured, if yes, execute javascript
- Perform actions based on action
-
The configuration file is divided into the following contents, and the format description is as follows:
-
DEBUG: Boolean
True/False
,, whether to turn on the debugging mode, True-no report is generated, easy to debug
IMAGE: Boolean ,,True/False
whether to take a screenshot, DEBUG is False and IMAGE is set to True to take a screenshot -
MAIL: Email configuration, if the configuration is correct, the test case execution report will be sent
-
BROWSER, the setting of the browser layer
type: string, the type of browser used, currently supported
Chrome/Firefox
bmp_path: string,
browsermob_proxy
address, proxy use, access to api request useproxy: Boolean,,
True/False
whether to open the proxyh5: Boolean,,
True/False
whether to use h5 for testing or not to open the browserdevice_name: string, the name of the simulated device in h5 mode, currently only valid under Chrome, the strategy implemented by Firefox is to modify user_agent and set the browser interface size
headless: Boolean,,
True/False
whether to open the headless mode, no interface test, easy to integrate into the server automated deployment -
WEBSITE, start page setting
url: string, start page address, currently only this configuration is available
-
MENU, test case details
key: string, menu path
name: string, menu name/function name, convenient for users to view configuration and use
type: string, the way to find elements, consistent with that provided by webdriver
content: string, the above type corresponds to the content used to find the element, and it can be found once, and some paths cannot be found once through (referring to general rules, not absolute paths), then content is used. If configured as none, the current will not run The configuration step 7 finds the subsequent process of the element, but will continue to perform other configurations
contents: Array, the content used by finding the element multiple times
index: Integer, there may be multiple elements, use index to fix one, -1 means default, -2 means all, greater than or equal to 0, means list subscript
opertaion: array, operation to be done before action, array type, format is the same as above
action: string, the action to be performed
1. none: configured as none, no task operation is performed, if the javascript option is configured, javascript will be executed on the element, because the action is the last to be executed
2. open: open the menu, similar Click, but you can add an open configuration item, which configures the judgment flag, and currently realizes the class judgment. If there is this class, it will not be clicked. The purpose is to prevent a certain menu from being opened, and then clicking it will close
3. click: Click on the element
4. moveToClick: Move to the target element and click on the element
5. sendKeys: Fill in a value where you can input, see value below for details
5. modifyKeys: Same as sendKeys, except that it will be cleared first, see value below for details
6. upload: upload, value is the address of the uploaded filevalue: string, if the action is sendKeys (input)/modifyKeys (modify), value is required, and value can use a custom method, such as pkgpath:utils.util.Util:randstr:6(from utils.util import Util && Util .randstr(6)), see utils/util for other custom methods
javascript: String, javascript code, such as: "arguments[0].scrollIntoView();", currently supports simple javascript code operation, complex ones need to be modified by yourself, if the current configuration item is obtained for multiple elements, this javascript will be executed
listener: Array, the api url that needs to be monitored and the code returned, BrowserMob Proxy must be installed to use the listener, and the correct application path must be configured
iframe: string, the id or name of the iframe. After configuration, the switch to the corresponding iframe layer will be executed. If the configuration is empty, it will remain in the current iframe layer or no switching operation will be performed. If there is no configuration, it will return to the corresponding iframe layer. Outermost
wait: object, the element waiting to be loaded, containing type and content (the meaning is the same as above), such as waiting for an element to appear
wait_disappear: object, the element waiting to disappear, containing type and content (same meaning as above), such as waiting for the disappearance of the mask layer
wait_time: number, specify the fixed waiting time for a certain action, in seconds
-
TEST, array, specify the specific test cases to be executed. If
testa2
there are any moresubmenu
, it will be automatically obtained and executed. TheTEST
configuration of multiple files
will be combined and executed. The order of execution is the order of names when the test cases are enteredTEST: - login - testa.testa1.testa2 - testb.testb1.testb2 - testc.testc1
-
YAML example
DEBUG: True/False #是否开启调试模式,True-不生成生成报告,方便调试
IMAGE: True/False #是否截图,DEBUG为False且IMAGE设置为True时截图
MAIL:
SEND: True/False
# 邮箱服务端配置
SMTP:
username: [email protected]
password: xxxxxxxxxx
host: smtp.163.com
port: 25
# 收件人列表
receiver:
- [email protected]
- [email protected]
BROWSER:
#浏览器类型
type: Chrome/Firefox
#bmp程序路径
bmp_path: your/browsermob-proxy/path
#是否开启代理,开启后,配置listener的操作,会记录请求日志到logs目录下
proxy: True/False
#H5
H5: True/False
#模拟的设备名称,目前只有在Chrome下生效,Firefox实现的策略是修改user_agent并设置浏览器界面大小
device_name: iPhone 7
#是否开启无头模式
headless: True/False
WEBSITE:
#启动页地址
url: http://www.your_website_url.com
MENU:
login:
key: login
name: 登录
type: xpath
content: //button[@class='btn btn-primary' and text()='登录']
index: -1
action: click
listener:
-
url: login/url
code: 0
operation:
-
name: 用户名
type: tag name
content: input
value: xxxxx
index: 2
action: sendKeys
-
name: 密码
type: tag name
content: input
value: xxxxx
index: 3
action: sendKeys
to sum up
-
When using contents to get the element multiple times, get the element in the last range, use .//(dot+double slash)
-
When using class matching, if there are spaces in the class, you must also include them when configuring xpath, or use contains
-
In pre-commit.sample project file Git Hook,
cp pre-commit.sample .git/hooks/pre-commit
,
dependenceyapf
andpyflakes
, for the format detection python code syntax -
Use examples can basically be found in the files in the config directory, I wish you a happy use, if you have any questions or suggestions, please submit an issue