2016-11-03 32 views
1

我正在創建一個jmeter scritp,並且我遇到的scnerio如下所示: 首先嚐試使用密碼1登錄到應用程序,如果登錄失敗,請嘗試使用密碼2,如果登錄以password2失敗,然後結果應該是失敗,否則通過。 我創建了一個HTTP請求和Password1,password2是用戶定義的變量。JMeter如何使用兩個密碼驗證登錄

+0

當您提交了錯誤的密碼,不服務器返回403錯誤代碼?或在HTML正文中帶有錯誤消息的200個成功代碼?你的設計取決於它。 –

+0

@Naveen當我們提交錯誤的密碼時,它會返回200個正文中帶有錯誤消息的成功代碼。我使用了regEx Extractor來獲取錯誤消息並將其存儲在參考中。 –

回答

0

有兩個請求Req_1和Req_2,並使用每個密碼的「登錄」操作一個進行配置。使用'正則表達式提取器'或其他來解析對Req_1的響應,並使用'If Controller'查看它是否通過,並僅在Req_1登錄失敗時提交Req_2。

+0

我也這麼做。我在Req_1中使用了正則表達式提取器,並將合格或失敗狀態存儲在名爲「StatusCode」的引用變量中。然後在控制器查看是否通過時使用,然後僅當Req_1失敗時才提交Req_2。這是很好的選擇。 –

0

添加BeanShell的採樣測試計劃,並添加以下代碼(給定網站的工作代碼):

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.CloseableHttpResponse; 
import java.util.List; 
import java.util.ArrayList; 


HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost("http://httpbin.org/post"); 
List nvps = new ArrayList(); 
nvps.add(new BasicNameValuePair("username", "vip")); 
nvps.add(new BasicNameValuePair("password", "secret")); 
httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 
HttpResponse response = httpclient.execute(httpPost); 
String responseString = EntityUtils.toString(response.getEntity()); 
String responseCode = String.valueOf(response.getStatusLine().getStatusCode()); 

String errorMessage = "secret"; 

if(responseString.contains(errorMessage)){ 
    log.info("first time failed"); 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost("http://httpbin.org/post"); 
    List nvps = new ArrayList(); 
    nvps.add(new BasicNameValuePair("username", "vip")); 
    nvps.add(new BasicNameValuePair("password", "secret")); 
    httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 
    HttpResponse response = httpclient.execute(httpPost); 
    String responseString = EntityUtils.toString(response.getEntity()); 
    String responseCode = String.valueOf(response.getStatusLine().getStatusCode()); 
    if(responseString.contains(errorMessage)){ 
     log.info("second time failed"); 
     IsSuccess = false; 
     ResponseCode = "403"; 
     ResponseMessage = "Login failed with both the passwords"; 
    } 
} 
return responseString; 

上方添加腳本「腳本」文本區域在BeanShell的採樣。

圖片參考: enter image description here

注:更換網址,用戶名,密碼和錯誤信息按您的要求。如果關鍵,請添加任何標題

如果您想使用HTTP採樣器,請按照@SaiMatam建議的過程進行操作。

  1. 添加如果控制器,比較是否接收到錯誤消息(在條件字段寫入條件)。
  2. Keep the same sampler (change only the password) inside If Controller。只有條件滿足時,該採樣器纔會執行。
  3. 添加響應斷言爲If Controller中的採樣器。 Add the error message in "Pattern to test" fieldselect "Not" checkbox in "Pattern Matching Rules"。這將確保如果收到錯誤消息,第二個請求被JMeter標記爲FAIL(我們檢查響應不應包含給定的字符串/模式),否則將其標記爲PASS。

參考文獻:

  1. http://jmeter.apache.org/usermanual/component_reference.html#If_Controller
  2. http://jmeter.apache.org/usermanual/component_reference.html#Response_Assertion
+0

@Sai做同樣的事情。我在Req_1中使用了正則表達式提取器,並將合格或失敗狀態存儲在名爲「StatusCode」的引用變量中。然後在控制器查看Req_1是否通過時使用,然後僅當Req_1失敗時才提交Req_2。這是很好的,但我想要的是在結果樹列表中,應該只有一個登錄請求應該是通過或失敗。如果其中任何一個密碼正常工作,應該通過。我不熟悉Beanshell和JSR223後處理器,並想知道是否有可能使用這些處理器。我知道核心的Java。 –

+0

您可以嘗試使用BeanShell Sampler並編寫Java代碼以使用HTTPClient庫發送HTTP Post請求。我會在完成後發佈答案。 –

+0

使用BeanShell取樣器構建GET請求的參考資料位於http://stackoverflow.com/a/36398252/2575259。你可以建立在這個基礎上。 –