selenium登录脚本java,如何从jmeter运行用Java编写的Selenium脚本?

本文讲述使用Java的Selenium脚本与JMeter的WebDriver Sampler集成时遇到java.net.MalformedURLException错误,而JavaScript测试正常。解决方案是不手动实例化WebDriver,添加Chrome Driver Config元素并配置路径,使用WDS.browser简写,且不调用quit()方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.(URL.java:600)

at java.net.URL.(URL.java:490)

at java.net.URL.(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?

解决方案

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();

8H7wU.png

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值