Search code examples
javascriptjasmineprotractor

How to handle basic authentication with protractor?


I'm trying protractor to write a few tests in a non angular application. I have to login in a page trough basic authentication in google chrome, but i have no idea how.

Authentication box

I already tried baseUrl: 'https://username:password@url' and capabilities: { 'browserName': 'chrome', 'chromeOptions' : { args: ['--login-user=foo', '--login-password=bar'] } }

But none if these worked for me. Anyone knows how to do it? I'm having some hard time on it.


Solution

  • The short answer is there is no easy way of doing it on chrome because they do not support modifying request headers -- see https://code.google.com/p/selenium/issues/detail?id=141 (title says response headers, but if you read it, it's for all headers).

    That being said, there are ways to do it, albeit difficult.

    1) Find a chrome extension/plugin that allows you to modify header. A simple search bring up many of them: https://chrome.google.com/webstore/search/modify%20header. You'll need to add the plugin to webdriver: see Is it possible to add a plugin to chromedriver under a protractor test?.

    2) You can use browsermob-proxy (https://github.com/lightbody/browsermob-proxy); this way you route your traffic through the proxy, which would add the headers for you. From the docs:

    POST /proxy/[port]/auth/basic/[domain] - Sets automatic basic authentication for the specified domain
    Payload data should be json encoded username and password name/value pairs (ex: {"username": "myUsername", "password": "myPassword"}
    

    There's a node project that may help you, https://github.com/zzo/browsermob-node, but you would still need to set up your proxy server yourself.

    Both ways for chrome are complex, but would get you what you want. (or you can stick with firefox and follow Robert's answer)