Skip to content

Commit

Permalink
added present?, user agent on driver init
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-voronenko committed Aug 17, 2017
1 parent 8b7c64a commit 7a9f97c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 65 deletions.
33 changes: 28 additions & 5 deletions generators/config/templates/capybara.rb
Expand Up @@ -37,6 +37,16 @@ module CapybaraHelpers
extend Howitzer::CapybaraHelpers
end

def cloud_user_agent(caps)
if CapybaraHelpers.chrome_browser?
caps['chromeOptions'] = { 'args' => ["--user-agent=#{Howitzer.user_agent}"] }
elsif CapybaraHelpers.ff_browser?
profile = Selenium::WebDriver::Firefox::Profile.new
profile['general.useragent.override'] = Howitzer.user_agent
caps[:firefox_profile] = profile
end
end

# :selenium driver

Capybara.register_driver :selenium do |app|
Expand All @@ -48,15 +58,15 @@ module CapybaraHelpers
profile['network.automatic-ntlm-auth.allow-non-fqdn'] = true
profile['network.ntlm.send-lm-response'] = true
profile['network.automatic-ntlm-auth.trusted-uris'] = Howitzer.app_host
profile['general.useragent.override'] = Howitzer.user_agent if Howitzer.user_agent
profile['general.useragent.override'] = Howitzer.user_agent if Howitzer.user_agent.present?
end
options = Selenium::WebDriver::Firefox::Options.new(profile: ff_profile)
params[:options] = options
end
if CapybaraHelpers.chrome_browser?
args = []
args << 'start-fullscreen' if Howitzer.maximized_window
args << "user-agent=#{Howitzer.user_agent}" if Howitzer.user_agent
args << "user-agent=#{Howitzer.user_agent}" if Howitzer.user_agent.present?
params[:options] = Selenium::WebDriver::Chrome::Options.new(args: args) if Howitzer.maximized_window
end
Capybara::Selenium::Driver.new app, params
Expand All @@ -67,7 +77,7 @@ module CapybaraHelpers
Capybara.register_driver :headless_chrome do |app|
startup_flags = ['headless']
startup_flags << 'start-fullscreen' if Howitzer.maximized_window
startup_flags << "user-agent=#{Howitzer.user_agent}" if Howitzer.user_agent
startup_flags << "user-agent=#{Howitzer.user_agent}" if Howitzer.user_agent.present?
startup_flags.concat(Howitzer.headless_chrome_flags.split(/\s*,\s*/)) if Howitzer.headless_chrome_flags
options = Selenium::WebDriver::Chrome::Options.new(args: startup_flags)
params = { browser: :chrome, options: options }
Expand Down Expand Up @@ -103,7 +113,7 @@ module CapybaraHelpers
caps = {
javascript_enabled: !Howitzer.phantom_ignore_js_errors
}
caps['phantomjs.page.settings.userAgent'] = "WebKit #{Howitzer.user_agent}" if Howitzer.user_agent
caps['phantomjs.page.settings.userAgent'] = "WebKit #{Howitzer.user_agent}" if Howitzer.user_agent.present?
Capybara::Selenium::Driver.new(
app, browser: :phantomjs,
desired_capabilities: caps,
Expand All @@ -122,6 +132,7 @@ module CapybaraHelpers
recordScreenshots: Howitzer.cloud_sauce_record_screenshots,
videoUploadOnPass: Howitzer.cloud_sauce_video_upload_on_pass
)
cloud_user_agent(caps) if Howitzer.user_agent.present?
url = "http://#{Howitzer.cloud_auth_login}:#{Howitzer.cloud_auth_pass}@ondemand.saucelabs.com:80/wd/hub"
CapybaraHelpers.cloud_driver(app, caps, url)
end
Expand All @@ -134,6 +145,7 @@ module CapybaraHelpers
idletimeout: Howitzer.cloud_testingbot_idle_timeout,
screenshot: Howitzer.cloud_testingbot_screenshots
)
cloud_user_agent(caps) if Howitzer.user_agent.present?
url = "http://#{Howitzer.cloud_auth_login}:#{Howitzer.cloud_auth_pass}@hub.testingbot.com/wd/hub"
CapybaraHelpers.cloud_driver(app, caps, url)
end
Expand All @@ -147,6 +159,7 @@ module CapybaraHelpers
)
caps[:resolution] = Howitzer.cloud_bstack_resolution if Howitzer.cloud_bstack_resolution.present?
caps[:device] = Howitzer.cloud_bstack_mobile_device if Howitzer.cloud_bstack_mobile_device.present?
cloud_user_agent(caps) if Howitzer.user_agent.present?
url = "http://#{Howitzer.cloud_auth_login}:#{Howitzer.cloud_auth_pass}@hub.browserstack.com/wd/hub"
CapybaraHelpers.cloud_driver(app, caps, url)
end
Expand All @@ -167,7 +180,7 @@ module CapybaraHelpers
' Check your settings, it should be one of' \
' [:ie, :iexplore, :ff, :firefox, :chrome, safari]'
end

