当前位置:网站首页>Automated testing ----- selenium (I)
Automated testing ----- selenium (I)
2022-07-25 20:23:00 【Pinellia and cool】
Catalog
What is automated testing
Automated testing refers to the automation of software testing , Run the application or system in the preset state , Preset conditions include normal and abnormal , Finally, evaluate the operation results . The process of transforming human driven test behavior into machine execution .
Automated testing includes UI automation , Interface automation , Unit test automation .
unit testing
The biggest investment should be in unit testing , Unit tests also run more frequently .java Our unit testing framework is Junit.
Interface automation testing
Interface testing is API test , be relative to UI automation API Automation is easier to achieve , The implementation is also more stable .
Characteristics of interface automatic test :
- In the early stage of the product , Intervene after the interface is completed
- Use case maintenance is small
- Suitable for small interface changes , Items with frequent interface changes
UI automated testing
UI Automated testing is closer to the needs of users and the actual business of software systems . And sometimes we have to UI Layer testing .
UI Features of automated testing :
- The maintenance of use cases is large
- Strong page relevance , You must intervene after the completion of the later project page development
- UI The test is suitable for projects with small interface changes
UI The benefits of Automation :
- Reduce the probability of human error , Create a reliable testing process
- Tedious tests can be carried out ( For example, the test process is consistent , Each time you enter data, it's different ,ddt)
- A lot of repeated tests can be carried out , regression testing
- Can save human resources
- Reusability of scripts
- You can perform tests that are difficult to perform by hand ( Precise timing )
selenium Introduce
Selenium yes web The application is based on UI Automatic testing framework of , Support for multiple platforms 、 Multi browser 、 Multilingual .
Now let's talk about selenium, It usually refers to Selenium2.0. It has a reason Selenium IDE,Webdriver,Selenium Grid form .
webdriver API
First, let's look at a simple automated test script
# coding = utf-8 // Prevent confusion code , There is no need to add , Because the editor defaults to UTF-8 Pattern .
from selenium import webdriver // Import webdriver tool kit , So you can use the API
import time
browser = webdriver.Firefox()
// Get the driver of the controlled browser , Here is get Firefox Of , Of course, you can also get Chrome browser , But to make this code effective , The corresponding browser driver must be installed
time.sleep(3)
browser.get("http://www.baidu.com")
time.sleep(3)
browser.find_element_by_id("kw").send_keys("selenium")
// By element ID Locate the element you want to operate , And input the corresponding text content to the element .
time.sleep(3)
browser.find_element_by_id("su").click()
// By element ID Navigate to element , And click .
browser.quit() // Exit and close the window notes :browser.close() You can also close the window . The difference between the two :close Method to close the current browser window ,quit Method not only closes the window , And I'm going to quit completely webdriver, Release and driverserver Connection between . So to put it simply quit Is more thorough close,quit Will better release resources .
Positioning of elements
principle : No matter how you locate , This method must be globally unique
Common methods of locating elements are :
- id If there is , You can globally and uniquely locate an element
- name Only when it exists and is globally unique can it locate
- class name Only when it exists and is globally unique can it locate
- link text Must be a link , And the linked content is globally unique , To locate
- partial link text Must be a link , And the linked content is globally unique , To locate ( Part of the content of the link can )
- tag name You must be globally unique to locate ( Tag name )
- xpath Any element can be located ( Right click -copy-copyXpath)
- css selector ( Right click -copy-copyselector)
#coding=utf-8
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get("http://www.baidu.com")
######### Positioning method of Baidu input box ##########
# adopt id Way positioning
browser.find_element_by_id("kw").send_keys("selenium")
# adopt name Way positioning
browser.find_element_by_name("wd").send_keys("selenium")
# adopt tag name Way positioning
browser.find_element_by_tag_name("input").send_keys("selenium") Can't succeed , because
input Too many, not the only .
# adopt class name Way positioning
browser.find_element_by_class_name("s_ipt").send_keys("selenium")
# adopt CSS Way positioning
browser.find_element_by_css_selector("#kw").send_keys("selenium")
# adopt xphan Way positioning
browser.find_element_by_xpath("//*[@id='kw']").send_keys("selenium")
# adopt link_text Way positioning
browser.find_element_by_link_text(" Journalism ").click()
############################################
browser.find_element_by_id("su").click()
time.sleep(3)
browser.quit()Operate the test object
webdriver There are several common methods to manipulate objects in :
- send_keys() Send information to the element
- click() Click on the element
- submit() Submit Form
- clear() Clear the content of the element
- text Get the content of the element
Add wait
Fixed waiting sleep()
We need to introduce time package , Fixed waiting must wait enough time in brackets
import time time.sleep(3)
Intelligent waiting
adopt implicitly_wait() Method can easily realize intelligent waiting ;implicitly_wait() The usage of is better than time.sleep()
More intelligent , The latter can only choose a fixed time to wait , The former can wait intelligently within a time range . When the script executes to an element location , If the element can be positioned , Then continue ; If the element cannot be located , Then it continuously determines whether the element is located in the way of polling . Until the set duration is exceeded .browser.implicitly_wait(30) // Intelligent waiting 30 second
Print information
Print title
#coding = utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
print(driver.title) # Put the page title Print out
Print URL
#coding = utf-8
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
print(driver.current_url) # Print urlThe operation of the browser
Browser maximization
browser.maximize_window()
Set browser width and height
browser.set_window_size(width, high) // Numbers are pixels Move the browser forward 、 back off
# Browser advances
browser.forward()
# Browser Back
browser.back()Control the browser scroll bar
# Slide the browser scroll bar to the top
document.documentElement.scrollTop=0
# Slide the browser scroll bar to the bottom
document.documentElement.scrollTop=10000
Keyboard events
To use keyboard keys , Must introduce keys package :
from selenium.webdriver.common.keys import Keysadopt send_keys() Call the key :
send_keys(Keys.TAB) # TAB
send_keys(Keys.ENTER) # enter
send_keys(Keys.SPACE) # Space bar
send_keys(Keys.ESCAPE) # backspace key (Esc)
......
Keyboard combination usage
send_keys(Keys.CONTROL,'a') # Future generations (Ctrl+A)
send_keys(Keys.CONTROL,'c') # Copy (Ctrl+C)
send_keys(Keys.CONTROL,'x') # Clip (Ctrl+X)
send_keys(Keys.CONTROL,'v') # Paste (Ctrl+V)
Mouse events
To use mouse events, you need to import the toolkit :
from selenium.webdriver.common.action_chains import ActionChains#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Chrome() driver.get("http://news.baidu.com") qqq =driver.find_element_by_xpath(".//*[@id='s_btn_wr']") ActionChains(driver).context_click(qqq).perform() # Right click ActionChains(driver).double_click(qqq).perform() # double-click # Locate the original location of the element element = driver.find_element_by_id("s_btn_wr") # Locate the target location to which the element is to be moved target = driver.find_element_by_class_name("btn") # Perform the move operation of the element ActionChains(driver).drag_and_drop(element, target).perform()
边栏推荐
- Fanoutexchange switch code tutorial
- CarSim simulation quick start (16) - ADAS sensor objects of CarSim sensor simulation (2)
- 4. Server startup of source code analysis of Nacos configuration center
- Cloud native guide: what is cloud native infrastructure
- Do you still have certificates to participate in the open source community?
- 股票软件开发
- Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
- [noi simulation] string matching (suffix automata Sam, Mo team, block)
- [today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
- Go language go language built-in container
猜你喜欢

FanoutExchange交换机代码教程
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference
![[cloud native | learn kubernetes from scratch] VIII. Namespace resource quotas and labels](/img/7e/2bdead512ba5bf5ccd0830b0f9b0f2.png)
[cloud native | learn kubernetes from scratch] VIII. Namespace resource quotas and labels

Cloud native guide: what is cloud native infrastructure

9.< tag-动态规划和子序列, 子数组>lt.718. 最长重复子数组 + lt.1143. 最长公共子序列

PreScan快速入门到精通第十九讲之PreScan执行器配置、轨迹同步及非配多个轨迹
![[tensorrt] dynamic batch reasoning](/img/59/42ed0074de7162887bfe2c81891e20.png)
[tensorrt] dynamic batch reasoning

PMP practice once a day | don't get lost in the exam -7.25

Socket error Event: 32 Error: 10053. Connection closing...Socket close
随机推荐
Introduction and construction of consul Registration Center
股票软件开发
网络RTK无人机上机测试[通俗易懂]
JS scope and scope chain
【TensorRT】动态batch进行推理
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
What is cluster analysis? Categories of cluster analysis methods [easy to understand]
Arrow parquet
Apache Mina framework "suggestions collection"
PMP采用最新考纲,这里有【敏捷项目管理】
Fanoutexchange switch code tutorial
[advanced mathematics] [1] function, limit, continuity
【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
【高等数学】【6】多元函数微分学
CarSim仿真快速入门(十五)—CarSim传感器仿真之ADAS Sensor Objects (1)
Notes - record a cannotfinddatasourceexception: dynamic datasource can not find primary datasource problem solving
10. < tag dynamic programming and subsequence, subarray> lt.53. maximum subarray and + lt.392. Judge subsequence DBC
9.< tag-动态规划和子序列, 子数组>lt.718. 最长重复子数组 + lt.1143. 最长公共子序列
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
Cloud native guide: what is cloud native infrastructure