[Selenium automated testing] How to build an automated testing environment and issues that should be paid attention to in the process of building the environment

 

Recently, many people have asked me privately, is it difficult to learn selenium? Most of the learning content for basic introductory is the version data before 3. For those who have never learned it, through the information and then to the code they wrote, they found that some things were not there, and some methods were different, which caused the script to fail to run. Remind me to see if I can spend some time to sort out the content of selenium , Starting from the basics, it is better to be able to sort out a selenium knowledge map.

The whole content may not be written at one time, so I plan to write it in multiple times. If you have any questions after reading it, you can send me a message. I will answer it for you. This is also for the convenience of selenium in the future. There will be a new version update, and it will be convenient for comparison in the future.

Let’s write this content from the following aspects:

  1. The role of selenium
  2. Introduction to selenium version
  3. How selenium works
  4. selenium installation
  5. Basic browser operations
     

1. Introduction to selenium

Selenium is a set of automated testing tools based on web applications developed by Thought Works, which runs directly in the browser and simulates user operations. It can be used for unit testing, integration testing, regression testing, system testing, smoke testing, acceptance testing, and can run on various browsers and operating systems.

At present, there are probably two types of people who use selenium. One is software test engineers. They can use selenium to realize automated testing to improve the efficiency of regression testing and reduce personnel execution costs. The second category may be many people who write crawlers, because there are many people who crawl data on the web page, and the server has done a lot of anti-crawling strategies, and various restrictions such as dynamic loading, etc., crawling data through the interface alone The difficulty and feasibility have been reduced, and selenium can simulate artificially performing various operations on the page, so various anti-crawling methods may be meaningless to it. Of course, some people say that there are various The verification code will also have an impact. Let’s not talk about the verification code today. I will see if I can publish a separate article on the verification code later.
 

2. Introduction to the version of selenium

Selenium has experienced 1, 2, 3, and 4 versions since its release. The latest version released so far is version 4.7. The version with the largest span is actually version 1.0 to version 2.0. Version 1.0 includes ide, Grid, core and The four parts of rc, 2.0 added webdriver on the basis of 1.0, webdriver provides a simpler programming interface, makes up for some deficiencies and limitations of Selenium-RC in 1.0, and provides a more concise restfull API interface externally.

The main content of selenium1.0:

  1. DE: The recording and playback of the test process can be completed through the IDE. It is mainly used for beginners to understand selenium, but it is not suitable for direct daily automated testing.
  2. Grid: It is selenium deployment, testing and execution (distributed test case execution).
  3. RC: selenium Remote Control, an agent and controller.
  4. Core: The core part of selenium's testing mechanism, including the execution of test case sets, assertions, composed of js code, and supports the operation of the Kua platform.
     

selenium2.0 = selenium1.0 + webdriver

3. How selenium works

 

3.1. Client (selenium supports multiple programming languages, so the client's request can use various languages ​​to call the interface), selenium starts the browser driver by calling the start method of the WeDriver class, and creates the startup of the service.
3.2. Through the session id returned by the server, carry this id in the next request, and send the operation to be performed by the client to the server. The server parses the operation in the request sent by the user, and sends the operation instruction to the browser. implement.
3.3. The browser executes the client operation and returns the result of the operation to the server, and the server encapsulates the execution result and returns it to the client.
When the client starts the server, the server will start a 9515 port to connect with the client, the client calls the method provided by selenium, and selenium converts the request into the corresponding operation interface instruction, and calls the corresponding operation instruction API interface, through the API interface call, the user operation is sent to the browser for execution.

The content of this part needs to analyze the code in the follow-up process to understand its principle and process.
 

4. selenium installation

Install the selenium environment, here is python 3.8 as an example, after installing python, and configuring python-related environment variables, if you don’t understand, you can private message me.
What needs to be installed are:
1. selenium
2. browser driver
3. browser

4.1 install selenium

If the python environment has been installed, you can use pip to install selenium. In the start menu, enter cmd, open the dos window, and execute the following command

Click to view the code

4.2 Install browser driver

Browser driver installation needs to be determined according to the browser. To use different browsers, you need to choose a different browser driver. Browser drivers also need to be considered according to the browser version. Corresponding to the driver and driver version, the driver download addresses of several common browsers are listed below 1. Chrome (Google) browser
: http://chromedriver.storage.googleapis.com/index.html
Version, in the help of the chrome browser, check about, the browser version will be displayed above, or directly enter chrome://settings/help on the browser url address, the following is the version of my browser

 

According to the version number, find the corresponding version on the driver download page

 

Because I am a Windows system, I choose chromedriver_win32.zip. After downloading, unzip the chromedriver.exe file inside. In fact, it doesn’t matter where it is placed. The processing when writing code will be different in different places. It is generally recommended to put it in a location that can be accessed by the path environment variable, or simply put it in the python installation directory

2. Firefox (Firefox) browser: Releases · mozilla/geckodriver · GitHub
uses the same method to download the corresponding version of the geckodriver driver. The corresponding relationship between the version of Firefox and the version of geckodriver is as follows. You can also directly go to the official website to view it.
Official website address: Supported platforms — Firefox Source Docs documentation

 3. Edge: Microsoft Edge WebDriver - Microsoft Edge Developer
must first determine the version of Edge, and then download the driver from the official website according to the version

 4.3 Installing the browser
This part of the content will not be repeated. You can directly download and install it from the official website. Here is a reminder for novices that it is best not to modify the installation path during the installation process.

4.4 Verify the installation result
Open pycharm and write the following code. If you can open the corresponding browser and jump to the Baidu page, the installation is successful. Here we take Chrome browser as an example

# 导入webdriver库
from selenium import webdriver
# 启动chrome浏览器
driver = webdriver.Chrome()
# 打开百度页面
driver.get('https://www.baidu.com')

5. Basic browser operations

In the above code, it has been explained how to open the browser and jump to the specified URL address. The following describes the basic operation method of the browser

#coding=utf-8
__author__ = 'Meteor'
from selenium import webdriver
from time import sleep
#打开一个Firefox浏览器
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
#等2秒
sleep(2)
#刷新
driver.refresh()
 
#等2秒
sleep(2)
#后退
driver.back()
#等2秒
sleep(2)
#前进
driver.forward()
#等2秒
sleep(2)
#最小化
driver.minimize_window()
#等2秒
sleep(2)
#最大化
driver.maximize_window()
 
#等2秒
sleep(2)
#关闭浏览器
driver.close()

In addition to the above basic operations, you can also configure the startup of the browser through the Option class

# 导入Option类
from selenium.webdriver.chrome.options import Options
# 实例化一个Option类的对象
options = Options()
 
# 通过Options对象的add_argument方法添加启动的配置
options.add_argument('--window-size=1366,768')  #--window-size=1366,768 表示设置浏览器的大小
# 启动浏览器,并将启动参数带入到浏览器中
driver = webdriver.Chrome(options=options)

Guess you like

Origin blog.csdn.net/lzz718719/article/details/130870051