cloud_user_agent(caps) if Howitzer.user_agent.present?
Capybara::Selenium::Driver.new(app, browser: :remote, url: Howitzer.selenium_hub_url, desired_capabilities: caps)
end

Expand All @@ -183,6 +196,7 @@ module CapybaraHelpers
cap['record_video'] = Howitzer.cloud_cbt_record_video
cap['record_network'] = Howitzer.cloud_cbt_record_network
cap['max_duration'] = Howitzer.cloud_max_duration
cloud_user_agent(cap) if Howitzer.user_agent.present?
url = "http://#{URI.escape(Howitzer.cloud_auth_login, /@/)}:#{Howitzer.cloud_auth_pass}"\
'@hub.crossbrowsertesting.com/wd/hub'
CapybaraHelpers.cloud_driver(app, cap, url)
Expand All @@ -200,3 +214,12 @@ module CapybaraHelpers
"capybara-screenshot-#{Gen.serial}"
end
Capybara::Screenshot.prune_strategy = :keep_all

if Howitzer.user_agent.present?
driver = Capybara.current_session.driver
if driver.respond_to?(:add_headers)
driver.add_headers('User-Agent' => Howitzer.user_agent)
elsif driver.respond_to?(:header)
driver.header('User-Agent', Howitzer.user_agent)
end
end
2 changes: 1 addition & 1 deletion generators/config/templates/default.yml
Expand Up @@ -17,7 +17,7 @@
###########################################################
# TEST ENVIRONMENTS SETTINGS #
###########################################################
user_agent:
user_agent: # if blank, then default agent
page_load_idle_timeout: 20
maximized_window: true # Specify maximized browser window size

Expand Down
6 changes: 0 additions & 6 deletions generators/cucumber/templates/hooks.rb
Expand Up @@ -2,12 +2,6 @@
Capybara.use_default_driver
Howitzer::Log.print_feature_name(scenario.feature.name)
Howitzer::Log.print_scenario_name(scenario.name)
driver = Capybara.current_session.driver
if driver.respond_to?(:add_headers)
driver.add_headers('User-Agent' => Howitzer.user_agent)
elsif driver.respond_to?(:header)
driver.header('User-Agent', Howitzer.user_agent)
end
@session_start = CapybaraHelpers.duration(Time.now.utc - Howitzer::Cache.extract(:cloud, :start_time))
end

Expand Down
6 changes: 0 additions & 6 deletions generators/rspec/templates/spec_helper.rb
Expand Up @@ -24,12 +24,6 @@
RSpec.current_example.description
end
Howitzer::Log.print_scenario_name(scenario_name)
driver = Capybara.current_session.driver
if driver.respond_to?(:add_headers)
driver.add_headers('User-Agent' => Howitzer.user_agent)
elsif driver.respond_to?(:header)
driver.header('User-Agent', Howitzer.user_agent)
end
@session_start = CapybaraHelpers.duration(Time.now.utc - Howitzer::Cache.extract(:cloud, :start_time))
end

