Basic Http Authentication

If your website is protected by basic auth, the login prompt comes in way before you can run any automated tests. You can deal with this alert (or prompt) by using WebDriver's Alert class and sending keys for your username and password. Alternatively, BrowserStack provides the basic authentication handling in the following three ways:

Basic authentication technique Use case
URL encoding When you open the first URL in the testscript which has basic authentication.
Supported Browser: All browsers and device combination
JavascriptExecutor for basic authentication When you navigate to a URL from within your testscript, which has basic authentication.
Supported Browser: All browsers and device combination except Safari browser on MacOS
JavascriptExecutor for dismissing login pop-up When you want to dismiss the basic auth login pop-up.
Supported Browser: All browsers and device combination except Safari browser on MacOS

URL encoding

URL encoding 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.

Note: If you have @ or : in your password or username you need to URL encode them too using URL encoding.

For example, if you have basic authentication enabled in the www.example.com/index.html page then by using URL encoding (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: 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.

JavascriptExecutor for basic HTTP Authentication

While navigating to a secured web page (using click action, Javascript navigation commands, etc.) URL encoding autologin is not supported. Hence sendBasicAuth JavascriptExecutor is used, 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' #Set `javascriptEnabled` capability to `true`
caps['unhandledPromptBehavior'] = 'ignore'
caps["javascriptEnabled"]=true #Set `javascriptEnabled` capability to `true`
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>"}}');

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 #Set `javascriptEnabled` capability to `true`
driver.execute_script('browserstack_executor: {"action": "dismissBasicAuth","arguments": {"timeout": "<time in milliseconds>"}}')
$driver->execute_script('browserstack_executor: {"action": "dismissBasicAuth","arguments": {"timeout": "<time in milliseconds>"}}');
Warning: BasicAuth JavascriptExecutor does not work on Safari browsers on Mac OS.

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