Testing (5): What is automated testing, automated testing classification, selenium tools, the first automated testing program

Table of contents

1. What is automated testing

In our daily life, we will see automated faucets, driverless cars, automated hand sanitizers, automated sweeping robots, etc...

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 the thought-driven testing behavior into the process of machine execution; through automated testing, manpower input is effectively reduced, and the quality and efficiency of testing are improved at the same time.

2. Classification of automated testing

Automated testing includes UI automation (interface testing), interface automation, and unit testing automation. According to the following pyramid model for automated testing planning, the best Zigong test output-input ratio (ROI) can be generated with less investment Get good income.

2.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

2.2 UI automated testing

UI automation testing is divided into mobile automation testing and web automation testing . The selenium tools introduced later are also mainly used for web automation testing.

 

In the test pyramid, we are told to do as much automated testing of the API layer as possible, but the automated testing of the UI layer is closer to the needs of users and the actual business of the software;

[Features of UI automation]

  • Use cases are heavily maintained
  • The pages are 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

[Benefits of UI automation]

  • Used for regression testing to reduce manpower input
  • Reduce time for retesting and enable fast regression testing
  • Create a good and reliable testing process to reduce false errors
  • Allows for more tedious testing
  • better use of resources
  • Importance of test scripts

[UI automation testing framework]

There are many testing frameworks for the UI layer, such as AutoIT for Windows client testing, selenium for web testing, and TestPlanteggPlant, Robot framework, QTP, etc.
The following mainly takes the Web UI automated testing framework Selenium as an example for a detailed introduction. Selenium has the following advantages:

  • Free, no need to worry about cracking software
  • Small and exquisite, it is just a package for different languages, and QTP needs to download and install more than one G program.
  • Support multiple languages, familiar with C, java, ruby, python, or all C#, you can complete automated testing through selenium, while QTP only supports VBS
  • Support multiple platforms: windows, linux, MAC,
  • Support multiple browsers: ie, ff, safari, opera, chrome
  • Provides many APIs for testing
  • Supports the execution of distributed test cases, which can distribute test cases to different test machines for execution, which is equivalent to the function of a distribution machine
     

[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:

  1. 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.
  2. 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:

1. For projects with frequent changes in requirements, the automation script cannot be reused, the maintenance cost is too high, and the cost performance is low. 2. The
project cycle is short, and the number of uses after the automation script is compiled is not many, and the cost performance is low.
3. The project with strong interaction requires manual labor Intervening items that automation cannot implement

[What skills do you need to know about automated testing?

Understand the basic business of the system under test, understand the basic framework of the business, understand functional testing, understand a programming language, understand databases and operating systems, understand common testing frameworks...

 3.
Introduction of selenium tool 3.1 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 constitutes the current Selenium2.0. Now when we talk about selenium, we generally refer to Selenium 2.0. It consists of Selenium IDE, Webdriver, Selenium Grid.
 

3.2 Environment deployment

What is the required environment to use selenium to implement web automation testing?

  • First of all, you need a browser, I choose Chrome browser here
  • ChromeDriver: Driver for Google Chrome
  • selenium-toolkit

【What is a browser driver?

For a computer, it can drive the computer and equipment to work.
For automation, the script code written by selenium cannot directly open the browser, which requires the driver to open the browser.

 

The driver is equivalent to a server

3.3 Common methods of selenium

  • Find page elements findElement

 

 

  • Find a list of elements with the same label/attribute findElements() method

  •  The method of locating elements, through the methods provided in the By class

 

  • xpath method
  • grammar: 

For the use of the xpath method, just copy XPath in the page as the parameter of the method 

4. The first automated test case

Step 1: Create a Maven project and introduce selenium dependencies

 Step 2: Create packages and java files in the test directory

Step 3: Write code in Test1 to achieve the goal of "searching for Di Lieba in the Baidu search box" 

package com.autotest;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test1 {
    //百度网址搜索关键词"迪丽热巴"
    public  void dilirebaTest() throws InterruptedException {
        ChromeDriver chromeDriver=new ChromeDriver(); //创建一个驱动实例
        Thread.sleep(3000);
        chromeDriver.get("https://www.baidu.com"); //输入百度网址,访问百度首页
        Thread.sleep(3000);
        //找到搜索输入框元素,并输入关键字"迪丽热巴"
        chromeDriver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
        Thread.sleep(3000);
        //找到"百度一下"按钮并点击
        chromeDriver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        chromeDriver.quit(); //关闭浏览器
    }
}

Step 4: Start the program

package com.autotest;

public class RunAutoTest {

    public static void main(String[] args) throws InterruptedException {

        Test1 test1=new Test1();
        test1.dilirebaTest();
    }
}

[A simple automation example mainly consists of the following steps]

  1. Create a driver instance, create a session
  2. visit website
  3. find element
  4. operating elements
  5. end session

рекомендация

отblog.csdn.net/lzz718719/article/details/130683707