使用 JMeter Webdriver (Plugin Selenium) 制作 HTTP Header Manager 并在 Dynatrace 中查看结果

问题描述

如果我使用 JMeter,我想知道如何将 HTTP 标头管理器放在 Selenium Webdriver Sampler 中。

我知道 JMeter 中有标准工具(HTTP Header Manager),但是当我在测试中使用 HTTP 请求时,该工具很有用。在这种情况下进行测试,我只使用带有 Java 1.8 的 WebDriver Sampler。目标是在 dynatrace 中查看我从 JMeter 发送的标签。有可能这样做吗?如果答案是肯定的,我该怎么做?感谢您的帮助!

解决方法

  1. WebDriver Sampler 不尊重 HTTP 标头管理器
  2. WebDriver itself doesn't support working with HTTP Headers 并且该功能不太可能实现

所以选项在:

  1. 使用像 ModHeader 这样的扩展,但在这种情况下,您必须从 WebDriver Sampler 切换到 JSR223 Sampler。示例代码:

    def options = new org.openqa.selenium.chrome.ChromeOptions()
    options.addExtensions(new File('/path/to/modheaders.crx'))
    def capabilities = new org.openqa.selenium.remote.DesiredCapabilities()
    capabilities.setCapability(org.openqa.selenium.chrome.ChromeOptions.CAPABILITY,options)
    def driver = new org.openqa.selenium.chrome.ChromeDriver(capabilities)
    
    
    driver.get('http://example.com')
    
  2. 使用像 BrowserMob 这样的代理作为 WebDriver 的代理,并配置它为每个拦截的请求添加标头。示例初始化代码(您可以将其放入上述 JSR223 Sampler 中的 setUp Thread Group 中)

    def proxy = new net.lightbody.bmp.BrowserMobProxyServer()
    def proxyPort = 8080
    proxy.setTrustAllServers(true)
    proxy.addRequestFilter((request,contents,info) -> {
        request.headers().add('your header name','your header value')
        return null
    })
    proxy.start(proxyPort)