Skip to content

Commit

Permalink
Add http credentials to browserContext options (#3036)
Browse files Browse the repository at this point in the history
* [ISSUE#3035] Add http credentials to browserContext options

* little fix to isAuthenticated flag logic which doesnot change anything with config set once before the first time browser starts
  • Loading branch information
nikocanvacom authored Sep 13, 2021
1 parent e98116c commit 3429731
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/helper/Playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ class Playwright extends Helper {
if (this.options.restart && !this.options.manualStart) await this._startBrowser();
if (!this.isRunning && !this.options.manualStart) await this._startBrowser();

this.isAuthenticated = false;
if (this.isElectron) {
this.browserContext = this.browser.context();
} else if (this.userDataDir) {
Expand All @@ -364,6 +365,10 @@ class Playwright extends Helper {
acceptDownloads: true,
...this.options.emulate,
};
if (this.options.basicAuth) {
contextOptions.httpCredentials = this.options.basicAuth;
this.isAuthenticated = true;
}
if (this.options.recordVideo) contextOptions.recordVideo = this.options.recordVideo;
if (this.storageState) contextOptions.storageState = this.storageState;
this.browserContext = await this.browser.newContext(contextOptions); // Adding the HTTPSError ignore in the context so that we can ignore those errors
Expand Down Expand Up @@ -559,7 +564,7 @@ class Playwright extends Helper {
page.setDefaultNavigationTimeout(this.options.getPageTimeout);
this.context = await this.page;
this.contextLocator = null;
if (this.config.browser === 'chrome') {
if (this.options.browser === 'chrome') {
await page.bringToFront();
}
}
Expand Down Expand Up @@ -714,9 +719,9 @@ class Playwright extends Helper {
url = this.options.url + url;
}

if (this.config.basicAuth && (this.isAuthenticated !== true)) {
if (this.options.basicAuth && (this.isAuthenticated !== true)) {
if (url.includes(this.options.url)) {
await this.browserContext.setHTTPCredentials(this.config.basicAuth);
await this.browserContext.setHTTPCredentials(this.options.basicAuth);
this.isAuthenticated = true;
}
}
Expand Down

0 comments on commit 3429731

Please sign in to comment.