I am creating a jmeter scritp and the scnerio where i am struck is as follows: First Try login to an application with Password 1, if login fails then try with password 2, if login fails with password2 aswell then the result should be fail otherwise pass. I have created a HTTP request and Password1, password2 are user defined variables.
1
votes
when you submit the wrong password, does server return 403 error code? or 200 success code with an error message in the body of HTML? your design depends on it.
- Naveen Kumar R B
@Naveen When we submit wrong password it returns 200 success code with an error message in body. I have used regEx Extractor to get the error message and have stored it in a reference.
- Ashish Mathur
2 Answers
0
votes
0
votes
Add BeanShell Sampler to the Test Plan and add the following code (working code for the given website):
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;
Add above script to "Script" text area in the BeanShell sampler.
Note: replace the URL, username, password and error message as per your requirements. Add any headers if crucial.
If you want to use HTTP Sampler, follow the process as suggested by @SaiMatam.
- Add If controller, compare whether the error message is received (write condition in Condition field).
Keep the same sampler (change only the password) inside If Controller. this sampler would get execute only if the condition is satisfied.- Add Response Assertion for the sampler in If Controller.
Add the error message in "Pattern to test" field.select "Not" checkbox in "Pattern Matching Rules". This will make sure that, if the error message is received, the second request is marked as FAIL by JMeter (we are checking that the response should not contain the given string/pattern), otherwise will mark it as PASS.
References:
