[software test report] Selenium+TestNG comprehensive exercise

[software test report] Selenium+TestNG comprehensive exercise

Student No.: 04191315

Name: He Xiang

College: Computer College

Major: Software Engineering

Full code: https://github.com/He-Xiang-best/Software-Quality-Assurance-and-Testing

1, Test requirements

1.1 test module

Login function module

1.2 test contents

  • Use [Selenium+Java + database] for data-driven test, and do login function test for the Web project built by yourself

  • Use [Selenium+Java+Excel] for data-driven test, and do login function test for the Web project built by yourself

  • Use [Junit] to unit test the web program developed by yourself to realize simple addition, deletion, query and modification operations

1.3 test cases

Field nameDescription
identifier UC1
Test itemLogin function
designerHe Xiang
Test environment requirementsNormal connection with the server; Software: Chrome browser version 96 or above, jdk1 8 +, maven related dependencies and TestNG related packages
test methodManual test; Black box test; White box test
Enter description(1) Click the login link (2) fill in the login information, in which the login information of "user name" and "password" must be consistent with the data stored in the database information and not empty (3) click the login button
Output standardPrompt information on the interface: (2) successful login; (2) specific prompt shall be given when the input information does not meet the requirements; (3) if login fails, the specific reason for login failure will be displayed. Jump to relevant pages
special requirementsEnter the background login page
Dependencies between use casesnothing

2, Test design idea

2.1 Selenium+Java + database and Excel

2.2 Junit unit test

3, Test code (core part)

3.1 Selenium+Java + Database

    @Test(dataProvider="getDatabaseData")
    public void webTestByUseDatabase(String username , String password){
        openChrome(url);
        chromeDriver.findElement(By.name("username")).sendKeys(username);
        chromeDriver.findElement(By.name("password")).sendKeys(password);
        chromeDriver.findElement(By.xpath("//input[@type='submit']")).click();
        assertEquals(FileRoot.getUsername(), username);
        assertEquals(FileRoot.getPassword(), password);
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        chromeDriver.quit();
    }

    @DataProvider(name = "getDatabaseData")
    public Object[][] getDatabaseData() throws ClassNotFoundException, SQLException {
        //Read database file information
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection(FileRoot.getUrl(),FileRoot.getSqlUserName(),
                FileRoot.getSqlPassword());
        Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = s.executeQuery("select * from user ");
        int total = 0;
        while (rs.next()) {
            total++;
        }
        Object[][] data = new Object[total][2];
        rs.beforeFirst();
        int a = 0;
        while (rs.next()) {
            data[a][0] = rs.getString("user_name");
            data[a][1] = rs.getString("user_pwd");
            a++;
        }

        return data;
    }

3.2 Selenium+Java+Excel

    @Test(dataProvider="getExcelData")
    public void webTestByUseExcel(String username , String password){
        openChrome(url);
        chromeDriver.findElement(By.name("username")).sendKeys(username);
        chromeDriver.findElement(By.name("password")).sendKeys(password);
        chromeDriver.findElement(By.xpath("//input[@type='submit']")).click();
        assertEquals(FileRoot.getUsername(), username);
        assertEquals(FileRoot.getPassword(), password);
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        chromeDriver.quit();
    }

    @DataProvider(name = "getExcelData")
    public Object[][] getExcelData(){
        //Read Excel file data
        List<Map<Integer, String>> list = EasyExcel
                .read(FileRoot.getExcelPath())
                .sheet()
                .doReadSync();
         Object[][] data = new Object[list.size()][];
        int row=0, column;
            for (Map<Integer, String> map : list) {
                data[row] = new Object[map.size()];
                column=0;
                for (String value : map.values()) {
                    data[row][column] = value;
                    column++;
                }
                row++;
        }
        return data;
    }

3.3 Junit unit test

package com.study;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.springboot.entity.User;
import com.study.springboot.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;

@SpringBootTest

class SpringbootApplicationTests {

/*
 * @author: He Xiang
 * @date: 2021/10/6 0:56
 * @description: security Security access test
 */
    @Test
    public void contextLords(){
        PasswordEncoder pe = new BCryptPasswordEncoder();
        String encode = pe.encode("123");
        System.out.println(encode);
        boolean matches = pe.matches("123",encode);
        System.out.println(matches);
    }

/*
 * @author: He Xiang
 * @date: 2021/10/6 0:55
 * @description: mybatis-plus Database testing
 */

 @Autowired
 UserService userService;

  @Test
  public void query() {
    //System.out.println(userService.getById(10));
    System.out.println(userService.list(null));

  }

