Web automated testing framework pystest, selenium combined with python, testers do not need to know the code, only need to write configuration to achieve, and it is convenient for testers who understand the code to extend

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.yamlthis file is the global configuration, the file in the config directory can overwrite this configuration

  • Use:, python test.py filenamefilename is the file name in the config directory of the project. For example python3 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,test1Open 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

Instructions for use

  • Framework operation logic:

    1. Get configuration, merge configuration items, start browser
    1. wait_disappear Whether to configure waiting to disappear
    1. iframe is configured to switch iframe
    1. wait whether to configure waiting for loading
    1. Whether the listener is configured to listen, the configuration is to open the listener
    1. Find elements according to type, content, index
    1. Whether javascript is configured, if yes, execute javascript
    1. Perform actions based on action
  • The configuration file is divided into the following contents, and the format description is as follows:

    1. DEBUG: Boolean True/False,, whether to turn on the debugging mode, True-no report is generated, easy to debug
      IMAGE: Boolean ,, True/Falsewhether to take a screenshot, DEBUG is False and IMAGE is set to True to take a screenshot

    2. MAIL: Email configuration, if the configuration is correct, the test case execution report will be sent

    3. BROWSER, the setting of the browser layer

      type: string, the type of browser used, currently supported Chrome/Firefox

      bmp_path: string, browsermob_proxyaddress, proxy use, access to api request use

      proxy: Boolean,, True/Falsewhether to open the proxy

      h5: Boolean,, True/Falsewhether to use h5 for testing or not to open the browser

      device_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/Falsewhether to open the headless mode, no interface test, easy to integrate into the server automated deployment

    4. WEBSITE, start page setting

      url: string, start page address, currently only this configuration is available

    5. 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 file

      value: 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

    6. TEST, array, specify the specific test cases to be executed. If testa2there are any more submenu, it will be automatically obtained and executed. The TESTconfiguration of multiple files
      will be combined and executed. The order of execution is the order of names when the test cases are entered

      TEST:
          - 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,
    dependence yapfand pyflakes, 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

Guess you like

Origin blog.csdn.net/sting_bo/article/details/113794119