How to run selenium scripts written in java from jmeter?

亡梦爱人 提交于 2021-02-05 09:43:48

问题


I am trying to use my Selenium scripts in java with JMeter's WebDriver Sampler.

Inside the webdriver sampler, the language is seleced to java, and the following code added:

package automationFramework;

public class FirstTestCase {

    public static void main(String[] args) {

        // Create a new instance of the Firefox driver
        WebDriver driver = new ChromeDriver();

        //Launch the Online Store Website
        driver.get("www.google.com");

        // Print a Log In message to the screen
        System.out.println("Successfully opened the website www.google.com");

        //Wait for 5 Sec
        Thread.sleep(5);

        // Close the driver
        driver.quit();
    }
} 

I am facing the following error:

java.net.MalformedURLException: unknown protocol: data
at java.net.URL.<init>(URL.java:600)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler.sample(WebDriverSampler.java:80)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:475)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:418)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:249)
at java.lang.Thread.run(Thread.java:745)

However, tests written in javascripts work just fine.

What is the problem here? How to solve it?


回答1:


You should not be instantiating WebDriver instance, JMeter does it for you given you add Chrome Driver Config element to your Test Plan and configure path to the ChromeDriver executable.

Once done you should be able to use WDS.browser shorthand like:

WDS.sampleResult.sampleStart();
WDS.browser.get("http://google.com");
WDS.log.info("Successfully opened the website www.google.com");
Thread.sleep(5000);
WDS.sampleResult.sampleEnd();

Also don't call quit() method, the WebDriver instance(s) will be shut down when test will be finished.

See Using Selenium with JMeter's WebDriver Sampler guide to get started with Selenium and JMeter integration.




回答2:


If you are using Chrome driver 2.28 with Selenium 3.x.x you have to set the path of the Chrome driver before you open the browser.

Add this line: System.setProperty("webdriver.chrome.driver", "C:\\your_folder\\chrome.exe");

Next, WebDriver driver;

Let me know if this helps you.




回答3:


Another way you could do this is extract you webdriver test in a jar file then run in using a junit test in JMeter.



来源:https://stackoverflow.com/questions/43080234/how-to-run-selenium-scripts-written-in-java-from-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!