Skip to content

Commit

Permalink
Merge pull request #316 from taizo/custom_headers
Browse files Browse the repository at this point in the history
Added support for custom headers in PhantomJS driver.
  • Loading branch information
douglascamata committed May 9, 2014
2 parents 57761ea + 9fa2b84 commit 34ead6f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion splinter/driver/webdriver/phantomjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ class WebDriver(BaseWebDriver):
element_class = WebDriverElement

def __init__(self, user_agent=None, load_images=True,
desired_capabilities=None, wait_time=2, **kwargs):
desired_capabilities=None, wait_time=2,
custom_headers={}, **kwargs):
capabilities = DesiredCapabilities.PHANTOMJS.copy()
if user_agent is not None:
capabilities['phantomjs.page.settings.userAgent'] = user_agent
capabilities['phantomjs.page.settings.loadImages'] = load_images
if isinstance(custom_headers, dict):
for name, value in custom_headers.items():
capabilities['phantomjs.page.customHeaders.%s' % name] = value
if desired_capabilities:
capabilities.update(desired_capabilities)

Expand Down
5 changes: 5 additions & 0 deletions tests/fake_webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def upload_file():
return '|'.join(buffer)


@app.route('/headers', methods=['GET'])
def request_headers():
return str(request.headers)


@app.route('/foo')
def foo():
return "BAR!"
Expand Down
20 changes: 20 additions & 0 deletions tests/test_webdriver_phantomjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,23 @@ def test_can_work_on_popups(self):
# FIXME: Check https://github.com/detro/ghostdriver/issues/180 to see if
# we can implement this test
pass


class PhantomJSBrowserTestWithCustomHeaders(unittest.TestCase):

@classmethod
def setUpClass(cls):
custom_headers = {'X-Splinter-Customheaders-1': 'Hello',
'X-Splinter-Customheaders-2': 'Bye'}
cls.browser = Browser("phantomjs", custom_headers=custom_headers)

def test_create_a_phantomjs_with_custom_headers(self):
self.browser.visit(EXAMPLE_APP + 'headers')
self.assertTrue(
self.browser.is_text_present('X-Splinter-Customheaders-1: Hello'))
self.assertTrue(
self.browser.is_text_present('X-Splinter-Customheaders-2: Bye'))

@classmethod
def tearDownClass(cls):
cls.browser.quit()

0 comments on commit 34ead6f

Please sign in to comment.