5

I am running some unittests with Selenium Webdriver.

I have an entire test that runs successfully using webdriver.Firefox(), here is the setup:

def setUp(self):
    self.driver = webdriver.Firefox()
    self.driver.implicitly_wait(30)
    self.base_url = "http://www.nike.com"
    self.verificationErrors = []
    self.accept_next_alert = True

The test runs successfully, however I have to manually enter in basic auth multiple times for the test to keep moving forward.

In an attempt to bypass basic auth and have the entire test truly automated, I have switched from Firefox to phantomjs, as it sounds like you can pass in Basic Auth with each click()

Here is my setup after switching to phantomjs:

def setUp(self):
    dcap = dict(DesiredCapabilities.PHANTOMJS)
    dcap["phantoms.page.settings.userName"] = ("testuser")
    dcap["phantoms.page.settings.userPassword"] = ("testpass")
    self.driver = webdriver.PhantomJS(desired_capabilities=dcap, service_args=['--ignore-ssl-errors=true'])
    self.driver.implicitly_wait(30)
    self.base_url = "http://www.nike.com
    self.verificationErrors = []
    self.accept_next_alert = True

But I get the following error:

NoSuchElementException: Message: {"errorMessage":"Unable to find
element with xpath '(//button[@type='button'])[2]'","request":
{"headers":{"Accept":"application/json","Accept-Encoding":"identity",
"Connection":"close","Content-Length":"113","Content- Type":"application/json;charset=UTF-8",
"Host":"127.0.0.1:58025","UserAgent":"Pythonurllib/2.7"},"httpVersion":"1.1",
"method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"c2fa02e0-1df0-11e6-a2ad-c325e56df16d\",
\"value\": \"(//button[@type='button'][2]\"}","url":"/element","urlParsed":
{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"",
"password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":    
["element"]},"urlOriginal":"/session/c2fa02e0-1df0-11e6-a2ad-c325e56df16d/element"}}

I am not sure if this error is just a difference between phantomjs and firefox, or if I'm just passing auth incorrectly.

david
  • 6,303
  • 16
  • 54
  • 91

1 Answers1

4

Here is the way to define the basic authentication token in the headers with PhantomJS and Selenium:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import base64

authentication_token = "Basic " + base64.b64encode(b'username:password')

capa = DesiredCapabilities.PHANTOMJS
capa['phantomjs.page.customHeaders.Authorization'] = authentication_token
driver = webdriver.PhantomJS(desired_capabilities=capa)

driver.get("http://...")
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • Thanks, will this then pass the authentication_token on every successive click ? – david May 19 '16 at 20:04
  • Yes if the click is a link to a new page, but I'm not sure for an Ajax call. – Florent B. May 19 '16 at 20:19
  • 1
    I accepted your answer, however this did not end up resolving my issue because basic auth is requested from multiple hosts (embedded content in the page requires it's own basic auth). I'm not sure that there is a way to pass credentials to multiple hosts this way using Selenium, I thought phantomjs would offer a workaround but I guess not. – david May 19 '16 at 20:52