Expand Down
6 changes: 0 additions & 6 deletions generators/turnip/templates/spec_helper.rb
Expand Up @@ -23,12 +23,6 @@
RSpec.current_example.description
end
Howitzer::Log.print_scenario_name(scenario_name)
driver = Capybara.current_session.driver
if driver.respond_to?(:add_headers)
driver.add_headers('User-Agent' => Howitzer.user_agent)
elsif driver.respond_to?(:header)
driver.header('User-Agent', Howitzer.user_agent)
end
@session_start = CapybaraHelpers.duration(Time.now.utc - Howitzer::Cache.extract(:cloud, :start_time))
end

Expand Down
17 changes: 2 additions & 15 deletions lib/howitzer/capybara_helpers.rb
Expand Up @@ -10,7 +10,7 @@ module CapybaraHelpers
# Testingbot or Browserstack cloud service

def cloud_driver?
%i[sauce testingbot browserstack].include?(Howitzer.driver.to_sym)
%i[sauce testingbot browserstack crossbrowsertesting].include?(Howitzer.driver.to_sym)
end

# @return [Boolean] whether or not current browser is
Expand All @@ -35,7 +35,7 @@ def ff_browser?
# @raise [SelBrowserNotSpecifiedError] if selenium driver and missing browser name

def chrome_browser?
browser? :chrome
browser?(:chrome) || Howitzer.driver == 'headless_chrome'
end

# @return [Boolean] whether or not current browser is Safari.
Expand Down Expand Up @@ -105,8 +105,6 @@ def cloud_driver(app, caps, url)
http_client.read_timeout = Howitzer.cloud_http_idle_timeout
http_client.open_timeout = Howitzer.cloud_http_idle_timeout

apply_user_agent(caps) if Howitzer.user_agent

options = {
url: url,
desired_capabilities: ::Selenium::WebDriver::Remote::Capabilities.new(caps),
Expand All @@ -132,17 +130,6 @@ def cloud_resource_path(kind)

private

def apply_user_agent(caps)
browser = Howitzer.cloud_browser_name
if browser.casecmp('chrome').zero?
caps['chromeOptions'] = { 'args' => ["--user-agent=#{Howitzer.user_agent}"] }
return
end
profile = Selenium::WebDriver::Firefox::Profile.new
profile['general.useragent.override'] = Howitzer.user_agent
caps[:firefox_profile] = profile
end

def browser?(*browser_aliases)
return cloud_browser?(*browser_aliases) if cloud_driver?
return selenium_browser?(*browser_aliases) if selenium_driver? || selenium_grid_driver?
Expand Down
8 changes: 2 additions & 6 deletions lib/howitzer/web/page.rb
@@ -1,6 +1,7 @@
require 'singleton'
require 'rspec/expectations'
require 'addressable/template'
require 'howitzer/capybara_helpers'
require 'howitzer/web/capybara_methods_proxy'
require 'howitzer/web/page_validator'
require 'howitzer/web/element_dsl'
Expand All @@ -23,6 +24,7 @@ class Page
include PageValidator
include ::RSpec::Matchers
include RSpec::Wait
include CapybaraHelpers

# This Ruby callback makes all inherited classes as singleton classes.

Expand Down Expand Up @@ -162,12 +164,6 @@ def reload
Howitzer::Log.info "Reload '#{current_url}'"
visit current_url
end

private

def chrome_browser?
Howitzer.driver == 'headless_chrome' || Howitzer.selenium_browser == 'chrome'
end
end
end
end
20 changes: 0 additions & 20 deletions spec/unit/lib/capybara_helpers_spec.rb
Expand Up @@ -694,24 +694,4 @@
end
end
end

describe '.apply_user_agent' do
let(:caps) { {} }
before { allow(Howitzer).to receive(:user_agent) { 'test_user_agent' } }
subject { apply_user_agent(caps) }
context 'when chrome browser' do
before { allow(Howitzer).to receive(:cloud_browser_name) { 'chrome' } }
it do
subject
expect(caps['chromeOptions']['args'].join).to include(Howitzer.user_agent)
end
end
context 'when firefox browser' do
before { allow(Howitzer).to receive(:cloud_browser_name) { 'firefox' } }
it do
subject
expect(caps[:firefox_profile]).not_to be_nil
end
end
end
end

0 comments on commit 7a9f97c

Please sign in to comment.