简体   繁体   中英

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?

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.

I spent quite a bit of time trying to get this working. The answer from cevado didn't work for me, but I was able to set the user-agent by setting the following.

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

Hope this helps.

In addition:

  • according to the documentation of current version -> {:hound, "~> 1.0"} , the atom: :user_agent must be used:

     Hound.start_session(%{user_agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"})

Config in config/config.exs (selenium):

config :hound, driver: "chrome_driver", browser: "chrome_headless"

Example module for test purpose:

defmodule Example.Test do
  use Hound.Helpers

  def get_github_home() do
    Hound.start_session(%{user_agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"})
    navigate_to("https://www.github.com/")
    
    IO.inspect(page_source())
    
    Hound.end_session()
  end
end

Usage (iex):

Example.Test.get_github_home()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM