Robot+selenium write web UI automation use cases

Usually we can use robot framework to write interface automation use cases, but if some sites do not separate the front and back ends, there is a lot of repeated testing workload in the iteration process, there is no interface to call for verification, and there are automated testing requirements, what should we do? At this time, the automation of the web UI with frequent pitfalls is imperative. Robot is just an automation framework. Fortunately, it is stable and has excellent scalability. If you want to drive a web browser to work automatically, you only need to install another artifact, selenium. The following will outline how to get started with web UI. Once you enter the door, The rest is the workload of baidu and the help of the official website. It is recommended to use it in the investigation, otherwise it will be useless.

Prerequisites for writing use cases:

  • The robot/python3 environment has been deployed, see:
  • Install selenium, recommended version 3.11.0: pip install selenium==3.11.0
  • Install robotframework-seleniumlibrary, recommended version 3.1.1: pip install robotframework-seleniumlibrary==3.1.1
  • Install webdriver, recommend the popular chromedriver, download it from here: http://chromedriver.storage.googleapis.com/index.html

Reference the selenium package in the use case file

*** Settings ***
Library  Collections
Library  SeleniumLibrary

An example is as follows:

*** Test Cases ***

my first html
        #创建一个chrome浏览器
    Open Browser    http://www.我的域名.cn/     chrome

     #输入文本框
    input text      id=inputName            admin
    input text      id=inputPassword        admin
    input text      id=inputCode            123

    #点击按钮
    Click Button    Xpath=//form/div/span/button

        #单击A标签    | locator表示定位器
        #Click Link | locator

        #单击某个标签
        #Click Element    Xpath=//ul[@id='systemSetting0']

    ${cnt}          get title
    log     ${cnt}

    ${title}        get window titles
    log     ${title}
    Title Should Be    一个后台
    Location should Contain  http://www.我的域名.cn/
    Page Should Not Contain     不包含我
    Page Should Contain         必须包含我

        # 关闭打开的浏览器
        Close Browser
    Close All Browsers

When you execute the use case, you will see that a chrome is automatically opened, and the password is entered, and the login is clicked. It may be too fast to see clearly. In order to see clearly, you can use the sleep method to stay for a few seconds at each step:

# 这里的单位是秒
sleep   3

Of course, if your web loading is slow, it is unreliable to stay for a few seconds to avoid it. It is recommended to use this method:

Wait Until Page Contains Element

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326320724&siteId=291194637