Is it possible to take a screenshot with JMeter webdriver?

3085 views
Skip to first unread message

ophir....@blazemeter.com

unread,
Jun 26, 2014, 1:48:22 PM6/26/14
to jmeter-...@googlegroups.com
It's obviously possible with webdriver itself, but I couldn't make it work with JMeter webdriver with my limited webdriver skills.


Has anyone done this?
If not, anyone want to take a shot at it? :)

Thanks
Ophir

gli...@gmail.com

unread,
Jun 27, 2014, 9:47:39 AM6/27/14
to jmeter-...@googlegroups.com, ophir....@blazemeter.com
Hi Ophir,

Your link has the answer. Being translated into WebDriver Sampler JavaScript it'll look as follows:

var selenium = JavaImporter(org.openqa.selenium)
var fileUtils = JavaImporter(org.apache.qpid.util)
var io = JavaImporter(java.io)
WDS
.browser.get('http://example.com')
var screenshot = WDS.browser.getScreenshotAs(selenium.OutputType.FILE)
fileUtils
.FileUtils.copy(screenshot, io.File('/path/to/screenshot.png'))

See Using Selenium with JMeter's WebDriver Sampler guide for more information on JavaImporter usage. 

Cheers. 

ophir....@blazemeter.com

unread,
Jun 27, 2014, 10:13:17 AM6/27/14
to jmeter-...@googlegroups.com, ophir....@blazemeter.com, gli...@gmail.com
Thanks for the answer.

I'm still getting an error but it looks like we're close.

Here is my code:

var selenium = JavaImporter(org.openqa.selenium)
var fileUtils = JavaImporter(org.apache.qpid.util)
var io = JavaImporter(java.io)
WDS.browser.get('http://example.com')
var screenshot = WDS.browser.getScreenshotAs(selenium.OutputType.FILE)
fileUtils.FileUtils.copy(screenshot, io.File('/Users/ophir/Downloads/screenshot.png'))

and here is the error:

2014/06/27 10:09:05 ERROR - com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot call method "copy" of undefined (<Unknown source>#6) in <Unknown source> at line number 6 

gli...@gmail.com

unread,
Jun 27, 2014, 10:22:08 AM6/27/14
to jmeter-...@googlegroups.com, ophir....@blazemeter.com, gli...@gmail.com
Hi Ophir,

It looks like that FileUtils isn't in your classpath. That's weird as it indicates a problem with your JMeter installation. However it can be worked around as follows:

Just change the last line of sample code to look like:

screenshot.renameTo(io.File('/Users/ophir/Downloads/screenshot.png'))

Relevant import can also be removed. 

Ophir from BlazeMeter

unread,
Jun 27, 2014, 11:14:04 AM6/27/14
to Dmitri Tikhanski, jmeter-...@googlegroups.com
It works!

Thanks a zillion !!!
--
Ophir Prusak
BlazeMeter The Load Testing Cloud

bruno...@infoglobo.com.br

unread,
May 13, 2016, 12:55:47 PM5/13/16
to jmeter-plugins, ophir....@blazemeter.com, gli...@gmail.com
Your example works, but it had a mistake, you forgot the new keyword:
fileUtils.FileUtils.copy(screenshot, new io.File('/Users/ophir/Downloads/screenshot.png'))

syme...@gmail.com

unread,
Feb 8, 2017, 4:27:50 PM2/8/17
to jmeter-plugins, ophir....@blazemeter.com, gli...@gmail.com, bruno...@infoglobo.com.br
I tried using this 

var pkg=JavaImporter(org.openqa.selenium,org.openqa.selenium.support.ui,org.openqa.selenium.support.ui.Select) //import java selenium packagesPerfTest
var Thr=JavaImporter(java.lang.Thread) //import Thread sleep packages
var wait = new pkg.WebDriverWait(WDS.browser,120) //import WebDriverWait Package
var selenium = JavaImporter(org.openqa.selenium)
var fileUtils = JavaImporter(org.apache.qpid.util)
var io = JavaImporter(java.io)
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)
var conditions = org.openqa.selenium.support.ui.ExpectedConditions
var exception = null 

try
{
    
//wait for the I agree field to be visible
wait.until(pkg.ExpectedConditions.presenceOfElementLocated(pkg.By.xpath('//CHECKBOX[@id="agree-check"]')))
}
catch (err)
{
   WDS.log.error(err.message)
   var screenshot = WDS.browser.getScreenshotAs(selenium.OutputType.FILE)
   screenshot.renameTo(io.File('C:\PONG-PT\screenshot.png'))
  fileUtils.FileUtils.copy(screenshot, new io.File('C:\\PONG-PT\\screenshot.png'))
   exception = err

but doesnt work , get an error  ‘TypeError: null has no such function "copy" in <eval> at line number 97’   

nikhil panchghare

unread,
Aug 26, 2021, 3:03:33 PM8/26/21
to jmeter-plugins
this will work in this way -

screenshot.renameTo(new java.io.File('C:\\Worktime\\Dev_Tools\\screenshot.png'));

Liliane et Robert Bourgault du Coudray

unread,
Aug 31, 2021, 11:11:04 PM8/31/21
to jmeter-...@googlegroups.com
Another simple solution is to save the sampler's response as an html file with the "getResponseDataAsString()" function.
For example:

def myFile = new File("C:\Screenshots/test.html")
myFile.withWriter("utf-8")
{
   writer -> writer.writeLine(prev.getResponseDataAsString())
}

--
You received this message because you are subscribed to the Google Groups "jmeter-plugins" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jmeter-plugin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jmeter-plugins/aa85aee6-e62b-4ecf-96de-0c224a8fb6can%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages