setting request headers in selenium

后端 未结 9 1171
青春惊慌失措
青春惊慌失措 2020-11-29 21:53

I\'m attempting to set the request header \'Referer\' to spoof a request coming from another site. We need the ability test that a specific referrer is used, which returns a

相关标签:
9条回答
  • 2020-11-29 22:51

    Had the same issue today, except that I needed to set different referer per test. I ended up using a middleware and a class to pass headers to it. Thought I'd share (or maybe there's a cleaner solution?):

    lib/request_headers.rb:
    
    class CustomHeadersHelper
      cattr_accessor :headers
    end
    
    class RequestHeaders
      def initialize(app, helper = nil)
        @app, @helper = app, helper
      end
    
      def call(env)
        if @helper
          headers = @helper.headers
    
          if headers.is_a?(Hash)
            headers.each do |k,v|
              env["HTTP_#{k.upcase.gsub("-", "_")}"] = v
            end
          end
        end
    
        @app.call(env)
      end
    end
    

    config/initializers/middleware.rb
    
    require 'request_headers'
    
    if %w(test cucumber).include?(Rails.env)
      Rails.application.config.middleware.insert_before Rack::Lock, "RequestHeaders", CustomHeadersHelper
    end
    

    spec/support/capybara_headers.rb
    
    require 'request_headers'
    
    module CapybaraHeaderHelpers
      shared_context "navigating within the site" do
        before(:each) { add_headers("Referer" => Capybara.app_host + "/") }
      end
    
      def add_headers(custom_headers)
        if Capybara.current_driver == :rack_test
          custom_headers.each do |name, value|
            page.driver.browser.header(name, value)
          end
        else
          CustomHeadersHelper.headers = custom_headers
        end
      end
    end
    

    spec/spec_helper.rb
    
    ...
    config.include CapybaraHeaderHelpers
    

    Then I can include the shared context wherever I need, or pass different headers in another before block. I haven't tested it with anything other than Selenium and RackTest, but it should be transparent, as header injection is done before the request actually hits the application.

    0 讨论(0)
  • 2020-11-29 22:57

    With the solutions already discussed above the most reliable one is using Browsermob-Proxy

    But while working with the remote grid machine, Browsermob-proxy isn't really helpful.

    This is how I fixed the problem in my case. Hopefully, might be helpful for anyone with a similar setup.

    1. Add the ModHeader extension to the chrome browser

    How to download the Modheader? Link

    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. Go to the browser extensions and capture the Local Storage context ID of the ModHeader

    1. Navigate to the URL of the ModHeader to set the Local Storage Context

    .

    // 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. Now add the headers to the request using 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: []
              }]));");
    

    Where token-1, value-1, token-2, value-2 are the request headers and values that are to be added.

    1. Now navigate to the required web-application.

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

    0 讨论(0)
  • 2020-11-29 22:58

    You can do it with PhantomJSDriver.

    PhantomJSDriver pd = ((PhantomJSDriver) ((WebDriverFacade) getDriver()).getProxiedDriver());
    pd.executePhantomJS(
                "this.onResourceRequested = function(request, net) {" +
                "   net.setHeader('header-name', 'header-value')" +
                "};");
    

    Using the request object, you can filter also so the header won't be set for every request.

    0 讨论(0)
提交回复
热议问题