Basic Http Authentication

To test a website which is protected by basic auth (username and password), you can authenticate yourself using one of the following techniques in the test script:

Basic authentication technique Use case
Pass username and password in the URL When you open the first URL which has basic authentication (using driver.get, etc.) in the testscript.
Basic authentication - JSExecutor When you navigate to a URL which has basic authentication (using click action, Javascript navigation commands, etc.).
Dismiss login pop-up - JSExecutor When you want to dismiss the basic auth login pop-up.
Important: Safari in MacOS does not support basic authentication by passing the username and password in URL. Unfortunately, currently we do not have any workaround to solve this problem.

Pass username and password in the URL

Passing username and password in the URL helps to avoid the login prompt. This is achieved by encoding the username and password in the URL, that is, prepending username:password@ to the hostname in the URL.

For example, if you have basic authentication enabled in the www.example.com/index.html page then by passing username and password in the URL (refer the below code), you can avoid the login prompt and get authenticated automatically.

// ... Get the URL
driver.get("https://<username>:<password>@www.example.com/index.html")
// ... Get the URL
driver.get('https://<username>:<password>@www.example.com/index.html').then(function(){

});
// ... Get the URL
driver.Navigate().GoToUrl("https://<username>:<password>@www.example.com/index.html");
// ... Get the URL
$web_driver->get("https://<username>:<password>@www.example.com/index.html");
# ... Get the URL
driver.get("https://<username>:<password>@www.example.com/index.html")
# ... Get the URL
driver.navigate.to "https://<username>:<password>@www.example.com/index.html"
# ... Get the URL
$driver->get('https://<username>:<password>@www.example.com/index.html');
Note: If you have @ or : in your password or username you need to encode them using URL encoding. However, some browsers (some versions of Chrome & IE) don’t support URL encoding anymore as per RFC 3986. Thus you can use sendBasicAuth JavascriptExecutor for basic authentication.

Supported Browser: All browsers and device combination except Safari browser on MacOS.

JavascriptExecutor for basic HTTP Authentication

While navigating to a secured web page (using click action, Javascript navigation commands, etc.) which has basic authentication, passing username and password in the URL for autologin is not supported. Hence sendBasicAuth JavascriptExecutor is used (refer the below code), which performs autologin to a webpage with a predefined username and password.

caps.setCapability("unhandledPromptBehavior", "ignore"); 
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("browserstack_executor: {\"action\": \"sendBasicAuth\", \"arguments\": {\"username\":\"<username>\", \"password\": \"<password>\", \"timeout\": \"<time in milliseconds>\"}}");
var capabilities = {
  'unhandledPromptBehavior' : 'ignore'
};
driver.executeScript('browserstack_executor: {\"action\": \"sendBasicAuth\", \"arguments\": {\"username\":\"<username>\", \"password\": \"<password>\", \"timeout\": \"<time in milliseconds>\"}}');
capability.AddAdditionalCapability("unhandledPromptBehavior", "ignore", true);  
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"sendBasicAuth\",\"arguments\": {\"username\": \"<username>\", \"password\": \"<password>\", \"timeout\": \"<time in milliseconds>\"}}");
$caps = array(
  "unhandledPromptBehavior" => "ignore"
);
$driver->executeScript('browserstack_executor: {"action": "sendBasicAuth", "arguments": {"username":"<username>", "password": "<password>", "timeout": "<time in milliseconds>"}}');
desired_cap = {
  'unhandledPromptBehavior': 'ignore'
}  
driver.execute_script('browserstack_executor: {\"action\": \"sendBasicAuth\", \"arguments\": {\"username\":\"<username>\", \"password\": \"<password>\", \"timeout\": \"<time in milliseconds>\"}}')
caps['javascriptEnabled'] = 'true' #Additionally, include this capability for JavaScript executors to work
caps['unhandledPromptBehavior'] = 'ignore'

driver.execute_script('browserstack_executor: {"action": "sendBasicAuth", "arguments": {"username":"<username>", "password": "<password>", "timeout": "<time in milliseconds>"}}')
my $extraCaps = {
  'unhandledPromptBehavior' => 'ignore'
} 
$driver->execute_script('browserstack_executor: {"action": "sendBasicAuth", "arguments": {"username":"<username>", "password": "<password>", "timeout": "<time in milliseconds>"}}');

Supported Browser: All browsers and device combination except Safari browser on MacOS and Android mobile devices.

JavascriptExecutor to dismiss login pop-up

The dismissbasicauth JavascriptExecutor is used to dismiss login pop-up while navigating to a secured web page. This is used in cases where you want to test your website to ensure appropriate page is loaded on dismissing the login pop-up.

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("browserstack_executor: {\"action\": \"dismissBasicAuth\",\"arguments\": {\"timeout\": \"<time in milliseconds>\"}}");
driver.executeScript('browserstack_executor: {\"action\": \"dismissBasicAuth\",\"arguments\": {\"timeout\": \"<time in milliseconds>\"}}');
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"dismissBasicAuth\",\"arguments\": {\"timeout\": \"<time in milliseconds>\"}}");
$driver->executeScript('browserstack_executor: {"action": "dismissBasicAuth", "arguments": {"timeout":"<time in milliseconds>"}}');
driver.execute_script('browserstack_executor: {\"action\": \"dismissBasicAuth\",\"arguments\": {\"timeout\": \"<time in milliseconds>\"}}')
caps["javascriptEnabled"]=true #Additionally, include this capability for JavaScript executors to work

driver.execute_script('browserstack_executor: {"action": "dismissBasicAuth","arguments": {"timeout": "<time in milliseconds>"}}')
$driver->execute_script('browserstack_executor: {"action": "dismissBasicAuth","arguments": {"timeout": "<time in milliseconds>"}}');

Supported Browser: All browsers and device combination except Safari browser on MacOS and Android mobile devices.

Warning: Basic authentication using URL encoding will not work if there is any URL redirection and authentication is required on the redirected website. You need to use our JavascriptExecutor in those cases.

We're sorry to hear that. Please share your feedback so we can do better







Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better







Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Talk to an Expert