Configuring Elixir's Hound for custom user agents and authenticated proxies

Hey folks! I’ve been tinkering with Hound for headless browser testing in Elixir. It’s pretty cool, but I’m stuck on a couple things:

  1. How do I change the user agent string? I’m using PhantomJS in remote WebDriver mode, and I can see the default settings, but I can’t figure out how to modify the user agent through Hound.

  2. I need to use HTTP proxies with authentication for some tests. Any idea how to set this up with Hound? I know how to do it directly with PhantomJS, but I’m not sure where to configure this in the Hound setup.

I’ve got Hound working fine otherwise, but these two issues are holding me back. Any help would be awesome! Here’s a dummy code snippet to show what I’m working with:

defmodule MyApp.BrowserTest do
  use ExUnit.Case
  use Hound.Helpers

  hound_session()

  test "visits a webpage" do
    navigate_to("http://example.com")
    assert page_title() == "Example Domain"
  end
end

Thanks in advance for any tips or pointers!

I’ve been using Hound for a while now, and I can share some insights on your questions. For changing the user agent, you can actually do it by executing JavaScript through Hound’s execute_script/1 function. Something like this should work:

Hound.Helpers.ScriptExecution.execute_script("Object.defineProperty(navigator, 'userAgent', {get: function(){ return 'Your Custom User Agent'; }});")

Regarding authenticated proxies, I’ve found that the best approach is to configure it at the WebDriver level rather than through Hound directly. If you’re using PhantomJS, you can pass the proxy settings as command-line arguments when starting the PhantomJS server.

One thing to keep in mind is that these configurations might affect the behavior of your tests, so make sure to document these changes and potentially create separate test environments for different configurations. It’s also worth checking if these modifications are necessary for all your tests or just specific scenarios.

hey aroberts, i’ve messed with hound too. for user agent, try this:

execute_script("navigator.userAgent = 'My Cool Agent';")

for proxies, launch phantomjs separately with proxy args, then point hound to it:

System.cmd("phantomjs", ["--proxy=1.2.3.4:8080", "--webdriver=9999"])
# connect hound to localhost:9999

hope that helps!

I’ve faced similar challenges with Hound and found some workarounds. For changing the user agent, you can use Hound.Helpers.Session.set_window_size/3 to execute JavaScript that modifies the navigator.userAgent. It’s not ideal, but it works.

As for authenticated proxies, I ended up using a custom PhantomJS script and launching it separately, then connecting Hound to that instance. You can pass proxy settings to PhantomJS via command line arguments, then set up Hound to connect to that running instance.

Here’s a rough example of how you might set up the PhantomJS script:

phantomjs_args = [
  "--proxy=proxy.example.com:8080",
  "--proxy-auth=username:password",
  "--webdriver=4444"
]
System.cmd("phantomjs", phantomjs_args)

# Then in your test setup:
Application.put_env(:hound, :driver, {Hound.Drivers.Phantomjs, :browser, [url: "http://localhost:4444"]})

Hope this helps point you in the right direction!