Interface automation test framework construction

1. Principle and characteristics

  1. Parameters are managed in XML files
  2. Simply encapsulate a httpUtils tool class with httpClient
  3. Test case management uses testNg management, uses TestNG parameterized tests, and executes cases through xml files.
  4. The test report uses a third-party package ReportNG project organization with Maven

2. Preparation

    Tools used: eclipse,
    third-party jar packages used by maven: dom4j, reportng, testng
    Difficulties in understanding: encapsulation of httpUtils and xmlUtil tool classes; dom4j use; application of CookieStore

3. Framework Concept

1. Project structure

2. Use case execution process

3. Interface call process

4. Scheduling script process

 

Fourth, the framework implementation

1. Input parameters

1.1 Parameters are managed in XML files

Example: Here, the input parameters of the interface to test the acquisition of roles are page and rows, and the content of mapRole.xml is as follows

<?xml version="1.0" encoding="UTF-8"?>
<map>
	<bean beanName="GetRole">
		<!--Locator lists -->
		<locator name="page" value="1"></locator>
		<locator name="rows" value="10"></locator>
	</bean>
</map>

1.2 Encapsulate an xmlUtil tool class responsible for reading XML and use the third-party jar package dom4j

1.2.1 The return value of the readXMLDocument method in xmlUtil is HashMap<String, String>

public static HashMap<String, String>  readXMLDocument(String beanName,String xmlName){
    
}

Parameter xmlName (the name of the xml file); parameter beanName (the name of the node in the xml file);

1.3 Encapsulate a CookieUtil tool class to store cookies through CookieStore

1.3.1 The return value of the setCookieStore method in the CookieUtil class is CookieStore

public  CookieStore setCookieStore(HttpResponse httpResponse) {

}

1.4 Simple encapsulation of a httpUtils tool class with httpClient has get.post, put, delete methods

1.4.1 The post encapsulation method in httpUtils is as follows:

public CloseableHttpResponse post(String url, Map<String, String> params,CloseableHttpClient httpclient,CookieStore cookieStore){

}

2. Return parameters

2.1 Create an interface to return the object ResponseBean

Object ResponseBean, including status, statusCode, contentType, body, url, method, cookies

2.2 Create a ReponseUtil tool class in the tool class

The ReponseUtil tool class is responsible for converting the requested return data CloseableHttpResponse into ResponseBean

public ResponseBean setResponseBean(CloseableHttpResponse httpResponse) {

}

3. Test case

Test case management uses testNg management, uses TestNG parameterized tests, and executes cases through xml files

3.1 Test case script

public class GetRoleTest {
    static CookieStore cookieStore ;
    static CookieUtil cookieUtil=new CookieUtil() ;
    CloseableHttpClient client;
    HttpUtils httpUtils=HttpUtils.getInstance();


    @Parameters({ "url", "objBean" ,"statusCode","xmlName"})
    @BeforeSuite

    /*
    * 登录进入系统获取JSESSIONID放入到CookieStore中
    * */
    public  void TestLoginIn(String url ,String objBean, String statusCode,String xmlName) {

        Map<String,String> params=xmlUtil.readXMLDocument(objBean,xmlName);
        client = HttpClients.createDefault();
        CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore);
        //cookieUtil.printResponse(httpResponse);
        cookieStore=cookieUtil.setCookieStore(httpResponse);

    }

    @Parameters({ "url", "objBean" ,"statusCode","body","xmlName"})
    @Test(priority = 2)
    public  void TestGetRole(String url ,String objBean, String statusCode,String body,String xmlName) {
        Map<String,String> params=xmlUtil.readXMLDocument(objBean,xmlName);
        client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore);
        ResponseBean rb=new ReponseUtil().setResponseBean(httpResponse);
//        add Assert
        Assert.assertEquals("OK", rb.getStatus());
        Assert.assertEquals(statusCode, rb.getStatusCode());
        Assert.assertEquals(true, rb.getBody().contains(body));

    }
    @AfterSuite
    public void closeClient(){
        try {
            // 关闭流并释放资源
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • [Note] Because the cookie is checked every time when the API interface is tested, we have to perform the login operation to obtain the cookie every time.

3.2 Writing xml files

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestGetRole" parallel="classes" thread-count="5">
    <parameter name="url" value="/sys/login" />
    <parameter name="objBean" value="loginIn" />
    <parameter name="status" value="OK" />
    <parameter name="statusCode" value="200" />
    <parameter name="xmlName" value="mapRole" />
        <test name="TestGetRole" preserve-order="true">
            <parameter name="url" value="/json/getRoleInfo" />
            <parameter name="objBean" value="GetRole" />
            <parameter name="status" value="OK" />
            <parameter name="statusCode" value="200" />
            <parameter name="body" value="roleName" />
            <classes>
                <class name="com.lc.testScript.GetRoleTest">
                    <methods>
                        <include name="TestGetRole" />
                        <!--<include name="TestGetRole2" />-->
                    </methods>
                </class>
            </classes>
        </test>
</suite>

Right click -> run as -> TestNG Suite, the test cases of this scenario can be run

4. Test report and project organization

The test report uses a third-party package ReportNG project organization with Maven

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
..........................................
..........................................
..........................................
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <xmlFileName1>TestGetRole.xml</xmlFileName>
.................这里写testNG对应的XML名称----------------------
        <xmlFileName10>TestGetUser.xml</xmlFileName>
    </properties>
    <dependencies>
 ..........................
     </dependencies>   
  <build>

       <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/java/testSuites/${xmlFileName}</suiteXmlFile>
                        .................略............
                        ..............这里的和properties中的xmlFileName想对应............
                        <suiteXmlFile>src/test/java/testSuites/${xmlFileName10}</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>

      <!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 -->
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.5</version>
          <configuration>
              <properties>
                  <property>
                      <name>usedefaultlisteners</name>
                      <value>false</value>
                  </property>
                  <property>
                      <name>listener</name>
                      <value>org.uncommons.reportng.HTMLReporter</value>
                  </property>
              </properties>
              <workingDirectory>target/</workingDirectory>
          </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
  • [Note] Because it is a maven project, the xml file of testSuite should be placed in the test directory of maven, so right-click the pom.xml file maven test, and all test cases will be executed.

testing report

Current shortcomings of the framework

  • 1. The function of database data verification has not been perfected, and MyBatis is planned to be used.
  • 2. The xml file configuration is used for the parameters, although it is flexible but a bit cumbersome. I haven't thought of a good solution yet. Can testlink try it?

 

Project source code address: https://git.oschina.net/hellotester/iaf.git

Welcome everyone to pay attention to the WeChat public account and communicate with the QQ group

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325910160&siteId=291194637