Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set user-agent to use #60

Merged
merged 1 commit into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ stop_on_error: 1
# elements that take some time to load you can increase this default.
max_wait_time: 5

# Tell the driver Quke is using to send a different user-agent value to the site
# under test. Useful if you want the underlying driver to spoof what kind of
# browser the request is coming from. For example you may want to pretend to be
# a mobile browser so you can check what you get back versus the desktop
# version. Or you want to pretend to be another kind of browser, because the one
# you have is not supported by the site.
user_agent: "Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)"

# Anything you place under the 'custom' node in the `.config.yml` file will be
# available within your steps and page objects by calling
# `Quke::Quke.config.custom`. So using the example below we could access its
Expand Down
24 changes: 24 additions & 0 deletions lib/features/support/before_hook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'quke/configuration'

Before('~@nonweb') do
# We have to make a special case for phantomjs when it comes to implementing
# the ability to override the user agent. Unlike the selinium backed drivers
# specifying the user agent is not part of the arguments we pass in when
# initialising the driver. Instead its something we call on the driver once
# its been instantiated
# https://github.com/teampoltergeist/poltergeist#manipulating-request-headers
# That might not have been so bad, the folks behind poltergeist have also
# made it so that custom changes to the header only last for as long as the
# test is running. Once a test finishes, the changes are lost.
# Hence the only way we can ensure its set across all tests is by making use
# of the Before hook, and adding the User-Agent header each time.
if Quke::Quke.config.driver == 'phantomjs'
unless Quke::Quke.config.user_agent.empty?
page.driver.add_header(
'User-Agent',
Quke::Quke.config.user_agent,
permanent: true
)
end
end
end
12 changes: 12 additions & 0 deletions lib/quke/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ def max_wait_time
@data['max_wait_time']
end

# Return the value set for +user_agent+.
#
# Useful if you want the underlying driver to spoof what kind of browser the
# request is coming from. For example you may want to pretend to be a mobile
# browser so you can check what you get back versus the desktop version. Or
# you want to pretend to be another kind of browser, because the one you
# have is not supported by the site.
def user_agent
@data['user_agent']
end

# Return the hash of +browserstack+ options.
#
# If you select the browserstack driver, there are a number of options you
Expand Down Expand Up @@ -165,6 +176,7 @@ def default_data!(data)
'pause' => (data['pause'] || '0').to_s.downcase.strip.to_i,
'stop_on_error' => (data['stop_on_error'] || 'false').to_s.downcase.strip,
'max_wait_time' => (data['max_wait_time'] || Capybara.default_max_wait_time).to_s.downcase.strip.to_i,
'user_agent' => (data['user_agent'] || '').strip,
'custom' => (data['custom'] || nil)
)
end
Expand Down
8 changes: 7 additions & 1 deletion lib/quke/driver_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def phantomjs
# browser: :chrome,
# switches: [
# "--proxy-server=localhost:8080",
# "--proxy-bypass-list=127.0.0.1,192.168.0.1"
# "--proxy-bypass-list=127.0.0.1,192.168.0.1",
# "--user-agent=Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)"
# ]
# )
#
Expand All @@ -145,6 +146,8 @@ def chrome
result.push("--proxy-server=#{host}:#{port}") if config.use_proxy?
result.push("--proxy-bypass-list=#{no_proxy}") unless config.proxy['no_proxy'].empty?

result.push("--user-agent=#{config.user_agent}") unless config.user_agent.empty?

result
end
# rubocop:enable Metrics/AbcSize
Expand All @@ -160,6 +163,7 @@ def chrome
# http: "10.10.2.70:8080",
# ssl: "10.10.2.70:8080"
# )
# my_profile['general.useragent.override'] = "Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)"
# Capybara::Selenium::Driver.new(
# app,
# profile: my_profile
Expand Down Expand Up @@ -190,6 +194,8 @@ def firefox

profile.proxy = Selenium::WebDriver::Proxy.new(settings) if config.use_proxy?

profile['general.useragent.override'] = config.user_agent unless config.user_agent.empty?

profile
end
# rubocop:enable Metrics/AbcSize
Expand Down
1 change: 1 addition & 0 deletions spec/data/.user_agent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
user_agent: "Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)"
18 changes: 17 additions & 1 deletion spec/quke/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@
end
end

describe '#user_agent' do
context 'when NOT specified in the config file' do
it "defaults to ''" do
Quke::Configuration.file_location = data_path('.no_file.yml')
expect(subject.user_agent).to eq('')
end
end

context 'when specified in the config file' do
it 'matches the config file' do
Quke::Configuration.file_location = data_path('.user_agent.yml')
expect(subject.user_agent).to eq('Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)')
end
end
end

describe '#proxy' do
context 'when NOT specified in the config file' do
it 'defaults to blank values' do
Expand Down Expand Up @@ -245,7 +261,7 @@
Quke::Configuration.file_location = data_path('.no_file.yml')
# rubocop:disable Style/StringLiterals
expect(subject.to_s).to eq(
"{\"features_folder\"=>\"features\", \"app_host\"=>\"\", \"driver\"=>\"phantomjs\", \"pause\"=>0, \"stop_on_error\"=>\"false\", \"max_wait_time\"=>2, \"custom\"=>nil, \"browserstack\"=>{\"username\"=>\"\", \"auth_key\"=>\"\"}, \"proxy\"=>{\"host\"=>\"\", \"port\"=>0, \"no_proxy\"=>\"\"}}"
"{\"features_folder\"=>\"features\", \"app_host\"=>\"\", \"driver\"=>\"phantomjs\", \"pause\"=>0, \"stop_on_error\"=>\"false\", \"max_wait_time\"=>2, \"user_agent\"=>\"\", \"custom\"=>nil, \"browserstack\"=>{\"username\"=>\"\", \"auth_key\"=>\"\"}, \"proxy\"=>{\"host\"=>\"\", \"port\"=>0, \"no_proxy\"=>\"\"}}"
)
# rubocop:enable Style/StringLiterals
end
Expand Down
42 changes: 42 additions & 0 deletions spec/quke/driver_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@
end
end

context 'a user agent has been set in the .config.yml' do
it 'is not in the array returned. Unlike the other drivers we can only override the user agent when registering the driver' do
Quke::Configuration.file_location = data_path('.user_agent.yml')
config = Quke::Configuration.new
expect(Quke::DriverConfiguration.new(config).phantomjs).to eq(
[
'--load-images=no',
'--disk-cache=false',
'--ignore-ssl-errors=yes'
]
)
end
end

end

describe '#chrome' do
Expand Down Expand Up @@ -107,6 +121,18 @@
end
end

context 'a user agent has been set in the .config.yml' do
it 'returns an array containing the specified user-agent' do
Quke::Configuration.file_location = data_path('.user_agent.yml')
config = Quke::Configuration.new
expect(Quke::DriverConfiguration.new(config).chrome).to eq(
[
"--user-agent=#{config.user_agent}"
]
)
end
end

end

describe '#firefox' do
Expand Down Expand Up @@ -157,6 +183,22 @@
end
end

context 'a user agent has been set in the .config.yml' do
it 'returns an array containing the specified user-agent' do
Quke::Configuration.file_location = data_path('.user_agent.yml')
config = Quke::Configuration.new
profile = Quke::DriverConfiguration.new(config).firefox

# See spec/helpers.rb#read_profile_preferences for details of why we
# need to test the profile's properties in this way
preferences = read_profile_preferences(profile)

expect(preferences).to include(
'user_pref("general.useragent.override", "Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)")'
)
end
end

end

describe '#browserstack_url' do
Expand Down