java自定義jar包讓jmeter使用---給java參數化

上一篇文章中,提到怎麼生成jar包讓jmeter使用,此次咱們來試試作參數,由於發現調包的時候其實更多仍是參數化,那麼開始改造吧java

1.在httpclientpost這個類中替換參數,且打印參數

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

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;

public class httpclientPost {
	
	public static int sendpost(String email,String value) throws ClientProtocolException, IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost post = new HttpPost("https://user.zaful.com/m-users-a-act_sign.htm?pipeline=zf");
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", value));
		post.setEntity(new UrlEncodedFormEntity(params));
		CloseableHttpResponse response2 = httpclient.execute(post);

		int code = response2.getStatusLine().getStatusCode();
		HttpEntity http = response2.getEntity();

		InputStream instreams = http.getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(instreams));
		StringBuffer buffer = new StringBuffer();
		String line;
		while ((line = reader.readLine()) != null) {
			buffer.append(line + "br ");
		}
		reader.close();
		System.out.println("email:   "+email +" password:"+value);
		System.out.println(code +" respones:"+buffer);
		
		return code;
	}

}

在addcomputerInfo中實現getDefaultParameters方法,這步很重要apache

//參數化方法
	@Override
	public Arguments getDefaultParameters() {  
		// TODO Auto-generated method stub
	
		Arguments arguments=new Arguments();
		arguments.addArgument("email","");
		arguments.addArgument("value","");
		return arguments;
	}
	public SampleResult runTest(JavaSamplerContext arg0) {
		// TODO Auto-generated method stub
		SampleResult result=new SampleResult();
		result.sampleStart();  //事務啓動
		try {   //參數化方法的使用
		int responeCode=httpclientPost.sendpost(arg0.getParameter("email"),arg0.getParameter("value"));
			if (responeCode==200) {
				result.setSuccessful(true);
			}else {
				result.setSuccessful(false);
			}
		} catch (ClientProtocolException e) {
			result.setSuccessful(false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			result.setSuccessful(false);
			e.printStackTrace();
		}
		result.sampleEnd();//事務關閉
		return result;
		
		
	}
	public static void main(String[] args) {
		new addcomputerInfo().runTest(new JavaSamplerContext(new Arguments()));
	}

3、從新打包app

四:jmeter使用ide

能夠看到已經加載了兩個參數,咱們給下面參數作個參數化,增長一個計數器post

 

 增長計數器,每次遞增1,每次填充00013d

 

使用:code

 

 

 運行,由於都是沒有註冊的用戶,因此都是錯誤orm

 

 

相關文章
相關標籤/搜索