Friday, 14 December 2018

Jmeter - Pass HTTP failed request

While testing negative test cases with Jmeter, it is required to PASS the FAILED HTTP requests. This can be achieved by 2 ways.

  • You can use the Beanshell/Groovy code.
  • You can use the Response Assertion.(looks easy to me)
Here, I will show you how to use Response Assertion to pass the failed HTTP requests.

In case, you want all the HTTP requests to be PASSED, you can use only single Response Assertion in your script. However, if there are cases where some of the HTTP requests, if they get fail, should be marked as FAILED out of many HTTP requests. In that case, you should use Response Assertion element as child of each HTTP request for which you want to mark it PASSED.

Response Assertion can be found here



In order to PASS the FAILING requests, you have the enable the 'Ignore Status' checkbox of Response Assertion element and then use it accordingly. See below screenshot.


Without Response Assertion


With Response Assertion


Response Assertion for Single HTTP request.




Tuesday, 4 December 2018

Handle Dynamic body in Jmeter

The below beanshell code can handle dynamic body of the JMeter requests when used in beanshell preprocessor.


   import org.apache.commons.io.FileUtils;
    int countRoom = Integer.parseInt(vars.get("countRoom"));
    int countChild = Integer.parseInt(vars.get("countChild"));
    String FinalString = "";
    int agecounter = 0;
    List linesChildAge = FileUtils.readLines(new File(vars.get("ChildAgeCsv")));
    List linesRoomAdult = FileUtils.readLines(new File(vars.get("RoomAdultCsv")));
    
    for (int i = 0; i < countRoom; i++) {
    String Adult = linesRoomAdult.get(i);
    FinalString = FinalString + "\r\n          <room adult=\"" + Adult + "\">";
   
    for (int i = 0; i < countChild; i++)
    {
    String Age = linesChildAge.get(agecounter);
    agecounter++;
    FinalString = FinalString + "\r\n               <children>\r\n                    <child age=\"" + Age + "\"/>\r\n               </children>";
    }
    FinalString = FinalString + "\r\n          </room>";
    vars.put("FinalString", FinalString);
    }











Note: If you want my script, drop me an email on rahul225sharma@gmail.com.

Set path environment variable via Powershell

The below code can be used to append path environment variable.

Param 
      (
                  [Parameter(Mandatory=$false)]
                  [System.String]
                  $pathtoAppend = "C:\Tools\java\bin"
        )
$env_Path = [Environment]::GetEnvironmentVariable("Path","Machine")
$finalPath = "$env_Path;$pathtoAppend"
[Environment]::SetEnvironmentVariable("Path",$finalPath ,"Machine")

In the similar way, you can set/Append any environment variables in windows.

Set browser Proxy Script via command prompt

Proxy script is a way to define different Proxies for multiple URL's. In normal cases, you can only define a single proxy in the browser, from where you want the browser traffic to be routed. However, this method fails when you want to use multiple proxies (for some URLs) and no-proxy for other URLs. Hence, PROXY script is helpful in such scenarios.


The below commands can be used to set proxy script in browser for windows.



Via powershell command


Set-ItemProperty -Path 'HKLM:Software\Microsoft\CurrentVersion\Internet Settings' -Name AutoConfigURL -Value 'C:\proxyscript.pac' 


Via windows command prompt


REG ADD "HKLM:Software\Microsoft\CurrentVersion\Internet Settings" /v AutoConfigURL /t REG_SZ /d "C:\proxyscript.pac" /f


Note: you can use the URL of the proxy script(if you have) in the above commands instead of local path.