Use of Selenium, a Web automation testing framework, in Python

Keywords: Python Selenium

1. What is Selenium?

  • Selenium is one of the most widely used open source Web UI (user interface) automation test suites. It was originally developed by Jason Huggins in 2004 as Thought Works Internal tools.
  • Selenium supports automation across different browsers, platforms and programming languages.
  • Selenium can be easily deployed on Windows, Linux, Solaris, Macintosh and other platforms. In addition, it supports the OS (operating system) of mobile applications such as IOS (IOS, Windows Mobile and Android).
  • Selenium supports various programming languages by using drivers specific to each language. Selenium supports languages including C #, Java, Perl, PHP, Python and Ruby.
  • Selenium supports browsers including Internet Explorer, Mozilla Firefox, Google Chrome and Safari.

2. What are its characteristics?

  • Open source software: open source software can add some functions of tools as needed
  • Cross platform: linux, windows, mac
  • Core function: automatic testing can be carried out on multiple browsers
  • Multilingual: Java, Python, C#, JavaScript, Ruby, etc
  • Mature and stable: it has been widely used by google, Baidu, Tencent and other companies
  • Powerful: it can realize most of the functions of similar commercial tools. Because of its open source, it can realize customized functions

3. Test tool WebDriver

  • Webdriver (selenium 2) is an automatic testing tool for Web applications
  • It provides a friendly API
  • Webdriver is a complete set of class libraries and does not depend on any test framework except the necessary browser driver
  • WebDriver supports Firefox, IE, Opera, Chrome, safari and HtmlUnit

4. Browser related test operations

  • maximize_window

    # Maximize browser
    driver = webdriver.Chrome()
  • refresh

    # Refresh browser
    driver = webdriver.Chrome()
  • close

    # Close a browser window
    driver = webdriver.Chrome()
  • quit

    # Close all browser windows
    driver = webdriver.Chrome()
  • title

    # Gets the title of the current browser window
    driver = webdriver.Chrome()
  • current_url

    # Gets the URL of the browser's current window
    driver = webdriver.Chrome()

5. Positioning of HTML elements

  • id

    # Positioning by element ID attribute
  • name

    # Locate through the element name attribute
  • class_name

    # Positioning by element class attribute
  • tag_name

    # Positioning by element tag name
  • link_text

    # Accurately locate the text of the hypertext link element < a > < / a >
  • partial_link_text

    # Fuzzy positioning through the text of the hypertext link element < a > < / a >
  • xpath

    # Positioning by absolute positioning
    # Positioning by relative positioning
    # Positioning by element attributes
    driver.find_element_by_xpath("//*[@name='hobby'] ")
    # Positioning through the combination of hierarchy and attributes
    # Positioning through the combination of attributes and logic
    driver.find_element_by_xpath("//*[@id='password' and @name='password']")
  • css

    # Locate by id
    # Positioning by clss
    # Positioning by element
    # Positioning by attributes
    # Positioning through the hierarchy selector

6. Getting HTML elements

  • send_keys

    # Enter the text of the element
    username = driver.find_element_by_id("username")
  • clear

    # Clear the text of the element
    username = driver.find_element_by_id("username")
  • click

    # Click the element
    button = driver.find_element_by_id("btn")
  • size

    # Gets the size of the element
    username = driver.find_element_by_id("username")
  • text

    # Gets the text of the element
    username = driver.find_element_by_id("username")
  • get_attribute

    # Gets the properties of the element
    img = driver.find_element_by_id("picture")

7. Waiting operation of HTML element

When WebDriver locates a page element, if it is not found, it will wait for the specified time

  • Display wait

    Explicit wait acts on a single element, and the method is encapsulated in the WebDriverWait class

  • Implicit waiting

    Implicit waiting acts on global elements, and the method is called directly through the instantiation object of the browser

    • If the positioning of an element fails, an implicit wait valid length is triggered
    • If the loading is completed within the specified time, continue
    • Otherwise, the NoSuchElementException exception will be thrown. If the element is located for the first time, the implicit waiting time will not be triggered

8. Mouse related test operations

  • context_click

    from selenium.webdriver.common.action_chains import ActionChains
    username = driver.find_element_by_id("username")
    action = ActionChains(driver)
    # Right click
    element = action.context_click(username)
    # implement
  • double_click

    from selenium.webdriver.common.action_chains import ActionChains
    username = driver.find_element_by_id("username")
    action = ActionChains(driver)
    # Double click the mouse
    element = action.double_click(username)
    # implement
  • drag_and_drop

    from selenium.webdriver.common.action_chains import ActionChains
    div1 = driver.find_element_by_id("div1")
    div2 = driver.find_element_by_id("div2")
    action = ActionChains(driver)
    # Mouse drag
    element = action.drag_and_drop(div1, div2)
    # implement
  • move_to_element

    from selenium.webdriver.common.action_chains import ActionChains
    img = driver.find_element_by_id("picture")
    action = ActionChains(driver)
    # Mouse over
    element = action.move_to_element(img)
    # implement

9. Keyboard related test operations

from selenium.webdriver.common.keys import Keys

send_keys(Keys.BACK_SPACE)  #Delete key (BackSpace)
send_keys(Keys.SPACE)       #Spacebar (Space)
send_keys(Keys.TAB)         #Tab
send_keys(Keys.ESCAPE)      #Back key (Esc)
send_keys(Keys.ENTER)       #Enter
send_keys(Keys.CONTROL,'a') #Select all (Ctrl+A)
send_keys(Keys.CONTROL,'c') #Copy (Ctrl+C)

Posted by shinstar on Wed, 06 Oct 2021 17:54:56 -0700