bijumanvya
bijumanvya

Reputation: 31

How to call the methods inside a jar in Jmeter-webdriver sampler?

I have few methods inside a jar file- created from eclipse- and I would like to call those methods from my JMeter Webdriver sampler. This is what I did.

My java class:

package com.automation.methods;
 import org.openqa.selenium.*;

 public class testClass{

          public static void openWebApp(WebDriver driver,String url) {
		driver.get(url);
	}
 }

I have created a jar out this from eclipse and copied to JMeter_HOME/lib.

From JMeter-webdriver sampler, I tried to call this method as below:

 var testObj= JavaImporter(com.automation.methods.testClass);
 WDS.sampleResult.sampleStart();
 testObj.openWebApp(WDS.browser,'http://google.com.au');
 WDS.sampleResult.sampleEnd();

But this throws error : sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function openWebApp in object [object JavaImporter]. (#4) in at line number 4

Not sure what I miss here. I tried copying jar file to JMeter_HOME/lib/ext, but no difference in results. Do any one have any idea how to resolve this?

Appreciate your help,

manib.

Upvotes: 0

Views: 2233

Answers (3)

bijumanvya
bijumanvya

Reputation: 31

Thank you all for your reply. I worked based on your inputs and have got a solution by this:

importPackage(com.automation.methods);
var classObj=new testClass();
WDS.sampleResult.sampleStart();
classObj.openWebApp(WDS.browser,'http://google.com.au');
WDS.sampleResult.sampleEnd();

Earlier I tried this but didn't work because of some issue with my package. So I created a fresh new package and that resolved my issue!!!

Upvotes: 0

CharlieS
CharlieS

Reputation: 1452

Depends how you are running code in JMeter? Beanshell; JSR223; etc.

Your jar must be placed in {JMETER_HOME}/lib

In a beanshell you can import a static quite easily..

import com.automation.methods.testClass

You can then call the method from the static class..

testClass.openWebApp(...);

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168072

  1. Correct location for your .jar is lib folder, lib/ext should be used for real plugins
  2. See Using Java From Scripts guide for different options of accessing Java classes from JSR223-compliant scripting languages
  3. See The WebDriver Sampler: Your Top 10 Questions Answered guide for specifics of WebDriver Sampler plugin and using Java classes from it.
  4. In your case openWebApp method is static. Static fields and methods can be accessed from the class object itself. So if you want to call the method from WDS Sampler you need to do it a little bit differently. Update your code as follows:

    var testObj= new com.automation.methods.testClass;
    WDS.sampleResult.sampleStart();
    testObj.openWebApp(WDS.browser,'http://google.com.au');
    WDS.sampleResult.sampleEnd();
    

It should resolve your issue.

Upvotes: 0

Related Questions