当前位置:网站首页>UI automation based on Selenium
UI automation based on Selenium
2022-06-24 02:49:00 【luq89】
Catalog
One 、Selenium brief introduction
1、Selenium Introduction to tool components
2、Selenium WebDriver Introduction and implementation principle
Two 、Selenium WebDriver be based on Python Implementation script
1、Selenium Environmental installation
2、Selenium Common operations for manipulating the browser
3、 Write a simple one Selenium Script
3、 ... and 、 Automated testing framework And Architecture design The choice of
1、 be based on Python A popular testing framework for languages
One 、Selenium brief introduction
Selenium It's based on Web A collection of tools for automated testing of applications ,Selenium Test runs directly in browser , The essence is to drive the browser , Simulate user's operation . The main functions of the tool include : Test compatibility with browser , Carry out system function test .
1、Selenium Introduction to tool components
Selenium IDE: It's embedded in FireFox Browser plug-ins , Used in Firefox Record and playback on Selenium Script , Although only in Firefox Next use , But it can convert recorded scripts into various Selenium WebDriver Supported programming languages , Then it can be extended to a wider range of browser types ;
Selenium RC: It mainly consists of two parts :selenium Server+Client Libraries; among Selenium Server Responsible for controlling browser behavior , and Client Libraries It is used to control when writing test cases for testers selenium Server The library of .Selenium RC Also known as Selenium 1 Later and WebDriver Merger brings Selenium 2; stay Selenium 3.0 In the version Selenium RC No longer exists .
Selenium WebDriver: A set for operating the browser API; Can support multiple languages , Support various types of browsers , Cross operating system ,WebDriver Provides complete , Used to implement web Third party libraries for automated testing ;Selenium WebDriver There are currently two versions Selenium 2 and Selenium 3.
Selenium Grid: Is a dynamic tool , It can be used in multiple physical / Distribute and run concurrently in the virtual machine Selenium test . It can significantly speed up the testing process across different browsers , And shorten the test cycle by providing fast and accurate results .

2、Selenium WebDriver Introduction and implementation principle
Selenium WebDriver It is a general testing framework in the industry , Not only is web The standard of testing , At the same time, it is also the underlying core driver framework in the field of mobile testing
characteristic :
1、 Support multi browser operation :Chrome、IE、Firefox、Safaria etc.
2、 Support platform :Windows、Linux、Mac etc.
3、 Support for multiple languages :Python、Java、Ruby、C# etc.
Selenium The process implemented by the back end when the script is executed :
1. For each Selenium Script instructions , Will create a HTTP Request and send to the browser driver
2.WebDriver Launch browser driver , establish HTTP Server, To receive these http request
3.HTTP Server After receiving the request, control the corresponding browser according to the request
4. The browser performs specific test steps , Return the step execution result to HTTP Server
5.HTTP Server And return the result to Selenium Script for , If it's wrong http Code, we will see the corresponding error message on the console

WebDriver Is in accordance with the Client/Server Pattern design
Client: Programming language client ;
Server: Browser driver . It is used to receive requests from clients and drive browsers to perform operations , And then return the result ;
WebDriver Based protocols :JSON Wire protocol
JSON Wire protocol Is in http On the basis of the agreement , Yes http Request and response body Further specification of some data .body Part of the main transmission of specific data , And in WebDriver These data are based on JSON Existing and transmitted in the form of . So in Client and Server Between , As long as it's based on JSON Wire Protocol To transfer data , The driver of the same browser can handle scripts in different languages .
Two 、Selenium WebDriver be based on Python Implementation script
1、Selenium Environmental installation
install selenium
pip install selenium
install WebDriver The driver
The driver needs the corresponding browser version and selenium Version corresponding
WebDriver Drive installation path
Win: Copy webdriver To Python Installation directory
Mac: Copy webdriver To /usr/local/bin Under the table of contents
2、Selenium Common operations for manipulating the browser
Page base operation
Get URL :get(url)
window maximizing :maximize_window()
Minimize the window :minimize_window()
back off 、 Forward 、 Refresh :back()、forword()、refresh()
Close the browser :quit()
Webpage title :title
Webpage URl:current_url
Window handle
Current window handle :current.windows.handles
All window handles :windows.handles
Current page source code :page_source
Take a screenshot of the current page :get_screenshot_as_file(path)
Eight positioning methods of elements
id location :find_element_by_id()
name location :find_element_by_name()
class location :find_element_by_class_name()
link location :find_element_by_link_text()
partial link location :find_element_by_partial_link_text()
tag location :find_element_by_tag_name()
xpath location :find_element_by_xpath()
css location :find_element_by_css_selector()
There are three ways to wait for elements
Mandatory waiting : Commonly used for interaction between systems
time.sleep()
The recessive waiting : Global settings , Timeout can be set ; Can only be used to wait for elements
implicitly_wait()
Explicit wait : Wait for an element to be clicked 、 so 、 Whether the window is opened ; Each wait needs to be set separately
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)
Three switching modes
Window switch , Window handle
Get window handle :windowhandles[-1]
switch_to_window()
iframe Switch
switch_to.iframe()
Pop up switch toast
switch_to_alert
The mouse operation ( stay webdriver in Mouse operations are encapsulated in ActionChains class )
Click on :click()
perform() : To perform all ActionChains Actions stored in
double-click :double_click()
Drag and drop :drag_and_drop()
hover :move_to_element()
Right click :content_click()
Drop down box operation
Select()
User input 、 Keyboard operation 、 File path
send_keys()
send out js Instructions ( Mainly used to make up for selenium Specific page operations that cannot be done )
Page scrolling ( for example : Slide to the bottom of the page )
Modify element properties
System interactive upload
Win:pywinauto
Cross platform :pyautogui
3、 Write a simple one Selenium Script
Launch the browser
get Open the specified page 、 Perform element positioning and operation
from selenium import webdriver
# Launch Google browser
driver = webdriver.Chrome(executable_path=r"/Users/bytedance/Desktop/chromedriver")
# Set hidden wait
driver.implicitly_wait(5)
# Open the web page CSDN
driver.get("https://www.csdn.net/?spm=1001.2101.3001.4476")
# location " Input box "
input_box = driver.find_element("xpath",'//*[@id="toolbar-search-input"]')
# Input text :"Selenium Automated testing framework "
input_box.send_keys("Selenium Automated testing framework ")
# location " Search for " Function and click
driver.find_element("xpath",'//*[@id="toolbar-search-button"]/span').click()
# Close the browser
driver.quit()Use selenium The premise of locating page elements is to understand the basic HTML Page layout and various label meanings , adopt 【F12】 Developer tools , Locate and get the elements we need from the page source code ;
Shortcut : After finding the element , You can right click. Copy Element positioning expression .

