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:
The basic process for using cookie injection with testing on Sauce would be:
- Launch your browser on Sauce Labs.
- Inject cookies via Selenium.
- Go to the site or application you want to test.
- 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 ago, 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
# i.e. export SAUCE_USERNAME=YOUR_USERNAME
# export SAUCE_ACCESS_KEY=YOUR_ACCESS_KEY
require
'selenium-webdriver'
url =
"http://#{ENV['SAUCE_USERNAME']}:#{ENV['SAUCE_ACCESS_KEY']}@ondemand.us-west-1.saucelabs.com:443/wd/hub"
.strip
browser = Selenium::WebDriver.
for
(:remote, :url => url,
:desired_capabilities => {
:browserName =>
'firefox'
,
:version =>
'40'
,
:platform =>
'Windows 7'
})
browser.manage.add_cookie({
: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
'http://yourwebsite.com'
# ...
browser.quit