简体   繁体   English

如何在Jmeter-webdriver sampler中的jar中调用方法?

[英]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. 我从eclipse创建的jar文件中没有几种方法,我想从我的JMeter Webdriver采样器中调用这些方法。 This is what I did. 这就是我所做的。

My java class: 我的java课:

 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. 我已经从eclipse中创建了一个jar,并将其复制到JMeter_HOME / lib。

From JMeter-webdriver sampler, I tried to call this method as below: 从JMeter-webdriver采样器,我尝试按以下方式调用此方法:

  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]. 但这会引发错误:sun.org.mozilla.javascript.internal.EcmaError:TypeError:在对象[object JavaImporter]中找不到函数openWebApp。 (#4) in at line number 4 (#4)在第4行

Not sure what I miss here. 不知道我在这里想念什么。 I tried copying jar file to JMeter_HOME/lib/ext, but no difference in results. 我尝试将jar文件复制到JMeter_HOME / lib / ext,但结果没有区别。 Do any one have any idea how to resolve this? 有谁知道如何解决这个问题?

Appreciate your help, 感谢您的帮助,

manib. 马尼布

  1. Correct location for your .jar is lib folder, lib/ext should be used for real plugins .jar的正确位置是lib文件夹,应将lib / ext用于真正的插件
  2. See Using Java From Scripts guide for different options of accessing Java classes from JSR223-compliant scripting languages 有关符合JSR223的脚本语言访问Java类的不同选项,请参见使用Java从脚本指南。
  3. See The WebDriver Sampler: Your Top 10 Questions Answered guide for specifics of WebDriver Sampler plugin and using Java classes from it. 有关WebDriver Sampler插件的详细信息以及使用JavaDriver的Java类,请参阅《 WebDriver Sampler:您的十大问题解答指南》。
  4. In your case openWebApp method is static. 就您而言, openWebApp方法是静态的。 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. 因此,如果要从WDS Sampler调用该方法,则需要做一些不同的操作。 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. 它应该可以解决您的问题。

Depends how you are running code in JMeter? 取决于您如何在JMeter中运行代码? Beanshell; 豆壳; JSR223; JSR223; etc. 等等

Your jar must be placed in {JMETER_HOME}/lib 您的jar必须放置在{JMETER_HOME} / lib中

In a beanshell you can import a static quite easily.. 在beanshell中,您可以轻松导入静态对象。

import com.automation.methods.testClass

You can then call the method from the static class.. 然后,您可以从静态类中调用该方法。

testClass.openWebApp(...);

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!!! 所以我创建了一个新的新程序包,就解决了我的问题!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM