3

Selenium WebDriver テストにヘッダーを追加する方法はありますか? (Firefox Modify Headers プラグインと同様) ブラウザーを表示する必要があるため、HtmlUnitDriver を使用できません。

4

3 に答える 3

3

WebDriver では、ブラウザー ベースのドライバーを使用してヘッダーを変更または設定することはできません。次の URL で、ヘッダーと応答コードに関する彼らの決定に関する多くの情報を見つけることができます。 http://code.google.com/p/selenium/issues/detail?id=141

レンダリングされたページ要素をチェックするのではなく、応答とヘッダー情報だけをチェックするこのタイプのテストには、Apache HTTP クライアントを使用します。

また、上記の URL に記載されているように、セレン テストでブラウザー モブ プロキシを指定することもできます。私はこれを他の目的に使用しましたが、素晴らしいです。

于 2012-10-24T22:23:41.253 に答える
0

これを行うには、次のような代替方法がありますBrowsermob-Proxy

Browsermob-proxyセレングリッドで作業している間は独自の制限があるため、以下は私の場合の問題をどのように修正したかです。うまくいけば、同様の設定をしている人の助けになるかもしれません。

  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 を取得します

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-1、追加するリクエスト ヘッダーtoken-2value-2値です。

  1. 必要な Web アプリケーションに移動します。

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

于 2020-08-11T10:34:43.677 に答える