BackstopJS icon indicating copy to clipboard operation
BackstopJS copied to clipboard

testing pages with Basic HTTP Authentication

Open mayacode opened this issue 10 years ago • 7 comments

Hello,

I discovered, that in many companies staging/testing environments have Basic HTTP Authentication, as they are available from outside of company inner network. Is there any implemented solution already? Or maybe is there a way to add some settings to casper, before page will be open? As there is "onReadyScript" property, so maybe "beforeOpenUrlScript" would be also useful?

If there is any other option to use casper.setHttpAuth before page is open, I will appreciate any hint how to do this.

Best,

Maja

mayacode avatar Feb 03 '16 14:02 mayacode

It's very easy. Try this... https://github.com/garris/BackstopJS/issues/123#issuecomment-166804008

When you add the URL to the scenerios you can include the http basic auth username and password in the URL and it will log you in.

example: http://username:[email protected]

garris avatar Feb 03 '16 15:02 garris

True, but if I have more pages and if credential change I will have to change it in all links... so I would like to keep it in one file.

mayacode avatar Feb 03 '16 16:02 mayacode

I see. In that case you can totally use a global onReadyScript. See the documentation on how to implement this.

Your script would probably look like this...

module.exports = function(casper, scenario, vp) {
  casper.setHttpAuth([yourUserName], [yourPassword]);
  casper.thenOpen(scenario.url);
}

Please let me know if that does the trick.

garris avatar Feb 03 '16 17:02 garris

Also FYI: we are discussing a onBeforeEachScript in another thread. Please feel free to add any comments... ==> https://github.com/garris/BackstopJS/issues/147

garris avatar Feb 03 '16 17:02 garris

onReadyScript is launched AFTER thenOpen in genBitmaps.js, setting auth should be before it. I cannot see any point of opening the same page again.

Thanks for the link to other issue, will read.

And I would suggest to use casper.getCurrentUrl(), not scenario.url, especially if 'referenceUrl' property is defined.

mayacode avatar Feb 04 '16 08:02 mayacode

@mayacode How do you define getCurrentUrl()??

Farnoosh63 avatar Mar 29 '17 19:03 Farnoosh63

With backstopjs 6.3.25, the following javascript code must be put into backstop_data/engine_scripts/puppet/onBefore.js:

module.exports = async (page, scenario, vp) => {
    await page.authenticate({'username': 'myuser', 'password': 'secret'});
};

Taken from https://dev.to/sonyarianto/puppeteer-quick-tip-how-to-do-basic-authentication-2pe7

cweiske avatar Jun 08 '26 14:06 cweiske