Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir & Hound headless browser

Hound is pretty nice for testing web apps etc. when you need a headless browser. I got it working, played around with the tests etc. but there are 2 questions about hound which maybe someone can explain who is familiar with Elixir :)

1.) I’m using PhantomJS’s remote WebDriver mode (phantoms -w on localhost). I’ve set 'config :hound, driver: "phantomjs"' in config.exs so a simple "navigate_to @url” launches a PhantomJS instance and works properly. Now I want change the HTTP User Agent String for this request. PhantomJS provides this page.settings hash. Running the request above against a local PhantomJS in remote WebDriver mode shows me the following settings:

[INFO  - 2014-08-24T21:54:00.232Z] Session [27b92460-2bd9-11e4-a77f-1daa5df28587] - 
page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,
"loadImages":true,"localToRemoteUrlAccessEnabled":false,
"userAgent":"Mozilla/5.0 (Macintosh; PPC Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.7 Safari/534.34","webSecurityEnabled":true}
[INFO  - 2014-08-24T21:54:00.232Z] Session [27b92460-2bd9-11e4-a77f-1daa5df28587] - page.customHeaders:  - {}
[INFO  - 2014-08-24T21:54:00.232Z] Session [27b92460-2bd9-11e4-a77f-1daa5df28587] - Session.negotiatedCapabilities -
{"browserName":"phantomjs","version":"1.9.7","driverName":"ghostdriver","driverVersion":"1.1.0",
"platform":"mac-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,
"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,
"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct”}}

The question is: how to change the userAgent above? I didn’t find any example which deals with that. I know how it would look like running an PhantomJS instance directly as CLI tool with the appropriate JS config, but not sure, how hound manages that.

2.) I also need to use HTTP proxies with authentication. Same as in 1. I know how to deal with that launching PhantomJS from command line, but what is the right place to define them running on top of hound?

like image 457
ctp Avatar asked Sep 06 '25 03:09

ctp


1 Answers

You should pass a map as the additional_capabilities parameter to any function that starts a session.

Hound.start_session(%{userAgent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36"})

For the proxy option, the value should be another map with the properties.

Hound.start_session(%{proxy: %{property: "parameter", property: "parameter"}})

I never used it with proxy, so I'm not sure how to configure it properly.

like image 53
cevado Avatar answered Sep 07 '25 23:09

cevado