Search code examples
javaseleniumjmeterbeanshell

JMETER Beanshell ( java.lang.String ) not found in class


I am developing a Jmeter beanshell script to work with Selenium. I decided to use the beanshell sampler so i can use java and selenium commands not accessible in the WDS interface.

My code work fine except for some String inputs. Here is a sample (torn down to barebones):

import org.openqa.selenium.chrome.ChromeOptions;

ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");

and i get this error

2017/03/31 13:43:21 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``import org.openqa.selenium.chrome.ChromeOptions; debug();  ChromeOptions options . . . '' : Error in method invocation: Method addArguments( java.lang.String ) not found in class'org.openqa.selenium.chrome.ChromeOptions'  2017/03/31 13:43:21 WARN  - jmeter.protocol.java.sampler.BeanShellSampler: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.openqa.selenium.chrome.ChromeOptions; debug();  ChromeOptions options . . . '' : Error in method invocation: Method addArguments( java.lang.String ) not found in class'org.openqa.selenium.chrome.ChromeOptions'  201

I see this similar in some other commands like sendkeys.

Why is this command not taking my String? I can run the full script and the browser opens file so i know selenium is setup with jmeter. It is just certain commands that use Strings that do this.

Thanks J


Solution

  • Depending on your Selenium client libraries version you may not have this method taking single String as a parameter. Create a new Beanshell Sampler with the following line:

    log.info(javap(org.openqa.selenium.chrome.ChromeOptions));
    

    And look into JMeter console: you will see javap command printing out all the available methods for the ChromeOptions class, for instance for my installation (I have WebDriver Sampler plugin) it outputs the following:

    Class class org.openqa.selenium.chrome.ChromeOptions extends class java.lang.Object

    public boolean org.openqa.selenium.chrome.ChromeOptions.equals(java.lang.Object)

    public int org.openqa.selenium.chrome.ChromeOptions.hashCode()

    public void org.openqa.selenium.chrome.ChromeOptions.addArguments(java.lang.String[])

    public void org.openqa.selenium.chrome.ChromeOptions.addArguments(java.util.List)

    If you use the same Selenium libraries version (2.52.0) as I do you should change this line:

    options.addArguments("start-maximized");
    

    to this one:

    options.addArguments(new String[] {"start-maximized"});
    

    And your script should start working as expected.

    See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information on Beanshell scripting in JMeter tests.