程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python automated testing 27] Web automated testing theory, environment construction and common operations

編輯:Python

List of articles

  • One 、 Preface
  • Two 、Web Detailed explanation of automatic test
    • 2.1 What is? Web automated testing
    • 2.2 selenium Installation and environment construction
      • 2.2.1 Installation foundation dependencies and steps
      • 2.2.2 Webdriver download
      • 2.2.3 Installation success verification
    • 2.3 selenium Basic use
    • 2.4 selenium Element localization
      • 2.4.1 What is element positioning ?
      • 2.4.2 How elements are positioned
      • 2.4.3 Baidu search is realized through element positioning

One 、 Preface

This article will mainly explain Python in Web Basic theory of automated testing 、Webdriver Environment construction and some basic common operations of automated testing , In addition, there is a portal for a series of articles below , It's still being updated , Interested partners can also go to check , Don't talk much , Let's have a look ~

Series articles :
Series articles 1:【Python automated testing 1】 meet Python The beauty of the
Series articles 2:【Python automated testing 2】Python Installation configuration and PyCharm Basic use
Series articles 3:【Python automated testing 3】 First knowledge of data types and basic syntax
Series articles 4:【Python automated testing 4】 Summary of string knowledge
Series articles 5:【Python automated testing 5】 List and tuple knowledge summary
Series articles 6:【Python automated testing 6】 Dictionary and collective knowledge summary
Series articles 7:【Python automated testing 7】 Data operator knowledge collection
Series articles 8:【Python automated testing 8】 Explanation of process control statement
Series articles 9:【Python automated testing 9】 Function knowledge collection
Series articles 10:【Python automated testing 10】 File basic operation
Series articles 11:【Python automated testing 11】 modular 、 Package and path knowledge collection
Series articles 12:【Python automated testing 12】 Knowledge collection of exception handling mechanism
Series articles 13:【Python automated testing 13】 class 、 object 、 Collection of attribute and method knowledge
Series articles 14:【Python automated testing 14】Python Basic and advanced exercises of automatic test
Series articles 15:【Python automated testing 15】unittest The core concept and function of test framework
Series articles 16:【Python automated testing 16】 Test case data separation
Series articles 17:【Python automated testing 17】openpyxl Secondary packaging and data driven
Series articles 18:【Python automated testing 18】 Configuration file analysis and practical application
Series articles 19:【Python automated testing 19】 Log system logging Explain
Series articles 20:【Python automated testing 20】 Construction of interface automation test framework model
Series articles 21:【Python automated testing 21】 Interface automation test practice 1 _ Interface concept 、 Project introduction and test process Q & A
Series articles 22:【Python automated testing 22】 Interface automation test practice II _ Interface framework modification and use case optimization
Series articles 23:【Python automated testing 23】 Interface automation test practice III _ Dynamic parameterization and data forgery
Series articles 24:【Python automated testing 24】 Interface automation test practice IV _Python Operating the database
Series articles 25:【Python automated testing 25】 Interface automation test practice 5 _ Database assertion 、 Interface Association and related management optimization
Series articles 26:【Python automated testing 26】 Interface automation test practice 6 _pytest frame +allure Explain

Two 、Web Detailed explanation of automatic test

2.1 What is? Web automated testing

What is? Web automated testing ? Easy to understand ,Web Automated testing is the replacement of manual testing operations through code or tools , It belongs to simulated manual operation ,Web There are many tools for automated testing , There are many ways , There are mainly selenium,cypress,playwright etc. ,selenium At present, the most mainstream , Use selenium There are also many advantages :

""" selenium The advantages of 1、selenium Rich information , When you have questions and difficulties, you can look up the information at any time 2、selenium It is relatively easy to find a job ,HR Prefer to choose mainstream tools 、 Framework Test Engineer 3、 Support for multiple languages (Java / Python / go / js) 4、 Supports remote , And multiple browsers 5、 The current first choice in the industry , Industry mainstream """

2.2 selenium Installation and environment construction

2.2.1 Installation foundation dependencies and steps

selenium The installation of requires certain dependencies , This is to install selenium For automated testing Necessary condition , There are three main dependencies :

""" selenium Installation dependency : 1、 browser ( Suggest Google Chrome / firefox ) 2、selenium webdriver 3、python binding """
""" selenium Installation steps : 1、pip install selenium 2、 download Webdriver,Chrome The browser downloads through the official download or image , Corresponding to the browser version ( Currently known through testing 71 Version is a very compatible version ) 3、 Put in Python Installation directory or anywhere else """

