Automated testing selenium--webdriver common API s

Posted by ojsimon on Sat, 29 Jan 2022 03:04:59 +0100

Automated testing selenium (2) – webdriver common API s

Composition of simple script

1. Import the required package in the script header;

2. Get the driver of the browser;

3. Use browser driver to operate the text system to be tested;

4. Close the browser after the test.

Common API s of webdriver:

1. Element positioning: object positioning is the core of automatic testing. The premise of operating an object should first identify the object.

webdriver provides a series of object location methods. (no matter which method is used, the uniqueness of this attribute on the page must be guaranteed)

  • id: globally unique. You must be able to uniquely locate an element
  • name: it may not be globally unique, and it may not be located
  • class name: it may not be globally unique, and it may not be located
  • link text: not necessarily globally unique, it may not be located
  • partial link text: it may not be globally unique, and it may not be located
  • tag name: not necessarily globally unique. It may not be located
  • xpath: you must be able to locate an element
  • css selector: Style
2. Operation test object

webdriver provides the following common methods for manipulating objects

  • Click: click the object
  • send_keys: simulate key input on the object
  • Clear: clear the contents of the object
  • Submit: submit
  • Text: used to get the text information of the element
3. Add wait
  • Fixed wait: time Sleep (5) -- only a fixed waiting time can be selected
  • Intelligent waiting: browser implicitly_ Wait (30) -- wait intelligently within a time range. If it exceeds the time range, an exception will be thrown
4. Print information

Print title and URL:

print driver.title # print out the page title
print driver.current_url # print url

5. Browser operation

Browser maximization: driver maximize_ window()

Set browser width and height: driver set_ window_ size(480,800)

Operate the forward and backward of the browser: driver forward() driver. back()

Control browser scroll bar:

  • Drag the page scroll bar to the bottom

    js="var q=document.documentElement.scrollTop=10000"
    driver.execute_script(js)

  • Move the scroll bar to the top of the page

    js="var q=document.documentElement.scrollTop=0"
    driver.execute_script(js)

6. Keyboard events

from selenium.webdriver.common.keys import Keys # needs to import the keys package

Keyboard key usage:

Via send_keys() call key:
send_keys(Keys.TAB) # TAB
send_keys(Keys.ENTER) # enter

#The positioning of the tab is equivalent to clearing the default prompt information of the password box, which is equivalent to clear()
driver.find_element_by_id("account").send_keys(Keys.TAB)

#You can also locate the login button and use enter instead of click
driver.find_element_by_id("login").send_keys(Keys.ENTER)

Keyboard combination key usage

#ctrl+a select all input box contents
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')

#ctrl+x cuts the contents of the input box
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')

7. Mouse event

from selenium.webdriver.common.action_chains import ActionChains the ActionChains package needs to be imported

ActionChains class
context_click() # right click
double_click() # double click
drag_and_drop() # drag
move_to_element() # move

ActionChains(driver): generate user's behavior. All actions are stored in the actionchains object. The behavior stored through perform().
move_to_element(menu): move the mouse to an element. The menu has defined which element it points to
perform(): performs all stored actions

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

driver = webdriver.Chrome()
url = "https://www.baidu.com/"
driver.get(url)
driver.find_element_by_id("kw").send_keys("Yang Zi")
driver.find_element_by_id("su").click()
#aaa = driver.find_element_by_xpath("//*[@id='s-top-left']/a[1]")
#ActionChains(driver).context_click(aaa).perform() #Right click
#time.sleep(3)
#ActionChains(driver).double_click(aaa).perform() #double-click
time.sleep(6)
yule = driver.find_element_by_link_text("Na Ying and Yang Zi responded with discord")
ActionChains(driver).move_to_element(yule).perform()
time.sleep(6)
driver.quit()
8. Locate a set of elements

findElements

Usage scenario: batch operation of objects. If all checkbox es on the page are checked;

First obtain a group of objects, and then filter out some objects that need to be located in this group of objects. If you locate all checkbox es on the page, check one of them.

from selenium import webdriver
import time
import os

driver = webdriver.Chrome()
file_path = "file:///"+os.path.abspath("D://Users//Administrator//html//checkbox.html")
driver.get(file_path)

inputs = driver.find_elements_by_tag_name("input")
for input in inputs:
    if input.get_attribute("type") == "checkbox":
        input.click()