3、 ... and 、 Automated testing framework And Architecture design The choice of
1、 be based on Python A popular testing framework for languages
The encapsulation of public methods and the design of how to organize scripts and configuration files are called frameworks .
Unittest: Python Bring their own , The most basic unit testing framework
Nose: be based on Unittest Development , Easy to use , There are many plug-ins
Pytest: Also based on Unittest Development , Easy to use , More details , There are many plug-ins
Robot Framework: Based on a Python Keyword Driven Testing Framework for language , There is an interface , Functional perfection , Bring your own report and log Clear and beautiful
On the whole ,Unittest More basic , Convenient secondary development , Suitable for experts ;Pytest/Nose More convenient and quick , More efficient , Suitable for Xiaobai and companies pursuing efficiency ;Robot Framework Due to the interface and beautiful report , Easier to use , The flexibility and customizability are slightly poor .

2、PO Design patterns
For a good framework , What is indispensable is the idea of hierarchy , And in the Web UI Automated testing ,PO Pattern (Page Object) Is a very mainstream technology ;PO Design pattern provides a pattern of separating page element positioning and business operation process . When page elements change , You only need to maintain the corresponding page Layer modification positioning , There is no need to modify the business logic code .
PO The core idea is layering : Realize script reusability , Maintainability , Readability , Extensibility ; There are three main layers :
Object library layer :Base( Base class ), Some common methods for encapsulating pages , Such as initialization method 、 Find element method 、 Click the element method 、 Input method 、 Get text method 、 Screenshot method, etc .
The operational layer :page( Page object ), Encapsulates operations on elements , Encapsulate a page into an object .
The business layer :business( The business layer ), Combine one or more page operations to complete a business function test . For example, login. : Account number required 、 password 、 Click login to perform three operations .
Based on the idea of layering and PO Design patterns , We can design a basic framework model :
cases Test case layer : Store all test cases
common Public level : Store some public methods , Such as encapsulation page Page base class 、 Capture logs, etc
datas Test the data layer : Store test data
logs Log layer : Store all captured logs and error logs , It's easy to locate the problem
pages Page object layer : Store all page objects , A page is encapsulated into an object
reports Test report layer : Store the output test result data , Screenshot of failure
run Use case execution layer : Store test execution files
requirements.txt: Record all dependent packages of the current project and their exact version numbers , For subsequent migration projects .
边栏推荐
- 2022-2028 global marine clutch industry research and trend analysis report
- SAP retail characteristic profile I
- Wkwebview audio and video media playback processing
- Start tcapulusdb process
- Using the database middleware MYCAT to realize read-write separation (dual master and dual slave)
- MySQL Cases-MySQL 8.0.26 bug ERROR 1064 (42000) at line1: You have an error
- AI intelligent analysis is innovated again. In the future, the technical reserve of the security industry will become the top priority
- How to choose the appropriate configuration server?
- How to understand EDI requirements of trading partners
- Is your database "cloud native"?
猜你喜欢

2022-2028 global cancer biopsy instrument and kit industry research and trend analysis report

2022-2028 global indoor pressure monitor and environmental monitor industry research and trend analysis report

2022-2028 global portable two-way radio equipment industry research and trend analysis report

2022-2028 global medical modified polypropylene industry research and trend analysis report

2022-2028 global tungsten copper alloy industry research and trend analysis report
Cloudpods golang practice

2022-2028 global medical coating materials industry research and trend analysis report

What is etcd and its application scenarios

2022-2028 Global Industry Survey and trend analysis report on portable pressure monitors for wards

2022-2028 global third-party data platform industry research and trend analysis report
随机推荐
Internal reasons for cloud desktop unable to connect to the server and external reasons for cloud desktop connection failure
Cloud function pressure measurement based on wechat applet
How to understand EDI requirements of trading partners
Crawler series: using API
Tencent cloud temporary secret key scheme - character recognition example
Face recognition using cidetector
PHP verify mailbox format
LeetCode 724. Find the central subscript of the array
Some tips for using uitextview
Grc: GRC interface is mixed with restful API
Building a web site -- whether to rent or host a server
Production line motor monitoring and maintenance - application case of 5g edge calculation for paperboard line motor maintenance
Which is a good human voice synthesis platform? What are the human voice synthesis application scenarios
Deep and shallow copy
Tencent Ding Ke: the mission of Tencent security is to safeguard the beauty of digital
IPhone sending SMS implementation
Implementing an ORM framework against SQL injection with builder mode
Data backup is required for manual upgrade of WordPress website program
Add strikethrough to uilabel
VNC enters the password and goes around for a long time before entering the desktop. Use procmon to locate the reason