Monday 20 April 2015

Extract and co-relate last occurrence in Jmeter

To extract last occurrence for co-relating a value without using bean-shell/BSF/JSR223 components use following steps

1. Extract all occurrences using Regular Expression Extractor














     Use "-1" in Match No. to extract all concurrences
     ${cCellId2_matchNr} - contains count of values extracted

2. Consider that cCellId2 contains 5 values, now we need the value of  ${cCellId2_5}, but we cannot get that value by directly substituting  ${cCellId2_matchNr} in place of "5" instead we need to use "${__V()}" function

     a) ${cCellID2_${cCellId2_matchNr}} - wrong

     b) ${__V(cCellId2_${cCellId2_matchNr})} - correct






Wednesday 11 February 2015

Apply Regular Expression on a Jmeter Variable

There are few scripting areas where jmeter has upper hand over other load test tools, below is one such area.

In majority of load test tools you cannot apply a Regexp on a defined variable, in such cases we need to write some string functions which is a tedious job, but in jmeter you can easily achieve it by using Regular Expression Extractor(see below).


Step 1: Creating a jmeter variable by extracting a value to "cExpandContext1"



Step 2: To apply Regexp on above variable
  a) Add a Regular Expression Extractor to a request
  b) Select Jmeter Variable option under "Apply to" and input the variable name(i.e cExpandContext1)
  c) Now add extraction rule as we do for normal extractions (see below)


Thursday 5 February 2015

Ignore 404 errors jmeter

Jmeter by default marks transaction/request as fail when embedded resources(.js, .css. .img...etc) throws 404(file not found) HTTP status code, to mark transaction/request as pass use any of the following 4 methods.

1. Un-comment "httpsampler.ignore_failed_embedded_resources=false" in Jmeter properties file and mark it as true. (This technique ignores 500 errors)


2. Exclude corresponding resource(s) while downloading embedded resources(run-time) by using following Reg-ex.

    Regexp: ^((?!RESOURCE URL).)*$

    Eg:

   HTTP Request Defaults







3. Use Bean-shell assertion when explicitly requesting a resource
   
     Eg:
   
   

Note: Bean-Shell assertion doesn't work when downloading resources during test execution (i.e  when "Retrieve All Embedded Resources" checked on HTTP Request Defaults).  

   
4. Ignore Status
    
   Eg:



Note: It also ignores 5XX errors (eg: 500-Internal Server Error) which is not recommended, so use ignore status option when appropriate. 

Friday 30 January 2015

Call/reuse a Beanshell function jmeter

1. Go to Jmeter bin folder.
2. Open BeanShellFunction.bshrc file.
3. Define a function you want call and save the file.
4. Open jmeter properties file and search for "beanshell.function.init=BeanShellFunction.bshrc"
5. Uncomment this line by removing # symbol at the beginning of the line and save the file.
6. Go to jmeter and select Function helper dialog from Options menu.
7. Select Beanshell from the drop-down (shown below).
8. Write your defined function name and parameter(if any) in the Expression to evaluate (shown below).
9. Click on Generate button.
10. Use the function string generated wherever you want to call the function(shown below).



Tuesday 13 January 2015

Difference between (.*) and (.*?) in Regular Expressions


Quantifiers

  •  (.)   -  matches any single character except newline.
  •  (*)  -  between zero and unlimited times, as many times as possible, giving back as needed  [greedy]
  •  (.*) -  matches zero or more number of characters (except newline)
  • (.*?) - matches any character (except newline)
  • (*?)  - between zero and unlimited times, as few times as possible, expanding as needed [lazy]



Example:

Test String: <xyz>vikram</xyz><abc>ffasdfsaf</abc><xyz>awdfafsd</xyz>safasf

1. Regular Expression: <xyz>(.*)<\/xyz>
    Output: "vikram</xyz><abc>ffasdfsaf</abc><xyz>awdfafsd"

2. Regular Expression: <xyz>(.*?)<\/xyz>
    Output: "vikram"

In the first regular expression the (.*) matches till the end of Test String(i.e (.*) is greedy) and then Reg-ex Engine backtracks to first occurrence of right boundary from the end .

In the second regular expression the (.*?) matches some elements of Test String(i.e (.*?) is lazy) from the beginning and checks for right boundary, if does not exists expands further until right boundary matches.

Use (.*?) instead of  (.*) for efficient extraction.