time.sleep(3)
driver.quit()
9. Multi layer frame / window positioning
  • switch_to.frame() # multi-layer frame positioning

  • switch_to.window() # window positioning

  • driver.switch_to.default_content() # jumps out of the page embedded in the frame and back to the outermost original page.

    from selenium import webdriver
    import time
    import os
    
    driver = webdriver.Chrome()
    file_path = "file:///"+os.path.abspath("D://Users//Administrator//html//frame.html")
    driver.get(file_path)
    
    driver.switch_to.frame("f1")
    driver.switch_to.frame("f2")
    driver.find_element_by_id("kw").send_keys("angel")
    driver.find_element_by_id("su").click()
    time.sleep(3)
    driver.quit()
    
10. Hierarchical positioning
from selenium import webdriver
import time
import os
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
file_path = "file:///"+"D://Users//Administrator//html//level_locate.html"
driver.get(file_path)

driver.maximize_window()
driver.find_element_by_link_text("Link1").click()

ele = driver.find_element_by_id("dropdown1").find_element_by_link_text("Another action")
ActionChains(driver).move_to_element(ele).perform()

time.sleep(3)
driver.quit()
11. Positioning of drop-down box
from selenium import webdriver
import time
import os

driver = webdriver.Chrome()
file_path = "file:///"+os.path.abspath("D://Users//Administrator//html//drop_down.html")
driver.get(file_path)

driver.find_element_by_id("ShippingMethod")
options = driver.find_elements_by_tag_name("option")
time.sleep(3)
options[2].click()
# for option in options:
#     if option.get_attribute('value') == '9.03':
#         option.click()

time.sleep(3)
driver.quit()
12. Processing of alert, confirm and prompt
  • Textreturns the text message in alert/confirm/prompt

  • Click the confirm button

  • Disass() click the Cancel button, if any

  • send_keys() enter a value in the pop-up box. This alert\confirm cannot be used without a dialog box, otherwise an error will be reported

  • switch_to.alert # gets the handle of the operation pop-up

    from selenium import webdriver
    import time
    import os
    
    driver = webdriver.Chrome()
    file_path = "file:///"+os.path.abspath("D://Users//Administrator//html//alert.html")
    driver.get(file_path)
    
    driver.find_element_by_id("tooltip").click()
    # Get the operation handle of the operation pop-up frame and close the pop-up frame
    alert = driver.switch_to.alert
    alert.accept()
    
    time.sleep(3)
    driver.quit()
    

Enter text in alert:

from selenium import webdriver
import time
import os

driver = webdriver.Chrome()
file_path = "file:///"+os.path.abspath("D://Users//Administrator//html//send.html")
driver.get(file_path)

driver.find_element_by_tag_name("input").click()
time.sleep(3)
alert = driver.switch_to.alert
alert.send_keys("xxx")
# alert. Click OK
alert.dismiss() #Click Cancel
time.sleep(3)
driver.quit()
13. div dialog processing
from selenium import webdriver
import time
import os

driver = webdriver.Chrome()
file_path = "file:///"+os.path.abspath("D://Users//Administrator//html//modal.html")
driver.get(file_path)
# open a dialog box
driver.find_element_by_link_text("Click").click()
# Positioning div1
div1 = driver.find_element_by_class_name("modal-body")
div1.find_element_by_id("click").click()
time.sleep(3)
# Positioning div2
div2 = driver.find_element_by_class_name("modal-footer")
div2.find_element_by_class_name("btn").click()
# Positioning button
buttons = div2.find_elements_by_tag_name("button")
buttons[0].click()

time.sleep(3)
driver.quit()
14. Upload file operation

Locate the upload button through send_keys adds the local file path.

from selenium import webdriver
import time
import os

driver = webdriver.Chrome()
file_path = "file:///"+os.path.abspath("D://Users//Administrator//html//upload.html")
driver.get(file_path)
time.sleep(3)
# Locate the upload button and add the local file path
driver.find_element_by_tag_name("input").send_keys("C:\\Users\\Administrator\\Desktop\\New folder\\USB flash disk\\New folder\\screenshot\\a.PNG")
time.sleep(3)
driver.quit()

Topics: Selenium software testing Testing WebDriver