Detailed explanation of Python automated testing tool selenium

Selenium is a commonly used web automation testing tool that supports multiple programming languages ​​and multiple browsers. It can simulate user interaction, automatically execute test cases and generate test reports. Selenium is based on browser-driven implementation, combined with a variety of methods for locating elements, it can realize the testing of various complex web applications

1. Automated testing

Automated testing refers to the automation of software testing, running applications or systems in a preset state, including normal and abnormal conditions, and finally evaluating the running results. Transform human-driven testing behavior into machine-executed process.


Automated testing includes UI automation, interface automation, and unit test automation. Carrying out automated test planning according to this pyramid model can produce the best automated test output-input ratio (ROI), and can obtain good returns with less investment.

1.1 Unit testing

The biggest investment should be in unit testing, and unit testing runs more frequently.

The unit testing framework for java is Junit.

1.2 Interface Test

Interface testing is API testing. Compared with UI automation, API automation is easier to implement and more stable in execution.

Interface automation has the following characteristics:

  • It can be involved in the early stage of the product and after the interface is completed
  • Use case maintenance is low
  • Suitable for projects with small interface changes and frequent interface changes

Common interface automation testing tools include RobotFramework, JMeter, SoapUI, TestNG+HttpClient, Postman, etc.

1.3 UI testing

Although the test pyramid tells us to do as much automated testing at the API layer as possible, the automated testing at the UI layer is closer to the needs of users and the actual business of the software system. And sometimes we have to test the UI layer.

Features of UI Automation:

  • Use cases are heavily maintained
  • The page is highly relevant, and must be involved after the development of the project page is completed
  • UI testing is suitable for projects with small interface changes

There are many testing frameworks for the UI layer, such as AutoIT for Windows client testing, selenium for web testing, and TestPlant, eggPlant, Robot framework, QTP, etc.

1.3.1 Advantages of UI automation testing

Reducing a large amount of manpower input for regression testing caused by changes or multi-phase development of large-scale systems may be the most important task of automated testing, especially when program modifications are frequent, the effect is very obvious, and manpower input in the early stage of automated testing is relatively large. Many, but after entering the maintenance period in the later stage, a lot of manpower can be saved, and a lot of manpower is required for regression testing in the later stage of manual testing

  • Reduce time for retesting and enable fast regression testing
  • Create a good and reliable testing process to reduce human error
  • Can run more and more tedious tests
  • Can perform some tests that would be difficult or impossible to perform manually
  • better use of resources
  • Reusability of test scripts

1.3.2 Applicable objects of UI automation testing

Prerequisites for implementing automated testing: infrequent changes in requirements, long enough project cycles, and reusable automated test scripts.

Suitable for automation projects:

  • Product type project. For product-type projects, the new version is an improvement on the basis of the old version, and the functions of the project have not changed much, but the new and old functions of the project must be repeatedly tested for regression. Regression testing is the strength of automated testing. It can verify whether you have introduced new defects and whether old defects have been modified. To some extent, automated testing tools can be called regression testing tools.
  • Mechanical and frequent testing. Each time you need to enter the same, a large amount of some data, and the cycle of running in a project is relatively long. Such as compatibility testing.

Projects in the following situations are not suitable for automated testing:

  • For projects with frequent changes in requirements, automation scripts cannot be reused, the maintenance cost is too high, and the cost performance is low
  • The project cycle is short, the number of times of use after the automation script is compiled is not much, and the cost performance is low
  • For projects with strong interaction and projects that require manual intervention, automation cannot be implemented

1.4 Automated testing process

  • Analysis: grasp the system logic as a whole, and analyze the core architecture of the system.
  • Design: design test cases, the test cases should be clear and clear enough, and the coverage should be wide and precise
  • Implementation: To implement the script, there are two requirements: one is assertion, and the other is reasonable use of parameterization.
  • Execution: Executing scripts is far from as simple as we thought. Abnormalities during script execution require us to carefully analyze the cause.
  • Summary: The analysis of test results and the summary of the test process are the key to automated testing.
  • Maintenance: The maintenance of automated test scripts is a difficult but necessary problem to solve.
  • Analysis: In the process of automated testing, deeply analyze the coverage risks of automated use cases and the cost of script maintenance.

2、 selenium

Selenium is a UI-based automated testing framework for web applications that supports multiple platforms, browsers, and languages.

The early selenium RC has been replaced by the current webDriver, which can be simply understood as selenium1.0 + webdriver, and now Selenium2.0. Now when we talk about selenium, we generally refer to Selenium 2.0. It consists of Selenium IDE, Webdriver, Selenium Grid.

Make an introduction respectively:

Selenium IDE

Selenium IDE is a complete integrated development environment for Selenium testing, which can directly record user operations in the browser, and can playback, edit and debug test scripts. During the debugging process, you can step by step or adjust the execution speed, and you can browse the log error information at the bottom. The recorded test scripts can be exported in multiple languages, such as java, C#, Python, Ruby, etc., which is convenient for testers who master different languages ​​to operate. Webdrivers

Selenium RC runs JavaScript applications in browsers, and there will be environmental sandbox problems, while WebDriver can jump out of JavaScript sandboxes and create more robust, distributed, cross-platform automated test scripts for different browsers. Based on specific language (Java, C#, Python, Ruby, Perl, JavaScript, etc.) bindings to drive the browser to operate and validate Web elements.

How webdriver works:

  • After starting the browser, selenium-webdriver will bind the target browser to a specific port, and the started browser will serve as the remote server of webdriver.
  • The client (that is, the test script) uses the CommandExecutor to send an HTTP request to the server (communication protocol: The WebDriver Wire Protocol, in the body of the HTTP request, it will tell Selenium with a string in JSON format specified by the WebDriver Wire protocol that we want what the browser does next).
  • The server side needs to rely on native browser components to convert Web Service commands into browser native calls to complete operations.

selenium Grid

Selenium Grid is a server that provides a list of servers accessed by browser instances and manages the registration and status information of each node. It is possible to execute different test scripts on different servers at the same time.

3. Selenium IDE recording script

Open Edge-plugins-select selenium IDE:

Create a new project. At this time, there will be an Untitled test case in the Test Case window on the left. Right-click and rename it to "test"


Click the record button (small red dot) on the upper right of the IDE to start manual recording

Enter the URL to be tested (Zen Tao) such as http://www.baidu.com in the address bar, search for keywords, and you can see that the IDE has performed a recording operation.

Right-click on the page to add a checkpoint.

After the recording is over, click the record button (small red dot) to end this manual recording. In selenium IDE, select a Test Case, right-click and select "Export to export as a test.py file.

Run and debug the script in python.

# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class TestTest():
  def setup_method(self, method):
    self.driver = webdriver.Chrome()
    self.vars = {
    
    }
  def teardown_method(self, method):
    self.driver.quit()
  def test_test(self):
    self.driver.get("https://www.baidu.com/")
    self.driver.set_window_size(809, 864)
    self.driver.find_element(By.ID, "kw").click()
    self.driver.execute_script("window.scrollTo(0,0)")
    self.driver.find_element(By.ID, "kw").send_keys("四月是你的谎言")
    self.driver.find_element(By.ID, "su").click()

So far, we have completed the generation of a test case

This is the end of this article about the detailed explanation of selenium, a powerful tool for Python automated testing. Finally : In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】

insert image description here

Guess you like

Origin blog.csdn.net/m0_53918927/article/details/131313914