【问题标题】:Headers and Selenium Webdriver 2标头和 Selenium Webdriver 2
【发布时间】:2012-10-24 10:17:25
【问题描述】:

有没有办法在 Selenium WebDriver 测试中添加标题? (就像 Firefox Modify Headers 插件一样)我不能使用 HtmlUnitDriver,因为浏览器必须是可见的。

【问题讨论】:

  • 对于那些使用 Python 编写 Selenium 测试的人,您可以考虑使用Selenium Wire,它使您能够添加浏览器发送的标头以及检查请求和响应。

标签: header webdriver


【解决方案1】:

WebDriver 不允许您使用任何基于浏览器的驱动程序更改或设置标题。您可以在以下 URL 找到很多关于他们对标头和响应代码的决定的信息。 http://code.google.com/p/selenium/issues/detail?id=141

我们使用 Apache HTTP 客户端进行此类测试,我们不想检查呈现的页面元素,而只想检查响应和标题信息。

您还可以通过您的 selenium 测试以及上面的 url 中提到的方式提供浏览器 mob 代理。我已经将它用于其他目的,它很棒。

【讨论】:

    【解决方案2】:

    还有其他方法可以做到这一点,例如Browsermob-Proxy

    由于Browsermob-proxy 在我们使用 selenium 网格时有其自身的局限性,以下是我解决此问题的方法。希望对有类似设置的人有所帮助。

    1. 将 ModHeader 扩展添加到 chrome 浏览器

    如何下​​载 Modheader?链接

    ChromeOptions options = new ChromeOptions();
    options.addExtensions(new File(C://Downloads//modheader//modheader.crx));
    
    // Set the Desired capabilities 
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    
    // Instantiate the chrome driver with capabilities
    WebDriver driver = new RemoteWebDriver(new URL(YOUR_HUB_URL), options);
    
    1. 转到浏览器扩展并捕获 ModHeader 的本地存储上下文 ID

    1. 导航到 ModHeader 的 URL 以设置本地存储上下文

    .

    // set the context on the extension so the localStorage can be accessed
    driver.get("chrome-extension://idgpnmonknjnojddfkpgkljpfnnfcklj/_generated_background_page.html");
    
    Where `idgpnmonknjnojddfkpgkljpfnnfcklj` is the value captured from the Step# 2
    
    1. 现在使用Javascript将标头添加到请求中

    .

       ((Javascript)driver).executeScript(
             "localStorage.setItem('profiles', JSON.stringify([{  title: 'Selenium', hideComment: true, appendMode: '', 
                 headers: [                        
                   {enabled: true, name: 'token-1', value: 'value-1', comment: ''},
                   {enabled: true, name: 'token-2', value: 'value-2', comment: ''}
                 ],                          
                 respHeaders: [],
                 filters: []
              }]));");
    

    其中token-1value-1token-2value-2 是要添加的请求标头和值。

    1. 现在导航到所需的网络应用程序。

      driver.get("your-desired-website");

    【讨论】:

      【解决方案3】:

      下面是一个简短的例子,说明如何在 Python 中使用 Seleniumwire:

      from seleniumwire import webdriver
      
      def set_chrome_driver():
              options = webdriver.ChromeOptions()
              options.add_argument("--start-maximized")
              options.add_argument("--disable-infobars")
              options.add_argument("--no-proxy-server")
              driver = webdriver.Chrome(executable_path=r'C:\Automation_package\chromedriver.exe')
              driver.get('http://172.1.1.1:5000/path/api/')
              driver.header_overrides = {"iv-user": "Admin", "iv-groups": "SuperAdmin", "iv-roles": "Viewers",}
              driver.get('http://172.1.1.1:5000/path/api/')
      

      【讨论】:

        猜你喜欢
        • 2011-05-11
        • 2018-01-21
        • 2012-03-30
        • 2013-07-25
        • 1970-01-01
        • 2013-12-28
        • 2011-08-25
        • 1970-01-01
        • 2013-05-25
        相关资源
        最近更新 更多