How to Set a Custom User Agent in Ruby

Setting custom User Agent in Selenium Webdriver for PhantomJS with Ruby

This ended up not being "possible" out-of-the-box, as moonfly mentioned in his comments to my question. However, this turns out to be relatively easy to do. I should note, that this did not solve my particular problem. I assumed, perhaps naively, that I was getting strange results because of the User-Agent 'Ruby'. Turns out I spent hours figuring this out for nothing; I still get the same result.

But, for those of you that this will help, you'll have to make a quick monkey-patch to Selenium. Nasty, gross, ugly, hacky, I know. But, it did the trick.

Note, that this is a monkey-patch for Ruby's selenium-webdriver, version 2.43.0 -- if you have another version, there is no guarantee this will work.

module Selenium
module WebDriver
module Remote
module Http

class Default < Common
private

def request(verb, url, headers, payload, redirects = 0)
# THIS IS WHERE OUR CUSTOM CHANGE BEGINS
headers.merge!('User-Agent' => 'Whatever User Agent You May Want')
# THIS IS WHERE OUR CUSTOM CHANGE ENDS

request = new_request_for(verb, url, headers, payload)

retries = 0
begin
response = response_for(request)
rescue Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EADDRINUSE
# a retry is sometimes needed on Windows XP where we may quickly
# run out of ephemeral ports
#
# A more robust solution is bumping the MaxUserPort setting
# as described here:
#
# http://msdn.microsoft.com/en-us/library/aa560610%28v=bts.20%29.aspx
raise if retries >= MAX_RETRIES

request = new_request_for(verb, url, headers, payload)
retries += 1

retry
rescue Errno::ECONNREFUSED => ex
if use_proxy?
raise ex.class, "using proxy: #{proxy.http}"
else
raise
end
end

if response.kind_of? Net::HTTPRedirection
raise Error::WebDriverError, "too many redirects" if redirects >= MAX_REDIRECTS
request(:get, URI.parse(response['Location']), DEFAULT_HEADERS.dup, nil, redirects + 1)
else
create_response response.code, response.body, response.content_type
end
end

end

end
end
end
end

Set custom user agent on rails testing

Since you're not specifying a driver Capybara should be using rack_test. With the rack_test driver you can set the user agent header, in your test code before calling visit, with

page.driver.header('User-Agent', 'the user agent string you want')

That should then make request.user_agent accessible in your application code.

A different solution would be register a specific driver for your ipad tests

Capybara.register_driver(:ipad_rack_test) do |app|
Capybara::RackTest::Driver.new(app, :headers => { 'HTTP_USER_AGENT' => 'User agent string' })
end

and then specify your driver as :ipad_rack_test

How to set custom user-agent for Mechanize in Rails

You can set the user agent from an alias

a = Mechanize.new
a.user_agent_alias = 'Mac Safari'

Available aliases are stored in the AGENT_ALIASES constant.

p Mechanize::AGENT_ALIASES

Otherwise, use #user_agent to set your custom user agent.

a = Mechanize.new
a.user_agent = 'Custom agent'

How do I set the user agent for Ruby's RestClient?

RestClient.get 'http://localhost', :user_agent => "myagent"

See https://github.com/rest-client/rest-client/blob/master/lib/restclient.rb

how to set user agent in rspec route assertions

I'm not sure this will work, but here's something to try:

If you look at the source for the RouteToMatcher, it just parses a controller/action pair (given as a hash or in controller#action format) and any other parameters given along with them and then delegates to ActionDispatch::Assertions::RoutingAssertions#assert_recognizes. But that method builds the request object from the parameters you pass in as the argument to route_to. So there's no easy way to access the request object from your spec. However, to build this dummy request,
it calls

ActionController::TestRequest.new

So you might try

ActionController::TestRequest.any_instance.stub(:user_agent).
and_return "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B329 Safari/8536.25"

Setting a HTTP user agent with mechanize?

Turns out that the issue was that user_agent_alias requires a specific type. All acceptable types are as follows:

  • Linux Firefox (3.6.1)
  • Linux Konqueror (3)
  • Linux Mozilla
  • Mac Firefox (3.6)
  • Mac Mozilla
  • Mac Safari (5)
  • Mac Safari 4
  • Mechanize (default)
  • Windows IE 6
  • Windows IE 7
  • Windows IE 8
  • Windows IE 9
  • Windows Mozilla
  • iPhone (3.0)
  • iPad
  • Android (Motorola Xoom)

Working code:

require 'rubygems'
require 'mechanize'

m = Mechanize.new
m.user_agent_alias = 'Mac Safari 4'
page = m.get("http://whatsmyuseragent.com/")
html = Nokogiri::HTML(page.body)
puts html.xpath('//*[(@id = "body_lbUserAgent")]').map(&:content)


Related Topics



Leave a reply



Submit