0

I have created a script in JMeter using Selenium jar and I have written script in WebDriver sample using Java script language .

Script is working fine

//var pkg2 = JavaImporter(net.lightbody.bmp.BrowserMobProxy);

var pkg = JavaImporter(org.openqa.selenium); //WebDriver classes
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait); //WebDriver classes
var wait = new support_ui.WebDriverWait(WDS.browser, 5000);

WDS.sampleResult.sampleStart(); //captures sampler's start time
WDS.sampleResult.getLatency();
WDS.log.info("Sample started");

WDS.browser.get('http://newtours.demoaut.com/mercuryreservation.php'); //opens website specified in 'http://duckduckgo.com'
WDS.log.info("Sample ended - navigated to flight reservation.com");


var btnView = WDS.browser.findElement(pkg.By.xpath("//div[contains(text(),'Mercury')]"))
WDS.browser.executeScript("arguments[0].click();", btnView)

WDS.log.info(btnView.parameters);
WDS.log.info(btnView.size);
var username=WDS.browser.findElement(pkg.By.xpath("//input[@name='userName']"));

username.sendKeys([WDS.parameters]);

var password=WDS.browser.findElement(pkg.By.xpath("//input[@name='password']"));
password.sendKeys(['mypassword']);


var login=WDS.browser.findElement(pkg.By.xpath("//input[@value='Login']"));
login.click();
WDS.sampleResult.sampleEnd();

Now I Would like to generate HAR file(HTTP Water fall) in JMeter.

Can anyone help me on this ?

  • Please tell why the Jmeter is only generating 1KB file after execution. and when we open the same on LoadRunner, no call are recorded. Regards Gaurav – Gaurav Verma Dec 17 '18 at 6:01
2
  1. Add JSR223 PreProcessor as a child of your WebDriver Sampler
  2. Put the following code into "Script" area

    def proxy = new net.lightbody.bmp.BrowserMobProxyServer()
    proxy.setHarCaptureTypes(net.lightbody.bmp.proxy.CaptureType.getAllContentCaptureTypes())
    proxy.enableHarCaptureTypes(net.lightbody.bmp.proxy.CaptureType.REQUEST_CONTENT, net.lightbody.bmp.proxy.CaptureType.RESPONSE_CONTENT)
    proxy.newHar()
    proxy.start(8888)
    vars.putObject('proxy',proxy)
    
  3. Configure your browser to use the BrowserMobProxy for all traffic

    Selenium BrowserMob JMeter

  4. Add JSR223 PostProcessor as a child of the WebDriver Sampler and out the following code into "Script" area:

    def proxy = vars.getObject('proxy')
    
    proxy.getHar().writeTo(new File("/path/to/your/file.har"))
    proxy.stop()
    

You can learn more about Groovy scripting in JMeter via Apache Groovy - Why and How You Should Use It article.

  • Thanks Dmitri.. This code works but Har file is not generated in proper manner. It always shown 1 KB file after TR executing. – avanish sisodia Feb 5 '18 at 14:11
0

Please share the sample script if you have anything because I tried the above process but unable to generate the HAR file and below is the error message

