[web automated testing]
1, Beginner, Intermediate, Advanced
primary:
Basic use of selenium’s common methods
Basic web automation test case writing
intermediate:
Advanced use of web automation testing skills (display waiting, implicit waiting)
Use case integration screenshots, logs, reports
advanced:
PageObject design pattern
Writing test cases based on pageobject mode
Senior and Expansion:
primary:
Form | Chapter | Description |
---|---|---|
Knowledge points | Web automated testing value and system | Value system technology selection learning route |
Knowledge points | Environment installation and use | selenium, chromedriver, firefox geckodriver |
Knowledge points | Automated use case recording | selenium IDE, recording, playback, basic use |
knowledge points | automated test case structure analysis | recording code analysis, Code structure optimization |
Knowledge points | Web browser control | Open web page, refresh, roll back, maximize, minimize |
Knowledge points | Common control positioning method | id name css xpath link positioning |
Knowledge points | Forced waiting and implicit waiting | Introduction to the three classic selenium waiting methods |
Knowledge points | Common control interaction methods | Click, enter, clear, get element text, size and other attribute information |
Practice | Tester forum search function automation test | Use case design, use case writing, assertion |
intermediate:
Form | Chapter | Description |
---|---|---|
Knowledge points | Advanced positioning-css | Css usage scenarios, syntax |
Knowledge points | Advanced positioning -xpath | xpath usage scenarios, grammar |
knowledge points | explicitly wait for advanced usage | display |
Knowledge points | Advanced control interaction method | Right click, page sliding, form filling and other automatic actions |
Knowledge points | Web page frame and multi-window processing | Multi-window, window recognition and switching under multi-frame |
Knowledge points | File upload pop-up processing | File upload automation and pop-up processing mechanism |
Knowledge points | Automated key data recording | Behavior logs, screenshots, page source |
Practical combat | E-Commerce Product Combat | Use Case Design, Log Encapsulation, Test Report |
Training Camp | Famous Product Web Automation Test actual combat | use case design, log encapsulation, test report |
advanced:
Form | Chapter | Description |
---|---|---|
Knowledge points | Browser reuse | Using remote debugging technology to realize automatic login |
Knowledge points | Cookie reuse | Automatic login by using cookie reuse |
Knowledge points | page object design pattern page object pattern development history Introduction, six design principles | |
Knowledge points | Automatic screenshot of exception | When the test case fails Automatic screenshots |
Knowledge points | Test case process design | Test device application, suite level initialization and cleanup, use case level |
Actual combat | E-commerce product actual combat page object design pattern application, BasePage encapsulation, test case writing based on page object mode | |
Training camp | Advanced practice of web automation testing | page object design pattern application, BasePage encapsulation, based on Writing test cases for page object mode |
Senior:
Form | Chapter | Description |
---|---|---|
Knowledge points | Selenium multi-browser processing | Automatic support for browsers such as chrome and firefox |
Knowledge points | Execute javascript scripts | Use selenium to directly interact with js on the current page |
Knowledge points | Selenium option common operations | Introduction and use of selenium option |
Knowledge points | capability configuration parameter analysis | capability usage, Exclusive capability of firefox chrome and other browsers |
Knowledge points | Cypress test framework introduction | web automation test framework cypress |
2. SeleniumIDE
SeleniumIDE is actually a plug-in on the browser.
Official website: https://www.selenium.dev/
Chrome plugin: https://chrome.google.com/webstore/detail/selenium-ide/mooikfkahbdckldjjndioackbalphokd
Firefox add-on: https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/
SeleniumIDE
Use case recording, use case collection, export script
3. Web browser control
Open web page, refresh, roll back, maximize, minimize, force wait
from selenium import webdriver import time driver = webdriver. Firefox() driver.get("https://www.baidu.com/") #Open Baidu #driver.minimize_window() #The page is minimized driver.maximize_window() #page minimized time.sleep(2) # Forced to wait for 2 seconds driver.refresh() #Refresh the browser, refresh is a method, so add brackets driver.get("https://blog.csdn.net/") #Open CSDN driver.back() #Return to the previous step, that is, return to Baidu sleep(1)
4. Common control positioning methods
(1) html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Tester Forum</title> </head> <body> <a href="https://ceshire.com/" class="link">Link</a> </body> </html>
tags:
Attribute: href
Class attribute: class
(2) Eight positioning methods
https://www.selenium.dev/documentation/webdriver/elements/locators/
practice site
https://vip.ceshiren.com/#/ui_study/
Method | Description |
---|---|
class name | The value corresponding to the class attribute |
css selector (key) | css expression |
id (key) | value corresponding to the id attribute\ |
name (key) | value corresponding to the name attribute |
link text | Find anchor elements whose visible text matches the search value |
partial link text | Finds anchor elements whose visible text contains the search value. If multiple elements match, only the first element will be selected. |
tag name | tag name |
xpath (emphasis) | xpath expression |
1) Element positioning method 1: ID
#Format 1, for example driver.find_element_by_id("su") #Format 2, for example, it is recommended to use format 2 driver.find_element(By.ID, "su")
e.g.
from selenium import webdriver import time from selenium.webdriver.common.by import By def id_position_method(): # instantiate chromedriver driver = webdriver. Chrome() # Open the website driver.get('http://www.ceshiren.com') # wait a second time. sleep(1) # Click on "Essence Post" driver.find_element(By.ID,"ember35").click() # wait two seconds time. sleep(2) if __name__ == '__main__': id_position_method()
2) Element positioning method two: name
driver.find_element(By.NAME,"The value corresponding to the Name attribute")
3) Element positioning method three: css selector (must be mastered)
driver.find_element(By.CSS_SELECTOR, "value") #method one: #Right click on the page, copy absolute positioning #Method 2: #Write css selector expression (must know)
4) Element positioning method four: xpath (must be mastered)
# format driver.find_element(By.XPATH, "value") #method one: #Right click on the page, copy absolute positioning #Method 2: #Write xpath expressions (must know)
5) Element positioning method five: link text
A, must be a label
B. The input element is the text in the label
driver.find_element(By.LINK_TEXT, text inside "a tag")
5. Mandatory waiting, explicit waiting, implicit waiting
(1) Forced waiting (not sure how long it takes to wait, too long sleep time will affect the efficiency of use case execution, and too short sleep time will report an error. It cannot solve the problem of instability)
e.g.
time. sleep(3)
(2) Implicit waiting (Solve the problem that the element cannot be found. Because the forced waiting time is too short, click on the element before the page is loaded, and report the error of no such element)
e.g.
driver.implicity_wait(3) #Poll every 0.5s to find out whether the element appears, and throw an exception if it does not appear
(3) Display waiting (to solve the problem that the element can be found, but the click effect is not triggered. Solve the waiting problem in interactive scenarios such as click.)
e.g.
WebDriverWait(driver, 10).util(exoexted_conditions.element_to_be_clickable((By.ID, "success_btn"))) #10 is the waiting time driver.find_element(By.ID, "success_btn").click()
6. Control interaction method
(1) click
driver.find_element(By.ID, "kw").click()
(2) input
driver.find_element(By.ID, "kw").send_keys("Baidu")
(3) Clear the information in the search box
driver.find_element(By.ID, "kw").clear()
(4) Get element text
driver.find_element(By.ID, "id").text
(5) Get the attribute of the element (html attribute value)
driver.find_element(By.ID, "id").get_attribute("name")
7,
Web pop-up positioning
Scenario 1, web page alert pop-up window
Solution 1, driver.switchTo().alert()
Drop-down box, date control positioning
scene 2,
The drop-down box of the label combination cannot be positioned
The date control of the label combination cannot be positioned
Solution 2. Introduce JS injection technology
File Upload
Scenario 3, input tag file upload
Solution 3. The input tag directly uses the send_keys() method
8, css expression
driver.find_element(By.CSS_SELECTOR, "value") #method one: #Right click on the page, copy absolute positioning #Method 2: #Write css selector expression (must know)
(1) Positioning scene
Support web products
Support webview on app side
(2) Absolute positioning
Right click->copy->css expression
e.g.
$("#ember63 > td.main-link.clearfix.topic-list-data > span > span > a")
Disadvantage: The hierarchical relationship is too clear. When a span is removed from it, the element will not be located. That is, it cannot adapt to changes in the front-end page
(3) Relative positioning
Advantages: strong maintainability, simple syntax, and solve various complex scenarios
$("#ember63 [title='new topic']") #First find the position where the id is ember63, and then find the element whose title is the new topic under it
If the method of $(“css expression”) does not work, you can try to enter $$(“css expression”). (probably due to the browser)
(4) CSS Basic Grammar
#tag name $('input') #.Class attribute value #If the value after class contains spaces, the spaces in the css expression should be replaced with dots. $('.s_ipt') #id attribute value $('#kw') #[attribute name='attribute value'] $('[name="wd"]')
Note:
Modify the variables in pycharm in batches, select – right click – click refactor – click rename
driver->self.driver, realize the use of variables across methods
(5) css relationship positioning
Type | Format |
---|---|
Descendants | element element |
parent and child | element>element |
union | Element, element |
Adjacent brother (just understand) | Element + element |
Brother ( Just understand) | Element 1~Element 2 |
(6) css sequence relationship
type | format |
---|---|
parent-child relationship + order | element element |
parent-child relationship + tag type + order | element element |
e.g.
#:nth-child(n) $('#form>input:nth-child(2)') #:nth-of-type(n) $('#form>input:nth-of-type(1)')
9, xpath
(1) Positioning scene
web automation testing
app automation testing
(2) Advantages
Strong maintainability, concise syntax, and can support more methods than css
(3) Debugging method
console
$x("xpath expression")
elements
#ctrl + f input xpath or css
(4) Basic grammar of xpath
expression | result |
---|---|
/ | Select from the child elements of the node |
// | Select from the descendant elements of the node |
* | Wildcard |
nodename | Select all child nodes of this node |
… | Select the parent node of the current node |
@ | Select attribute |
e.g.
# entire page $x("/") # All child elements in the page $x("/*") # All elements in the entire page $x("//*") # Find all div tag nodes on the page $x("//div") # Find the node whose id attribute is site-logo $x('//*[@id="site-logo"]') # Find the parent node of the node $x('//*[@id="site-logo"]/..')
(5) xpath sequence relationship (index)
xpath directly obtains the corresponding element through the index value
# Get all li elements under this node $x("//*[@id='ember21']//li") # Get the first li element of [all nodes] under this node $x("//*[@id='ember21']//li[1]")
(6) Advanced usage of xpath
[last()]: Select the last one
[@attribute name=’attribute value’ and @attribute name=’attribute value’]: and relationship
[@attribute name=’attribute value’ or @attribute name=’attribute value’]: or relationship
[text()=’text information’]: locate according to text information
[contains(text(),’text information’)]: contains positioning according to text information
Note: All expressions need to be combined with []
e.g.
# Select the last input tag //input[last()] # Select the input tag with the value of attribute name as passward and the value of attribute pwd as 123456 //input[@name='passward' and @pwd='123456'] # Select the input tag whose attribute name value is passward or attribute pwd value is 123456 //input[@name='passward' or @pwd='123456'] # Select all elements whose text information is 'Hogwarts Test Development' //*[text()='Hogwarts Test Development'] # Select all elements of the text packet 'Hogwarts' //*[contains(text(),'Hogwarts')]