简体   繁体   English

如何在WebDriver Sampler中设置JMeter Vars?

[英]How to set JMeter Vars from within WebDriver Sampler?

// I had previously used a CSS/JQuery extractor to get a URL from a page and add it to JMeter vars - accessing it here
var pageURL = "${valueFromJmeterVars}";

// navigate to that url
WDS.browser.get(pageURL); 

// selecting an element
var button = wait.until(pkg.ExpectedConditions.visibilityOfElementLocated(pkg.By.cssSelector(buttonLocator)));                                                                                                                                               

// log desired boolean value to console, so I can confirm is as expected
WDS.log.info('reserveASpotButton:' + reserveASpotButton.isEnabled());

// add my boolean to JMeter vars, so I can access later from beanshell post-processor (where I do my assertions)
vars.put("reserveASpotButtonIsEnabled", reserveASpotButton.isEnabled());

The last line above doesn't work. 上面的最后一行无效。

I can successfully use CSS/JQuery Extractor to add values to JMeter vars... 我可以成功使用CSS / JQuery Extractor将值添加到JMeter vars ...

But how can I do the same from within WebDriver Sampler? 但是,如何在WebDriver Sampler中执行相同的操作?

You can access JMeter API classes from within the WebDriver Sampler, it's implemented as JSR 223 standard for instance you can refer JMeter Variables (aka vars as follows) 您可以从webdriver的采样中访问JMeter的API类,它实现为JSR 223标准 ,例如,你可以参考JMeter的变量(又名vars如下)

In the WebDriver Sampler: 在WebDriver采样器中:

var ctx = org.apache.jmeter.threads.JMeterContextService.getContext()
var vars = ctx.getVariables();

vars.put('foo','bar')

Now you have ${foo} variable with the value of bar 现在您有了${foo}变量,其值为bar

See The WebDriver Sampler: Your Top 10 Questions Answered guide for more WDS sampler tips and tricks. 有关更多WDS采样器的技巧和窍门,请参阅《 WebDriver采样器:您的十大问题已回答》指南。

I believe that you need to cast it to String first, as per Using Selenium with JMeter's WebDriver Sampler guide JMeter Variables are basically Strings and you can't put boolean there. 我相信您需要首先将其转换为String,如将Jeleter与JMeter的WebDriver Sampler指南一起使用Selenium一样 。JMeter变量基本上是Strings,并且不能在其中放置布尔值。

just replace 只需更换

vars.put("reserveASpotButtonIsEnabled", reserveASpotButton.isEnabled());

with

vars.put("reserveASpotButtonIsEnabled", reserveASpotButton.isEnabled().toString());

And it should work. 它应该工作。

它不会起作用,因为在WebDriver Sampler中未定义“ vars”,例如在BeanShell Sampler中未定义。

There isn't a clean way to do this, but it is possible. 没有一种干净的方法可以做到这一点,但是有可能。 You can set the response headers in your WebDriver sampler: 您可以在WebDriver采样器中设置响应头:

WDS.sampleResult.setResponseHeaders(reserveASpotButton.isEnabled())

Then you can use a regular expression extractor to pull the data from the response headers. 然后,您可以使用正则表达式提取器从响应标头中提取数据。

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

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