  @Test
  void insert() {
    User user = new User();
    user.setUserName("Li Si");
    user.setUserPwd("456");
    System.out.println(userService.save(user));
    System.out.println(user.getUserId());
  }

  @Test
  void delete() {
    System.out.println(userService.removeById(11));
  }

  @Test
  void update() {

    System.out.println(userService.update(new UpdateWrapper<User>().lambda()
            .set(User::getUserPwd, "223").eq(User::getUserId, 11)));

  }

  @Test
  void page() {
    IPage<User> iPage = new Page<>(1,2);
    IPage<User> page = userService.page(iPage);
    List<User> records = page.getRecords();
    System.out.println(records);
    System.out.println(page.getPages());
  }


}

4, Test data

5, Data analysis

5.1 test run analysis

When the user name is emptyWhen the password is emptyThe user name and password are empty
Fill in both user name and passwordWrong user name or passwordThe user name and password are correct

The test items run as follows:

5.2 test condition category

Test situationinput dataexpected valueactual value
case1Enter the wrong user name and the correct passwordEnter page 404Enter page 404
case2Enter the correct user name and the wrong passwordEnter page 404Enter page 404
case3Enter the wrong user name and passwordEnter page 404Enter page 404
case4Do not enter the user name, enter the correct passwordPrompt "please fill in this field" in the user name columnPrompt "please fill in this field" in the user name column
case5Enter the correct user name without a passwordPrompt "please fill in this field" in the password columnPrompt "please fill in this field" in the password column
case6No user name, no passwordPrompt "please fill in this field" in the user name columnPrompt "please fill in this field" in the user name column
case7Enter wrong user name, no passwordPrompt "please fill in this field" in the password columnPrompt "please fill in this field" in the password column
case8Do not enter the user name, enter the wrong passwordPrompt "please fill in this field" in the user name columnPrompt "please fill in this field" in the user name column
case9Enter the correct user name and passwordLog in successfully and enter the background home pageLog in successfully and enter the background home page

5.3 test data report

The test report generated by TestNG is as follows:

5.4 test data details

6, Test summary

6.1 problem analysis

In this test, there is no problem in the general effect of the Web project built by ourselves, but many problems have been encountered in the test process, as analyzed as follows:

Question 1:


Problem 1 Analysis:

The mybatis plus framework is used here, and the relevant methods provided by the framework are used to query the database, but the data cannot be found, null pointer exceptions are reported, and the framed code is no problem. It can be tested successfully in junit unit test. I don't know whether TestNG supports it after using the framework, and if so, what improvements can be made. This problem was encountered in the test, and there was almost no relevant answer to the online query.

Then consider the error reporting reasons:

1. Because the test project is a springboot project, dependency injection and other problems are not solved in the process of using TestNG. There may be other requirements or code addition and other problems that need to be improved.


Therefore, I do not need to automatically rely on assembly injection, but directly use the new object to call the method. The result is still not good, and the same error is still reported

When it comes to this knowledge, we'll find out later whether there are really relevant problems. (question 2, too)

2. When using TestNG, there are other use requirements or code addition problems with mybatis plus framework support that need to be improved. So I don't apply mybatis plus framework. I use native jdbc to encapsulate in external methods to call and query. However, I also report the same error and can't query the data. After checking on the Internet, I also encounter relevant problems:


At present, there is no clear key problem and solution to this problem, but the solution is to put the code connecting to the operation database directly in @ DataProvider, and it can work normally, so it is the part given in the above code.

Question 2:

When asserting the address, the assertion cannot be completed due to problems such as automatic assembly dependency injection.

Similar to the first question in question 1, it involves this knowledge. Later, we will find out whether there are really relevant problems and how to solve them. Therefore, the scheme of this assertion was cancelled during the test.

In addition to some problems encountered in the test learning process, the test project itself also needs to be improved, but the final arrangement is tight, so we can make some improvement later.

6.2 points to be improved

Because the tested project uses the spring security security framework, when the password and user name are filled in and the login fails, the security framework will control the jump. Therefore, during the test, the front and back ends do not use asynchronous interaction. For example, ajax can query the exact login failure information. For example, the user does not exist and the user exists but the login password is wrong. At present, the project directly jumps to page 404 under the control of the security framework. This part will be modified and improved later.

6.3 operation summary

Through this experiment, I have a further learning and understanding of software testing. I can find solutions to the problems encountered in the testing, learn more knowledge, and apply the learned knowledge to practice. I can consciously learn relevant professional knowledge, and I believe I will continue to improve the knowledge learning in relevant aspects in the future, Apply knowledge better.

Keywords: Java Selenium

Added by MrOnline on Wed, 05 Jan 2022 02:46:29 +0200