Selenium WebDriver テストにヘッダーを追加する方法はありますか? (Firefox Modify Headers プラグインと同様) ブラウザーを表示する必要があるため、HtmlUnitDriver を使用できません。
3 に答える
WebDriver では、ブラウザー ベースのドライバーを使用してヘッダーを変更または設定することはできません。次の URL で、ヘッダーと応答コードに関する彼らの決定に関する多くの情報を見つけることができます。 http://code.google.com/p/selenium/issues/detail?id=141
レンダリングされたページ要素をチェックするのではなく、応答とヘッダー情報だけをチェックするこのタイプのテストには、Apache HTTP クライアントを使用します。
また、上記の URL に記載されているように、セレン テストでブラウザー モブ プロキシを指定することもできます。私はこれを他の目的に使用しましたが、素晴らしいです。
これを行うには、次のような代替方法がありますBrowsermob-Proxy
Browsermob-proxy
セレングリッドで作業している間は独自の制限があるため、以下は私の場合の問題をどのように修正したかです。うまくいけば、同様の設定をしている人の助けになるかもしれません。
- 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);
- ブラウザ拡張機能に移動し、ModHeader のローカル ストレージ コンテキスト ID を取得します
- 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
- を使用してリクエストにヘッダーを追加します
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-1
はvalue-1
、追加するリクエスト ヘッダーtoken-2
とvalue-2
値です。
必要な Web アプリケーションに移動します。
driver.get("your-desired-website");