2019-07-18 11:56:59,920 ERROR o.a.j.t.JMeterThread: Error calling threadStarted org.openqa.selenium.WebDriverException: invalid argument: cannot parse capability: proxy from invalid argument: Specifying 'socksProxy' requires an integer for 'socksVersion'
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 16 milliseconds Build info: version: '2.52.0', revision: '4c2593cfc3689a7fcd7be52549167e5ccc93ad28', time: '2016-02-11 11:22:43' System info: host: 'PC0CRTUU', ip: '10.115.42.113', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_181' Driver info: org.openqa.selenium.chrome.ChromeDriver at sun.reflect.GeneratedConstructorAccessor61.newInstance(Unknown Source) ~[?:?] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_181] at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_181] at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) ~[selenium-remote-driver-2.52.0.jar:?] at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) ~[selenium-remote-driver-2.52.0.jar:?] at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) ~[selenium-remote-driver-2.52.0.jar:?] at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249) ~[selenium-remote-driver-2.52.0.jar:?] at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:131) ~[selenium-remote-driver-2.52.0.jar:?] at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:144) ~[selenium-remote-driver-2.52.0.jar:?] at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:170) ~[selenium-chrome-driver-2.52.0.jar:?] at com.googlecode.jmeter.plugins.webdriver.config.ChromeDriverConfig.createBrowser(ChromeDriverConfig.java:74) ~[jmeter-plugins-webdriver-3.0.jar:?] at com.googlecode.jmeter.plugins.webdriver.config.ChromeDriverConfig.createBrowser(ChromeDriverConfig.java:21) ~[jmeter-plugins-webdriver-3.0.jar:?] at com.googlecode.jmeter.plugins.webdriver.config.WebDriverConfig.getPreparedBrowser(WebDriverConfig.java:233) ~[jmeter-plugins-webdriver-3.0.jar:?] at com.googlecode.jmeter.plugins.webdriver.config.WebDriverConfig.threadStarted(WebDriverConfig.java:220) ~[jmeter-plugins-webdriver-3.0.jar:?] at org.apache.jmeter.threads.JMeterThread$ThreadListenerTraverser.addNode(JMeterThread.java:762) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jorphan.collections.HashTree.traverseInto(HashTree.java:994) [jorphan.jar:5.0 r1840935] at org.apache.jorphan.collections.HashTree.traverse(HashTree.java:977) [jorphan.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.threadStarted(JMeterThread.java:730) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.initRun(JMeterThread.java:718) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:249) [ApacheJMeter_core.jar:5.0 r1840935] at java.lang.Thread.run(Unknown Source) [?:1.8.0_181] 2019-07-18 11:56:59,954 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script5.groovy: 1: unable to resolve class net.lightbody.bmp.BrowserMobProxyServer @ line 1, column 13. def proxy = new net.lightbody.bmp.BrowserMobProxyServer() ^

1 error

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile(GroovyScriptEngineImpl.java:187) ~[groovy-all-2.4.15.jar:2.4.15] at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:215) ~[ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:935) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:537) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.0 r1840935] at java.lang.Thread.run(Unknown Source) [?:1.8.0_181] Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script5.groovy: 1: unable to resolve class net.lightbody.bmp.BrowserMobProxyServer @ line 1, column 13. def proxy = new net.lightbody.bmp.BrowserMobProxyServer() ^

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:981) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:626) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:575) ~[groovy-all-2.4.15.jar:2.4.15] at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323) ~[groovy-all-2.4.15.jar:2.4.15] at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293) ~[groovy-all-2.4.15.jar:2.4.15] at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:279) ~[groovy-all-2.4.15.jar:2.4.15] at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:236) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:335) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile(GroovyScriptEngineImpl.java:185) ~[groovy-all-2.4.15.jar:2.4.15] ... 7 more 2019-07-18 11:56:59,969 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getHar() on null object at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.15.jar:2.4.15] at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_181] at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:221) ~[ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:935) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:537) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.0 r1840935] at java.lang.Thread.run(Unknown Source) [?:1.8.0_181] Caused by: java.lang.NullPointerException: Cannot invoke method getHar() on null object at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:47) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:34) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) ~[groovy-all-2.4.15.jar:2.4.15] at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) ~[groovy-all-2.4.15.jar:2.4.15] at Script6.run(Script6.groovy:3) ~[?:?] at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) ~[groovy-all-2.4.15.jar:2.4.15] ... 9 more 2019-07-18 11:56:59,970 ERROR o.a.j.t.JMeterThread: Error while processing sampler: 'TC01_Purdy_Homepage'. java.lang.IllegalArgumentException: Browser has not been configured. Please ensure at least 1 WebDriverConfig is created for a ThreadGroup. at com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler.sample(WebDriverSampler.java:62) ~[jmeter-plugins-webdriver-3.0.jar:?] at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622) ~[ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546) ~[ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.0 r1840935] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.0 r1840935] at java.lang.Thread.run(Unknown Source) [?:1.8.0_181] 2019-07-18 11:56:59,971 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1 2019-07-18 11:56:59,971 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1 2019-07-18 11:56:59,971 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test 2019-07-18 11:56:59,971 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, local)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.