本文介绍了在 Jmeter 中执行 Javascript 的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Jmeter 脚本的设置部分包含在生成实际负载之前绕过我的 Web 应用程序的登录页面的步骤.作为这些步骤的结果,服务器设置了证明成功登录的特定 cookie.

要做到这一点,我的脚本应该执行服务器响应注入的 Javascript 函数.由于它是一次性程序,因此我使用 WebDriver 执行此操作并在浏览器实例中执行 Javascript.

但我绝对不能作为加载线程的一部分做同样的事情,因为 WebDriver 对于性能测试来说(根本)不是一个好主意.

但是,我仍然想知道是否有一种方法可以在不创建浏览器实例的情况下计算 JS,因为我的 JS 脚本只不过是算术计算(虽然很复杂).所以,

  1. 非常感谢有关如何在不创建浏览器实例的情况下执行 JS 的任何想法.由于我可以编写 BeanShell 代码,所以这应该不是什么大问题(只需共享可以为与 DOM 无关的计算创建虚拟"浏览器的 lib 名称).不确定它会更快,但我会尝试.
  2. 如果我是对的并且浏览器的使用是唯一可能的解决方案,那么我正在寻找一种在不同线程之间共享一个浏览器窗口的方法(即使对于等待它的人来说会有延迟).尝试使用 JavascriptExecutor,但不知道如何将 WebDriver 转换为 JavascriptExecutor(beanshell 是使用太旧的 Java 编写的,我不熟悉它的语法和功能).也不能使用 WebDriver 插件,因为它不允许只创建没有 get("pageUrl") 的窗口.

提前感谢您的任何想法.

解决方案
  1. 您可以使用

  2. 您可以使用 WebDriver Sampler 为循环中的所有线程使用单个浏览器实例,例如:

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

    它将生成 JMeter 变量,例如:

    cookie1=foocookie2=酒吧等等.

因此您将能够使用 __threadNum() 函数引用变量值需要的地方,如:

${__evalVar(cookie${__threadNum})}

参见 WebDriver 采样器:您回答的前 10 个问题 有关使用 WebDriver Sampler 的更多信息的指南.

SetUp part of my Jmeter script contains steps to bypass login page of my Web Application before generating real load. As a result of these steps, server sets specific cookie that proves successful loginization.

To do that, my script should execute Javascript function injected in response by server. Since it's a one-time procedure, I do it using WebDriver and execute Javascript within browser instance.

But I definitely can't do the same as a part of load thread as WebDriver is not a good idea (at all) for performance testing.

However, I'm still wondering is it a way to calculate JS without creating a browser instance, because my JS script is nothing but arithmetic calculations (complex though). So,

  1. Any ideas about how to execute JS without creating browser instance are very much appreciated. Since I can write BeanShell code, it shouldn't be a big issue (just share the lib name that can create "virtual" browser for calculations not related to DOM). Not sure that it'll be faster, but I'll try to.
  2. If I'm right and browser usage is the only possible solution, then I'm looking for a way to share one single browser window between different threads (even with delay for those who waits for it). Tried to use JavascriptExecutor, but don't know how to convert WebDriver to JavascriptExecutor (beanshell is written using too old Java, and I'm not familiar with its syntax and functionality). Can't use WebDriver plugin too as it doesn't allow just to create window without get("pageUrl").

Thanks for any ideas in advance.

解决方案
  1. You can use JSR223 Sampler, choose javascript from "Language" dropdown and place your code in "Script" area

  2. You can use single browser instance using WebDriver Sampler for all threads in a loop, something like:

    var ctx = org.apache.jmeter.threads.JMeterContextService.getContext()
    var vars = ctx.getVariables();
    
    for (var i=0; i< THREADS_NUMBER; i++) {
        WDS.browser.manage().deleteAllCookies()
        WDS.browser.get('LOGIN_PAGE_URL')
        var cookie = WDS.browser.manage().getCookieNamed("COOKIE_NAME").getValue();
        vars.put("cookie" + i, cookie);
    }
    

    it will generate JMeter Variables like:

    cookie1=foo
    cookie2=bar
    etc.
    

So you will be able to refer variable values using __threadNum() function where required like:

${__evalVar(cookie${__threadNum})}

See The WebDriver Sampler: Your Top 10 Questions Answered guide for more information on using the WebDriver Sampler.

这篇关于在 Jmeter 中执行 Javascript 的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:07