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.
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.
Let see view result tree :
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.
This is because of this parts in the code
Now, lets see those individual test results from view result tree.
And, Finally the main sampler :
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.
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.
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..
Let see view result tree :
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.
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…:)
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.
ReplyDeleteThanks for link share..:).
DeleteError Can't find method org.apache.jmeter.samplers.SampleResult.sampleStart(string)
ReplyDeleteThat means, you have stop() in place but no start in begining.
Deletei have a question how to switch to third window in webdriver sampler what does wds.browser.windowhandle return
ReplyDeleteyou have to iterate window handls. See blogs associate to how to handle pop up in selenium. use that code.
DeleteI am getting
ReplyDelete2016/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
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.
DeleteIt is really very helpful, thanks for sharing the above code.
ReplyDeleteHi,
ReplyDeleteI have implemented same for sub sample reuqest but facing below error :
TypeError: WDS.sampleResult.subsampleStart is not a function in
make sure you know how to resolve type error (correct language selection and plugins installation )
DeleteI have updated same sub classes in my script ,facing TypeError: WDS.sampleResult.subSampleStart is not a function in .Please help me out.
ReplyDeletemake sure you know how to resolve type error (correct language selection and plugins installation )
DeleteHey Hi,
ReplyDeleteExcellent 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
can you share your JMX ?
DeleteHi Shantonu, Excellent blog. Very useful and thanks for the same. However when i try to use WDS.sampleResult.subSampleStart("Launch");
ReplyDelete<< 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.
as I have put in update section, you need to add following property in user.properties
Deletewebdriver.sampleresult_class=com.googlecode.jmeter.plugins.webdriver.sampler.SampleResultWithSubs
Hey Shantonu.. Great Blog man..
ReplyDeleteJust 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?
Thanks..
Deleteyour answers :
1. yes, only for debugging. you can also apply LOG after each step
2. yes, if you want separate results, separate those.
Hi Shantonu,
ReplyDeleteWe 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
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