DEV Community

Cover image for Selenium Webdriver - how to bypass server's basic authentication popup
Alan Schio
Alan Schio

Posted on

Selenium Webdriver - how to bypass server's basic authentication popup

Are you entering on E2E/Automated test world with selenium-webdriver and had faced this issue:

You need to access a page which has basic authentication popup, a secure server.

Basic auth popup

Well research and found how to do this was a little harsh, so this is a quick guide for you to don't suffer like i did.

The secret is the createCDPConnection. With selenium-webdriver we need to get to the browser api and perform a register

 const { Builder, By } = require('selenium-webdriver'); const Chrome = require('selenium-webdriver/chrome'); const assert = require("assert"); const url = `https://my.server.com/` describe('Server with basic auth', function () { before(async function () { driver = await new Builder().forBrowser('chrome').build(); }); beforeEach(async () => { // this is what does the trick const connection = await driver.createCDPConnection('page'); await driver.register("username", "password", connection); // now you can go the url safety await driver.get(url); }) it('Bypass the popup', async function () { const message = await driver.findElement(By.className("heading")).getText(); assert.equal('Welcome!', message); }) after(function () { await driver.quit() }) }) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)