airtest web automation practice

One, environment construction

Airtest has built-in python and selenium libraries and is encapsulated, so it is very convenient to use for web automation

Principle of operation:

       The airtest web automated test is based on the python language and the selenium library. By calling the chrome browser’s Devtools Protoco protocol, the user

       The operation is parsed into a python script

 

1. Prepare to install the Chrome browser (the new version is version 89)

      Special note: The chromedriver that comes with airtest does not support the latest version of chrome browser. You need to re-download the chromedriver corresponding to chrome browser to overwrite the version in airtest, otherwise chrome

      Won't start

      Download link: http://chromedriver.storage.googleapis.com/index.html

     

2. Start airtest and set the startup file path of the chrome browser

     Option->Settings->selenium->chromepath set chrome startup path->ok

      

3. Check the selenium window in the airtest window

4. Create a new .air Airtest project

       1) Create a new .air project

        2) Click to start the browser, import the corresponding package and initialization code

Package import and initialization code

5. Windows window operation

1) Windows window connection

        1. Select the game window

                Open the window that is running chrome, and then select the desired box, then the window will be displayed on the window interface

                Frame browser

     After selected, the chrome will be displayed in the device window

        2. Search window

             Searching and selecting the corresponding window is similar to selecting the game window

      3. Other

           Because the underlying implementation of Windows applications is different, some application windows using the default one-click embedding method will encounter some problems after embedding in AirtestIDE (such as unable to operate with the mouse after embedding, unable to embed normally, and unable to display after embedding Images, etc.).

          To solve these problems, we provide a solution without embedded connection. Please find Device- in the setting panel of AirtestIDE, Windows Embed Backup Methodand check it to use an alternative solution to connect to the window.

            The following connection method is the same as the conventional method, click on the selected window , and then select the window program to be tested:

Two, selenium commonly used operations

1. Window operation

 

Web recording cannot simulate key operations, which can be done manually through codes, such as send keys, keys.ENTER, etc.

2. Element operation

   · Clear() Clear the input content of the element

   · Send_keys() simulates keyboard key input

   · Click() simulates mouse clicks on elements

   · Submit() submit the form send keys(Keys.RETURN) is equivalent to enter to log in, if you need to enter Chinese, use sendkeys(u"Chinese user name") to prevent coding errors

 

3. The WebElement interface can get commonly used values:

· Size() Get the size of the element

·Text() Get the text of the element

·Get_attribute(id) to get the attribute value of the element

·Location () to obtain the coordinate value of the element, first find the element to be obtained, and then call the method

·Page_source() returns the source code of the page

· Driver.title() returns the title of the page

·Current_url() Get the url address of the current page

· Is_displayed() sets whether the element is visible

· Is_enabled() determines whether the element is selected

· Is_selected() determines whether the element is selected

·Tag_name() returns the element of tagname

 

4. Common keyboard operations

  • send_keys(Keys.ENTER) press enter
  • send_keys(Keys.TAB) Press Tab
  • send_keys(Keys.SPACE) Press the space bar
  • send_keys(Kyes.ESCAPE) Press the back key Esc
  • send keys(Keys.BACK SPACE) Press the delete key BackSpace
  • send_keys(Keys.SHIFT) 按下shift键
  • send_keys(Keys.CONTROL) Press the Ctrl key
  • send keys(Keys.ARROW DOWN) Press the mouse cursor down button
  • send_keys(Keys.CONTROL,'a') select all Ctrl+A
  • send_keys(Keys.CONTROL,'c') Copy key combination Ctrl+C
  • send_keys(Keys.CONTROL,'x') key combination cut Ctrl+X
  • send_keys(Keys.CONTROL,'v') key combination paste Ctrl+V

 

 

5. Element positioning

 · Find_element_by_id: Determine the id name of the element, find the element directly through id search, and return the first id element found on the page

· Find_element_by_name: Same as id, when determining the name of an element, you can find the locating element directly by name. Similarly, selenium will return the first name element found on the page

· Find element by xpath: xpath can traverse node elements and attributes in xml documents. It is an XML path language. xpath also expands the way to find elements by id/name, that is, if the current element is an id value, xpath will automatically recognize it as ("//*[@id="top-menu"]), and many types such as Input, button, anchor a, iamge, etc. can all be judged, such as ("//button[@data-role='submit']"). The form of xpath is complex and changeable. For the ever-changing content of website pages, xpath can adapt well.

Use the relative path writing method to start with a double slash, and the absolute path starts with a single slash to traverse from the top level of html, as shown in the following figure:

The general format is //tagname[ a href=" https://testerhome.com/attribute ">@attribute='value']/path/path, such as //[@id="J_PmTaskInput"]/div/label, [*] means include all

However, it is not recommended to use absolute paths, because the page may frequently change elements, and slight changes may cause positioning failure; relative path positioning is relatively less likely to change the position relationship, more reliable, so the script is more robust and adaptable .

· Find_elment_by_css_selector: locate the element through the css selector, and return the first matched element on the page.

find_element_by_css_selector("#J_Milestone > div.os-milestone-check > div")
find_element_by_css_selector("button.ui.teal.J_Pay")          #查找tagtype.class.class..
find_element_by_css_selector("div.ui.negative.button")    
#查找所有div标签里面class包含ui、negative、button的元素

Commonly used selectors:

#Indicates id, find the attribute whose id name is J_Milestone, and the next level div of this attribute

. Means class, find the class of the div next to the id as os-milestone-check

* Means all elements,> means the next level

[] means attribute, attribute name, [attribute=value] refers to the attribute of an attribute value

:nth-child(n) indicates which child element of the parent element

The advantage of css selector over xpath is that its performance is better than xpath. In addition, the page layout position sometimes changes, but css selector is relatively more stable, and its name generally does not change frequently.

· Find_element_by_class_name: Determine the class name of the element, you can directly find the locating element through the class name, and the first matching class attribute element of the page will be returned.

· Find_element_by_link_text: This is a very convenient location method for finding hyperlinks. When you know the label text name used by a hyperlink on the page, you can directly search through find element by link text, and the first match on the page will be returned. The anchor tag.

·Find element by partial link text: This is also a method to find hyperlinks.The difference fromfind element by link text is that the former is an accurate search, and the latter is a fuzzy search, which is also a very useful positioning method; the same is true Will return the first matching anchor tag on the page.

 

6, return multiple elements

find elements The method of finding elements is similar to find element, the difference is that find elements will return a list of lists.

  • find_elements_by_name
  • find_elements_by_xpath
  • find_elements_by_link_text
  • find_elements_by_partiallink_text
  • find_elements_by_tagname
  • find_elements_by_classname
  • find_elements_by_cssselector

Three, assertion

A complete test script must include a comparison of whether the test result is consistent with the predicted result, so as to determine whether the script is executed successfully. Airtest provides the following assertion methods:

  • assert_exists
  • assert_not_exists
  • assert_equal
  • assert_not_equal

 

Guess you like

Origin blog.csdn.net/qq_43384897/article/details/115282007