java接口测试脚本




    4.0.0

    com.atvivo
    MyInterface
    1.0-SNAPSHOT


    
        
        UTF-8

        
        execShell

    

    

        
            org.testng
            testng
            7.0.0
        

        
            org.apache.poi
            poi
            3.10-FINAL
        

        
            org.jsoup
            jsoup
            1.9.1
        

        
            org.apache.httpcomponents
            httpclient
            4.5.2
        

        
            org.apache.httpcomponents
            httpmime
            4.5.2
        
        
            com.jcraft
            jsch
            0.1.54
        

        
            mysql
            mysql-connector-java
            5.1.38
        


        
            commons-beanutils
            commons-beanutils
            1.6
        

        
            commons-collections
            commons-collections
            3.1
        


        
            commons-logging
            commons-logging
            1.1
        

        
            net.sf.ezmorph
            ezmorph
            1.0.6
        

        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        

    

    
        

            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    8
                    8
                
            

            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.19.1
                
                    
                    
                    
                    1
                    1
                    true
                    once
                    -Dfile.encoding=UTF-8
                    ${groupsTest}

                    
                        **/*Test.java
                    
                
            
        
    


 //Java脚本代码

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class NewApiTest {
    CloseableHttpResponse response = null;
    String entityStr = null;

    String url ="http://127.0.0.1:1080/WebTours/index.htm";

    @Test(invocationCount = 10,threadPoolSize=5)
    public void JdLoginTest() throws IOException {
        //获取连接客户端工具
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建POST请求对象
        HttpPost httpPost = new HttpPost(url);
        //添加请求头信息
        httpPost.addHeader("Content-Type","text/html; charset=iso-8859-1");
        httpPost.addHeader("User_Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36");

        ArrayList params = new ArrayList<>();
        try {
            params.add(new BasicNameValuePair("userSession","132742.044147428zicHQVipiQfiDDDDtAQzHpfVDDHf"));
            params.add(new BasicNameValuePair("username","admin"));
            params.add(new BasicNameValuePair("password","admin"));
            params.add(new BasicNameValuePair("login.x","64"));
            params.add(new BasicNameValuePair("login.y","12"));
            params.add(new BasicNameValuePair("JSFormSubmit","off"));
            //打包请求数据
            UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(params, "UTF-8");
            //post请求对象设置body请求体
            httpPost.setEntity(entityParam);
            //http请求对象执行http请求对象
            response = httpClient.execute(httpPost);
            //获取响应对象
            HttpEntity entity = response.getEntity();
            //从响应行获取响应码
            int code = response.getStatusLine().getStatusCode();
            System.out.println("StatusCode:"+code);
            Assert.assertEquals(200,code);
            //使用entityUtils工具对响应数据进行转码,属于表示层
            entityStr = EntityUtils.toString(entity,"UTF-8");
            System.out.println("接口返回结果是:="+entityStr);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(httpClient !=null){
                httpClient.close();
            }
            if(response!=null){
                response.close();
            }
        }
    }
}

你可能感兴趣的:(Java代码,性能测试,自动化测试,java,maven,开发语言)