Skip to content

Commit

Permalink
Add ability to set user-agent to use
Browse files Browse the repository at this point in the history
#58

This change allows users to specify the user agent a driver should use via the `config.yml`. This will only apply to the **Firefox**, **Chrome** and **PhantomJS** drivers.

The changes involved taking the argument passed in and correctly
correctly configuring the underlying driver to use it. In the case of
**Firefox** this involved updating the profile. For **Chrome** it was
simply adding an additional flag.

**PhantomJS** proved to be more of a problem. You have to add a header
via **Poltergiest**, however it only last for the current session i.e
test. This means before every scenario you have to keep adding it back
in.

This meant a new `before()` hook was added to handle this.

Other changes include tests and docoumentation.
  • Loading branch information
Cruikshanks committed Jun 5, 2017
1 parent 28c7587 commit f6be91c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
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
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
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

0 comments on commit f6be91c

Please sign in to comment.