Skip to content

Instantly share code, notes, and snippets.

@epall
Created February 10, 2011 22:46
Show Gist options
  • Save epall/821526 to your computer and use it in GitHub Desktop.
Save epall/821526 to your computer and use it in GitHub Desktop.
Patch to make Selenium 2 Python bindings work with Sauce OnDemand
commit f43bcc04f81013927cf1acf8a9435fe54469afca
Author: theautomatedtester <theautomatedtester@07704840-8298-11de-bf8c-fd130f914ac9>
Date: Thu Jan 27 13:35:59 2011 +0000
DavidBurns, on behalf of Eric Allen, adding HTTP Basic Auth support for issue 1096
git-svn-id: http://selenium.googlecode.com/svn/trunk@11164 07704840-8298-11de-bf8c-fd130f914ac9
diff --git a/py/CHANGES b/py/CHANGES
index 9678943..509ae86 100644
--- a/py/CHANGES
+++ b/py/CHANGES
@@ -14,3 +14,4 @@ Changes since 2011-01-01
* Corrected Element finding from the element
* Alert and Prompt handling
* Improved IEDriver
+* Basic Authentication support for Selenium 2
diff --git a/py/selenium/webdriver/remote/remote_connection.py b/py/selenium/webdriver/remote/remote_connection.py
index fdc48b4..8ade951 100644
--- a/py/selenium/webdriver/remote/remote_connection.py
+++ b/py/selenium/webdriver/remote/remote_connection.py
@@ -16,6 +16,7 @@
import logging
import string
import urllib2
+import urlparse
from command import Command
import utils
@@ -235,11 +236,31 @@ class RemoteConnection(object):
"""
LOGGER.debug('%s %s %s' % (method, url, data))
- request = Request(url, data=data, method=method)
+ parsed_url = urlparse.urlparse(url)
+ auth = None
+ password_manager = None
+ if parsed_url.username:
+ netloc = parsed_url.hostname
+ if parsed_url.port:
+ netloc += ":%s" % parsed_url.port
+ cleaned_url = urlparse.urlunparse((parsed_url.scheme, netloc, parsed_url.path,
+ parsed_url.params, parsed_url.query, parsed_url.fragment))
+ password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
+ password_manager.add_password(None, "%s://%s" % (parsed_url.scheme, netloc), parsed_url.username, parsed_url.password)
+ request = Request(cleaned_url, data=data, method=method)
+ else:
+ request = Request(url, data=data, method=method)
+
+
request.add_header('Accept', 'application/json')
- opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),
- HttpErrorHandler())
+ if password_manager:
+ opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),
+ HttpErrorHandler(),
+ urllib2.HTTPBasicAuthHandler(password_manager))
+ else:
+ opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),
+ HttpErrorHandler())
response = opener.open(request)
try:
if response.code > 399 and response.code < 500:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment