Skip to end of metadata
Go to start of metadata
Authentication dialogs present a challenge for automated testing with Selenium, because there's no way for Selenium to interact with them. A solution is to use Selenium to inject cookies that will let you bypass authentication by setting an authenticated state for the application or site you're testing. With this solution, you may need to make a change in the source code of the application so that the cookie is acknowledged, but your tests will be able to run without the need for user authentication credentials. 

The basic process for using cookie injection with testing on Sauce would be:

  1. Launch your browser on Sauce Labs. 
  2. Inject cookies via Selenium.
  3. Go to the site or application you want to test.
  4. Run your tests. 


You must be on the same domain that the cookie is valid for in order for this to work.

If the home page of the site you want to test takes a long time, you can try accessing a smaller page, like the 404 page, for the purpose of injecting the cookie before you access the home page.


This code example demonstrates how you would set the cookies, but you can find additional examples for Java, Python, Ruby, and Perl on the official Selenium Commands and Operations page
# prereq: have sauce username and accesskey set as environment variables

require 'selenium-webdriver'

url = "http://#{ENV['SAUCE_USERNAME']}:#{ENV['SAUCE_ACCESS_KEY']}".strip

browser = Selenium::WebDriver.for(:remote, :url => url,
				:desired_capabilities => { 
					:browserName => 'firefox',
					:version => '40',
					:platform => 'Windows 7'

	:name => 'CookieName',
	:value => 'CookieValue',
	:path => '/',
	:secure => false


puts "Printing out cookies : #{browser.manage.all_cookies}"

# go to URL / app where authentication was needed / prompted.
# i.e.
# browser.get ''
# ...