2015-01-06

How to debug steps in Jmeter Webdriver sampler?Jmeter Client side performance testing

In this article we are going to see how we can debug steps written in webdriver sampler.

This will help us finding specific step time as well as fail conditions. That means, if you are making script for long term project, it is must.

To make a good debug-able client side script, we can organize code in two ways.
1. Sub sampling the steps: 
 
In my previous post you know how to active this. (by adding  webdriver.sampleresult_class=true in user.properties). This is not needed for latest edition (plug in version 1.1.3)
So we will see how to divide them in sub samples.

Update : in current jmeter 5.3, the property will be 
webdriver.sampleresult_class=com.googlecode.jmeter.plugins.webdriver.sampler.SampleResultWithSubs

Step 1 : Add WDS.sampleResult.subSampleStart(‘label of the step’) , this will create sub sample with the label you specified.

Step 2: Write your step to be performed in the browser.

Step 3 : Add WDS.sampleResult.subSampleEnd(true) to close this subsample.
Its very simple. Lets apply to the scripts described in previous post. I will every process separate, like this
   1: var selenium = JavaImporter(org.openqa.selenium)
   2: var time = JavaImporter(java.util.concurrent.TimeUnit)
   3: WDS.browser.manage().timeouts().implicitlyWait(30, time.TimeUnit.SECONDS)
   4: WDS.sampleResult.sampleStart()
   5: WDS.sampleResult.subSampleStart('Goto Home Page')
   6: WDS.browser.get('https://www.sumazi.com/')
   7: WDS.sampleResult.subSampleEnd(true)
   8: WDS.sampleResult.subSampleStart('Click About')
   9: WDS.browser.findElement(selenium.By.linkText("About")).click()
  10: WDS.sampleResult.subSampleEnd(true)
  11: WDS.sampleResult.subSampleStart('Click Uses')
  12: WDS.browser.findElement(selenium.By.linkText("Uses")).click()
  13: WDS.sampleResult.subSampleEnd(true)
  14: WDS.sampleResult.subSampleStart('Click Team')
  15: WDS.browser.findElement(selenium.By.linkText("Team")).click()
  16: WDS.sampleResult.subSampleEnd(true)
  17: WDS.sampleResult.subSampleStart('Log In request')
  18: WDS.browser.findElement(selenium.By.linkText("Login")).click()
  19: WDS.browser.findElement(selenium.By.name("username")).clear()
  20: WDS.browser.findElement(selenium.By.name("username")).sendKeys("shantonu_oxford@yahoo.com")
  21: WDS.browser.findElement(selenium.By.name("password")).clear()
  22: WDS.browser.findElement(selenium.By.name("password")).sendKeys("1234567890")
  23: WDS.browser.findElement(selenium.By.linkText("Submit")).click()
  24: WDS.sampleResult.subSampleEnd(true)
  25: WDS.browser.navigate().back()
  26: WDS.sampleResult.subSampleStart('Click Blog')
  27: WDS.browser.findElement(selenium.By.linkText("Blog")).click()
  28: WDS.sampleResult.subSampleEnd(true)
  29: WDS.sampleResult.sampleEnd()

 And if we run, we can see like this from view result tree.

image


This is because of this parts in the code

image

Now, lets see those individual test results from view result tree.

image

image

image

image

image

image
 
And, Finally the main sampler :

image

You might have noticed that those sub samplers do not have size value as it is part of main sampler.

Main, sampler is working like as Transaction which will be shown in table and results.

Benefits :
->You can get time for specific step without doing massive change in code. Just adding those boundary.
->It will not show in any listener table or graph, so your report format will be same. No effect. 

Not Useful for :
-> If you want to measure size of the particular request. As it is part of a bigger request, it dont show specific size of the request. For this, you can follow like other method..

2. Keeping all steps in separate webdriver sampler :

Now, lets divide those steps among multiple webdriver sampler. Like this.

image

Let see Go to Home Page: webdriver sampler  which contains only request to visit home page.

   1: var selenium = JavaImporter(org.openqa.selenium)
   2: var time = JavaImporter(java.util.concurrent.TimeUnit)
   3: WDS.browser.manage().timeouts().implicitlyWait(30, time.TimeUnit.SECONDS)
   4: WDS.sampleResult.sampleStart()
   5: WDS.browser.get('https://www.sumazi.com/')
   6: WDS.sampleResult.sampleEnd()

or Log in request.

image

you might have followed, this is the time start and end. No sub sampling. BTW, you can apply sub sampling here also.

The full JMX file is here from Google Drive Share. Please see details scripts in there.

Now, lets run the script and see the view result tree. I am adding Synthesis Report to have more clear view. Form there..
image

