testing pages with Basic HTTP Authentication
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
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]
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.
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.
Also FYI: we are discussing a onBeforeEachScript in another thread. Please feel free to add any comments... ==> https://github.com/garris/BackstopJS/issues/147
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 How do you define getCurrentUrl()??
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