Web automation considerations

Posted by danielholmes85 on Thu, 13 Jan 2022 04:37:19 +0100

Like the App automation chapter I released, the first is how to locate our elements, because automation is to simulate the process of manual testing, so you need to test that function point, locate the element first, and then do the corresponding operations. The details are as follows

Common web elements are located as follows:

1. Use xpth to locate. class, ID and name (where id and name are generally unique on the page) are written as follows


-Use the absolute positioning of xpth (this is not recommended. The path is too long and the positioning time is too long)


-Use hierarchical positioning (principle: locate the superior first, and then use the absolute path to locate)


2. Use CSS_ Select to locate, you can observe the unique value of the element you want to locate, and you can directly take it for positioning, CSS_ The location speed of selector is faster than that of xpth, because xpth searches in the whole dom tree. It is written as follows:


3. Use LINK_TEXT some texts inserted with href links can be taken directly for positioning. The writing method is very simple


By mastering the above positioning methods, you can basically deal with most of the work. Of course, positioning is not only the above types, but also a lot of attention: (when selenium and appium are automating, app locates the whole activity page, while selenium searches the whole page)

Here are the pits I encountered and how I solved them:

Pit point 1

-When doing web automation, we will encounter that clicking on an item will pop up a new page. At this time, you can't locate it without doing any processing. You need to do a window switching operation

windows = self.driver.window_handles 
# Switch windows to the last one in the list

Pit point 2

-If there is a frame tag, you must first switch to the frame tag you want to locate before you can locate it successfully


Pit point 3

-When locating the drop-down box, you can use chrome sources. First click the drop-down box to open the drop-down box option, and then use the shortcut key ctrl + \ or F8 to correspond to your mac computer. The same command + \ is the same. Then click the elements option to locate it. This is also a small skill. I have stepped on this small point for a long time

Pit point 4

-When asserting, get the attribute value and use get_attribute, first locate the elements whose expected results are displayed on the page, then execute the test cases, and compare the actual results with the expected results

# Locate the element first
ele = self.find(By.CSS_SELECTOR,"[data-text='true']")
# Then get the attribute value of the element
ele_introduce = ele.get_attribute('data-text')

Pit point 5

-How to upload pictures? Most web pages now have this function. It's impossible not to test this

Generally, with the input tag, we can directly use positioning to locate the element on the page, and then use send_ Just keys, send_ Fill in the full path of the picture in keys, as follows

ps(find_and_send_keys is a method encapsulated by myself. You can use the normal writing method when you look at it)

self.find_and_send_keys(By.XPATH,'//*[@id="courseInfo"]/div[2]/div[2]/div[1]/div/div/span/div/span/input').send_keys( r"/Users/chenhao/PycharmProjects/graduate_design_project/public_class_web/usr_resource/upload_pictures.JPG")

Pit point 6

-There are many ways to use display waiting. My common writing methods are as follows. When we are automating, forced waiting is not recommended. It is only used during code debugging. Generally, display waiting and implicit waiting are used

# The first line of code below
locator = (By.XPATH,"//*[@class='ant-modal-footer']//button[2]")

# The second line of code below

# The third line of code below
  • The first line of code: define a variable locator = (). In the brackets, except: / / * [@ class='ant-modal-footer']//button[2], all other variables are written in a fixed way. You can specify them according to the elements you locate
  • The second line of code: visibility_of_element_located means to know that an element appears
  • The third line of code: then tell the operation you want to do on this element

Topics: Python Front-end Pycharm