Let see view result tree :

image

image

image

image

image

image
You need to look at this. This step is only for click Back from Browser, so time became Zero as it is not request from application , browser just gets preloaded page.

image

I use this method to write my script from beginning so that others can understand clearly and we can apply particular step specific formulas(like JS run or any function call that applies on that step only).

Benefits :
->We can see individual request size with time
->You can represent multiple request to gather using Transaction Controller like we do for server scripts. In this way, it is easy to measure transaction time.

Should be careful when
->Large Script to maintain
->Have per-defined report format.

Note : For difference in business, you might need to make this more specific on using. I am showing some ways to have more clear on code. These are easy to start. I will post on code style separately.  

Thanks…:)

21 comments:

  1. Thanks for the great guide, much appreciated. In my turn I would recommend checking out The WebDriver Sampler: Your Top 10 Questions Answered guide, it clarifies how to implement the most commonly used actions in WebDriver Sampler's JavaScript language.

    ReplyDelete
  2. Error Can't find method org.apache.jmeter.samplers.SampleResult.sampleStart(string)

    ReplyDelete
    Replies
    1. That means, you have stop() in place but no start in begining.

      Delete
  3. i have a question how to switch to third window in webdriver sampler what does wds.browser.windowhandle return

    ReplyDelete
    Replies
    1. you have to iterate window handls. See blogs associate to how to handle pop up in selenium. use that code.

      Delete
  4. I am getting

    2016/03/29 12:18:39 ERROR - com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler: In file: inline evaluation of: ``var selenium = JavaImporter(org.openqa.selenium) var time = JavaImporter(java.u . . . '' Encountered "var" at line 2, column 1.
    in inline evaluation of: ``var selenium = JavaImporter(org.openqa.selenium) var time = JavaImporter(java.u . . . '' at line number 2

    ReplyDelete
    Replies
    1. hi ajay, your error shows you have issues with line of code, i think separator(;) .. please past your code in comment so that I can see what issues.

      Delete
  5. It is really very helpful, thanks for sharing the above code.

    ReplyDelete
  6. Hi,
    I have implemented same for sub sample reuqest but facing below error :
    TypeError: WDS.sampleResult.subsampleStart is not a function in

    ReplyDelete
    Replies
    1. make sure you know how to resolve type error (correct language selection and plugins installation )

      Delete
  7. I have updated same sub classes in my script ,facing TypeError: WDS.sampleResult.subSampleStart is not a function in .Please help me out.

    ReplyDelete
    Replies
    1. make sure you know how to resolve type error (correct language selection and plugins installation )

      Delete
  8. Hey Hi,

    Excellent detailing done by you. Thanks for all the help !
    Just to take it a bit further, with Apache JMeter 5.1.1 ; the View Results Tree nor any detail honors the WDS.sampleResult.subSampleStart(CustomeLabel).

    Do we have any setting(s) which is to be done specifically here for having these labels in the listeners ? Please suggest.

    Thanks,
    Aashish

    ReplyDelete
  9. Hi Shantonu, Excellent blog. Very useful and thanks for the same. However when i try to use WDS.sampleResult.subSampleStart("Launch");
    << codes>>
    WDS.sampleResult.subSampleStart(true);
    It throws error - ERROR c.g.j.p.w.s.WebDriverSampler: TypeError: WDS.sampleResult.subSampleStart is not a function in at line number 8
    Can you please advise.

    ReplyDelete
    Replies
    1. as I have put in update section, you need to add following property in user.properties

      webdriver.sampleresult_class=com.googlecode.jmeter.plugins.webdriver.sampler.SampleResultWithSubs

      Delete
  10. Hey Shantonu.. Great Blog man..

    Just want to re-confirm:
    1. The 1st option, Sub-samples, can only be used for Debugging as it will not show the sub samples results separately in other listeners than Results Tree??

    2. To Get these sub steps as separate entries in other Listeners (Results Table/Aggregate Report), we need to spilt the steps in separate webdriver samplers?? (2nd Option suggested by you)

    Is my understanding correct?

    ReplyDelete
    Replies
    1. Thanks..

      your answers :

      1. yes, only for debugging. you can also apply LOG after each step
      2. yes, if you want separate results, separate those.

      Delete
  11. Hi Shantonu,

    We are creating client side scripts, in which we are using samples and sub samples. There are lot of instances in the script where we are adding a hard wait of approx. 30 sec. Is there a way where we can remove this hard wait time from the report

    ReplyDelete
    Replies
    1. if you put wait inside webdriver sampler , you cant. But there are some tricks to do. Quick way is to minimize time or deduct time after testing. (browser is an external resource, so time delay is not always working as expected)

      Delete