Automated test steps

Posted by electronish on Wed, 09 Mar 2022 11:19:35 +0100

1, Page element positioning and operation

What are page elements: all elements that can be seen on the browser, such as pictures, text boxes, buttons, drop-down lists, videos, etc.

What is automation? The code controls the elements on the browser and makes them "move" (execute according to the script design logic)

Main steps of automated testing:

1. Locate the object and target we want to execute in some ways

2. What is the command for this object

3. Assign a value to the located element by operation

4. Add assertion action

webdriver provides the following ways to locate elements:

  • id attribute positioning - > find_ element_ by_ id ("id attribute value")

  • Name attribute location - > find_ element_ by_ Name ("name attribute value")

  • class name attribute location - > find_ element_ by_ class_ Name ("id value")

  • link text: —>find_ element_ by_ link_ Text ("linked display text")

  • tag name: —>find_ element_ by_ tag_ Name ("tag name")

  • partial link text:—>find_ element_ by_ partial_ link_ Text ("display text of partial links")

  • xpath: find_element_by_xpath("xpath")

  • css: find_element_by_css_selector("css")

The test uses the id attribute to locate

"""
Demand: Baidu text box input selenium,Click Baidu
1,Baidu textbox 77
2,use Baidu Search
 use ID Attribute positioning:
1,Determine the object to operate on
2,obtain id Attributes( kw/su)
3,Other operations
"""
#Guide the package, create a browser object, and get the url address
from selenium import webdriver
#driver: it's just an ordinary variable, dr is OK
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
#Locate the text box and Baidu by ID
driver.find_element_by_id("kw").send_keys("selenium")
time.sleep(2)
driver.find_element_by_id("su").click()
time.sleep(2)
#Exit browser object
driver.quit()

The positioning method of id attribute is the same as that of name and class attribute. Next, let's talk about using link text and partial link text to locate

Element positioning

#Guide the package, create a browser object, and get the url address
from selenium import webdriver
#driver: it's just an ordinary variable, dr is OK
driver=webdriver.Chrome()
driver.get("https://www.baidu.com")
#Locate the text box and Baidu through the element
driver.find_element_by_link_text("Journalism").click()
time.sleep(5)
driver.find_element_by_partial_link_text("Virus traceability worldwide").click()
#Exit browser object
driver.quit()

Link text needs a complete link title, while partial link text needs a part to locate.

Absolute path location

/: root node

//: relative node

Absolute path corresponding to Baidu text box:

Parent and child nodes use / link (there is an obvious ownership relationship in the tree structure)

The nodes with the same signature are sibling nodes, ranking with [], the eldest is the first [1], the second is added with [2], (the same as the tag name), and the eldest is OK without writing

driver.find_element_by_xpath("/html/body/div/div/div[3]/div/div/form/span/input")

The absolute path is too troublesome and is generally not recommended

Positioning using element attributes

find_element_by_xpath("//input[@id='kw']")

Combination of hierarchy and attribute

find_element_by_xpath("//from[@id='form']/span[1]/input")

Logical operator

find_element_by_xpath("//input[@id='kw' and@class='su']")

css positioning

. represents a class selector

driver.find_element_by_css_selector(".s_ipt").send_keys("selenium")
driver.find_element_by_id("su").click()

#Represents the id selector

driver.find_element_by_css_selector("#su").click()

After locating the element, what can we do?

Clear (): clear text ----- used for clearing after wrong input

send_keys(value): simulate key input. In addition to sending letters, you can also send shortcut keys

Click (): click elements, such as buttons, hyperlinks, radio boxes and check boxes

current_url: returns the url address of the current page

Title: returns the title of the current page

Text: get the text displayed on the page (prompt box, warning box), or the text displayed on some page elements

get_attribute(name): get the attribute value. The value in the text box uses the value attribute name

is_displayed(): set whether the element is visible to the user. The result is true and false

is_enabled(): judge whether it is available

is_selected: to judge whether it is selected, use the check box or radio box to judge

driver.close (): only one window is closed, and the previous one is closed

driver.quit(): close the browser, no matter how many windows the page has

handler: each page has a value, which is unique to a page and an identification of the page

The driver needs to bind the handle. Your driver can only control the page you bind the handle to

Get the handle of the current window: driver current_ window_ handle

1. Open Taobao

2. Click poly cost-effective to enter the poly cost-effective page

3. Select "women's wear" to enter the women's wear page

4. Return to the poly cost-effective page

#Create a browser object, create a browser url, and navigate
from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://www.taobao.com")
#Locate the text box and Baidu by ID
driver.find_element_by_partial_link_text("Cost effective gathering").click()
time.sleep(2)
driver.find_element_by_partial_link_text("Women's wear").click()
time.sleep(2)
driver.back()

The "women's wear" here is from Taobao page, not from the women's wear in jucost-effective

What if you just want to get together and cost-effective women's clothes inside?

Bind the handle in the poly cost to the driver

1. Get a cost-effective handle

driver.window_handle can get all the handles of the open window in the browser

2. Bind handle to driver

#Gets the handle of all open windows
handles=driver.window_handle
#Bind a cost-effective handle to the driver
driver.switch_to_window(handlers[1])

Topics: Selenium