2.2.2 Webdriver download


Driver download :


Google browser corresponds to the driver version and the download link :

Super easy to use domestic image source :chrom Browser domestic image source

chrome Version history :chrom Version history

Google official version :chrom Official version


Edge The browser corresponds to the driver version and the download link :

Edge browser :Edge browser


IE The browser corresponds to the driver version and the download link :

IE Official version :Github Official version


Safari The browser corresponds to the driver version and the download link :

Safari browser :Safari browser


matters needing attention :

The driver must be compatible with the browser version , For example, the browser version is 71, Then download it. 71 The driver

After downloading, put it into the corresponding Python root directory

2.2.3 Installation success verification

After installation , We need to check whether the installation is successful , You can enter the following code and run , Check whether the installation is successful :

# Import selenium package 
from selenium import webdriver
# open Google browser , Chrome title case 
driver = webdriver.Chrome()

2.3 selenium Basic use

First of all, let's have a brief introduction to selenium The basic usage syntax of , Grammar is fixed grammar , Just remember

# Import selenium package 
from selenium import webdriver
# open Google browser , Chrome title case 
driver = webdriver.Chrome()
# window maximizing , Special attention is needed here , Generally speaking, after we open a browser , The first step is to maximize the window , To prevent the element from positioning failure 
driver.maximize_window()
# Use get Way to request Baidu page 
driver.get("https://www.baidu.com")
# Print the current browser address 
print(driver.current_url)
# Get the page title 
print(driver.title)
# Go to another website 
driver.get("https://www.iqiyi.com")
# back off 
driver.back()
# Forward 
driver.forward()
# Refresh 
driver.refresh()
# Close the current window 
driver.close()
# Close the entire browser and close the driver at the same time chromedriver
driver.quit()

2.4 selenium Element localization

2.4.1 What is element positioning ?

We need to know what elements are before we locate them , Element is a tag customized by the developer for the layout of a web page , We can find a specific precise location by developing the elements of the tag , This process is called element localization , Like using automated software to control input , The first step is to find the input field first , Then input operation can be carried out , Click on execute 、 double-click 、 Before dragging and other simulated manual operations , We all need to locate the elements first

2.4.2 How elements are positioned

stay selenium There are mainly 8 The orientation of big elements , Refer to the following :

""" selenium Eight elements positioning way : 1、id 2、name 3、class name 4、link text 5、partial link text 6、tag name 7、xpath 8、css selector """
""" There are actually 30 A way of positioning , But in most cases , Use this 8 This way can accurately locate """

The way elements are positioned is simple , Take Baidu's input box as an example , There are two main positioning methods , The author has only one way to introduce the screenshot :

""" Mode one : 1、 Press select when entering Baidu page F12 2、 Click the check element arrow , Click the position of the text input box again 3、 View element results -- When selected, there will be a line of light blue or other color special marks in Google browser by default Mode two : 1、 Right click the text input box when entering Baidu page 2、 Choose " Check " Button -- Select to automatically navigate to the corresponding row """

2.4.3 Baidu search is realized through element positioning

Baidu search is realized through element positioning , The specific way to adopt elements needs to be determined according to the actual situation , In this search , Baidu's search box can see id Elements , So we use id Just locate the element , Interested students can also try other ways they have :

""" Realize the idea : 1、 Initialize browser 2、 Open Baidu interface 3、 Locate Baidu text input box 4、 input data 5、 Click Baidu button """
import time
from selenium import webdriver
# Initialize a Google browser 
driver = webdriver.Chrome()
# Open the baidu interface 
driver.get("https://www.baidu.com")
# Maximization window 
driver.maximize_window()
# Through elements id, The value is kw Locate the text input box of Baidu 
input_text = driver.find_element("id", "kw")
# Input text 
input_text.send_keys(" How to marry Bai Fumi to the peak of life ")
# Through elements id, The value is su Position Baidu button 
baidu_button = driver.find_element("id", "su")
# Click on " use Baidu Search " Button 
baidu_button.click()
# Mandatory waiting 3 Second 
time.sleep(3)
# Close the browser 
driver.quit()


All right. ~ The above is all the content shared in this article , Have you learned ? I hope I can help you !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved