Skip to content

Commit

Permalink
Update Ruby, devices, and deprecated Selenium and Safari methods
Browse files Browse the repository at this point in the history
  • Loading branch information
samnissen committed Aug 9, 2018
1 parent 752a5f5 commit 9ab5302
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
@@ -1 +1 @@
ruby-2.2.1
ruby-2.4.1
89 changes: 63 additions & 26 deletions lib/device-info/devices.yaml
Expand Up @@ -11,67 +11,104 @@
:width: 1366
:height: 768
:iphone_default: &iphone_default
:user_agent: "Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B435 Safari/600.1.4"
:user_agent: "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1"
:portrait:
:width: 375
:height: 559
:height: 635
:landscape:
:width: 667
:width: 724
:height: 375
:iphone:
:iphone: &iphone
<<: *iphone_default
:iphone6splus:
:iphonex:
<<: *iphone_default
:iphone8plus: &iphone8plus
<<: *iphone_default
:portrait:
:width: 414
:height: 628
:height: 622
:landscape:
:width: 736
:height: 414
:iphone6s:
<<: *iphone_default
:iphone6plus:
:iphone8: &iphone8
<<: *iphone_default
:portrait:
:width: 414
:height: 628
:width: 375
:height: 553
:landscape:
:width: 736
:height: 414
:width: 667
:height: 375
:iphone7plus:
<<: *iphone8plus
:iphone7:
<<: *iphone8
:portrait:
:iphone6splus:
<<: *iphone8plus
:iphone6s:
<<: *iphone8
:iphone6plus:
<<: *iphone8plus
:iphone6:
<<: *iphone_default
:iphone5s:
<<: *iphone8
:iphone5s: &iphone5s
<<: *iphone_default
:portrait:
:width: 320
:height: 460
:height: 454
:landscape:
:width: 568
:height: 212
:iphone4s:
:height: 320
:iphonese:
<<: *iphone5s
:iphone5:
<<: *iphone5s
:iphone4s: &iphone4s
<<: *iphone_default
:portrait:
:width: 320
:height: 356
:landscape:
:width: 480
:height: 196
:ipad:
:user_agent: "Mozilla/5.0(iPad; U; CPU OS 8_1_1 like Mac OS X) AppleWebKit/600.14 (KHTML, like Gecko) Version/8.0 Mobile/12B435 Safari/600.1.4"
:iphone4:
<<: *iphone4s
:ipad: &ipad
:user_agent: "Mozilla/5.0 (iPad; CPU OS 11_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1"
:portrait:
:width: 768
:height: 960
:height: 954
:landscape:
:width: 1024
:height: 704
:ipad_pro:
:user_agent: "Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B137 Safari/601.1"
:height: 698
:ipad_default:
<<: *ipad
:ipad_pro: &ipad_pro
<<: *ipad
:portrait:
:width: 1024
:height: 1302
:height: 1296
:landscape:
:width: 1366
:height: 960
:height: 954
:ipad_pro_12:
<<: *ipad_pro
:ipad_pro_10:
<<: *ipad
:portrait:
:width: 834
:height: 1042
:landscape:
:width: 1112
:height: 764
:ipad_pro_9:
<<: *ipad
:portrait:
:width: 768
:height: 954
:landscape:
:width: 1024
:height: 698
:android_phone:
:user_agent: "Mozilla/5.0 (Linux; U; Android 4.0.1; en-us; sdk Build/ICS_MR0) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
:portrait:
Expand Down
28 changes: 19 additions & 9 deletions lib/webdriver-user-agent/browser_options.rb
Expand Up @@ -12,6 +12,7 @@ def initialize(opts, user_agent_string)
options[:browser] ||= :firefox
options[:agent] ||= :iphone
options[:orientation] ||= :portrait
set_preview_option(options[:safari_technology_preview]) if (@options[:browser] == :safari)

options[:viewport_width], options[:viewport_height] = parse_viewport_sizes(options[:viewport_width], options[:viewport_height])

Expand All @@ -35,14 +36,22 @@ def options
@options ||= {}
end

def set_preview_option(opt)
@stp = opt
@stp = false unless @stp.is_a?(TrueClass)
end

def initialize_for_browser(user_agent_string)
case options[:browser]
when :firefox
options[:profile] ||= Selenium::WebDriver::Firefox::Profile.new
options[:profile]['general.useragent.override'] = user_agent_string
profile ||= Selenium::WebDriver::Firefox::Profile.new
profile['general.useragent.override'] = user_agent_string

options[:options] ||= Selenium::WebDriver::Firefox::Options.new
options[:options].profile = profile
when :chrome
options[:switches] ||= []
options[:switches] << "--user-agent=#{user_agent_string}"
options[:options] ||= Selenium::WebDriver::Chrome::Options.new
options[:options].add_argument "--user-agent=#{user_agent_string}"
when :safari
change_safari_user_agent_string(user_agent_string)
options
Expand All @@ -60,12 +69,13 @@ def parse_viewport_sizes(width, height)
def change_safari_user_agent_string(user_agent_string)
raise "Safari requires a Mac" unless OS.mac?

# Safari only works in technology preview, as of July 2017
# https://github.com/SeleniumHQ/selenium/issues/2904#issuecomment-261736814
Selenium::WebDriver::Safari.technology_preview!

ua = "\\\"#{user_agent_string}\\\"" # escape for shell quoting
cmd = "defaults write com.apple.SafariTechnologyPreview CustomUserAgent \"#{ua}\""
if @stp
Selenium::WebDriver::Safari.technology_preview!
cmd = "defaults write com.apple.SafariTechnologyPreview CustomUserAgent \"#{ua}\""
else
cmd = "defaults write com.apple.Safari CustomUserAgent \"#{ua}\""
end
# Note that `com.apple.SafariTechnologyPreview` is the name specific
# to the beta browser provided by Apple, which is required to use
# Selenium with Safari, as of July 2017.
Expand Down
34 changes: 9 additions & 25 deletions lib/webdriver-user-agent/driver.rb
Expand Up @@ -16,37 +16,21 @@ def for(opts)
options = BrowserOptions.new(opts, user_agent_string)
build_driver_using options
ensure
# Note that `com.apple.SafariTechnologyPreview` is the name specific
# to the beta browser provided by Apple, which is required to use
# Selenium with Safari, as of July 2017.
# https://github.com/SeleniumHQ/selenium/issues/2904#issuecomment-261736814
# If this changes, and this gem is changed to support using the
# golden master Safari, the name of the configuration should be
# changed to `com.apple.Safari`
`defaults delete com.apple.SafariTechnologyPreview CustomUserAgent` if safari?(opts)
if safari?(opts)
case opts[:safari_technology_preview]
when true
`defaults delete com.apple.SafariTechnologyPreview CustomUserAgent`
else
`defaults delete com.apple.Safari CustomUserAgent`
end
end
end

private

def resize_inner_window(driver, width, height)
target_size = Selenium::WebDriver::Dimension.new(width.to_i, height.to_i)

case driver.browser
when :safari
# The other resizing windows method does not work for Safari
# and I'm unsure what the purpose of opening the second window is.
# Regardless, Safari doesn't seem to respond to the script, so
# closing the driver closes the connection to Safari altogether.
# Also, driver.manage.window.size = target_size raises an error:
# Selenium::WebDriver::Error::WebDriverError: unexpected response, code=400, content-type="text/plain"
# Required parameter 'width' has incorrect type: expected number.
driver.manage.window.resize_to target_size.width, target_size.height
else
driver.execute_script("window.open(#{driver.current_url.to_json},'_blank');")
driver.close
driver.switch_to.window driver.window_handles.first
driver.manage.window.size = target_size
end
driver.manage.window.resize_to target_size.width, target_size.height
end

def build_driver_using(options)
Expand Down
36 changes: 21 additions & 15 deletions spec/webdriver-user-agent_spec.rb
Expand Up @@ -6,14 +6,16 @@
require 'selenium-webdriver'
require 'watir'

CHROMEBROWSER_UICHROME_HEIGHT = 72
CHROMEBROWSER_UI_MINIMUM_HEIGHT = 200
FIREFOXBROWSER_UICHROME_HEIGHT = 79
SAFARIBROWSER_UICHROME_HEIGHT = 38
CHROMEBROWSER_UICHROME_HEIGHT = 114
CHROMEBROWSER_UICHROME_HEIGHT_TALL = 41
CHROMEBROWSER_UI_MINIMUM_HEIGHT = 158
FIREFOXBROWSER_UICHROME_HEIGHT = 74
SAFARIBROWSER_UICHROME_HEIGHT = 38

describe "webdriver user agent" do
after :each do
@driver.quit if @driver
@browser.close if @browser rescue nil
@driver.quit if @driver rescue nil
end

# window.innerWidth and window.innerHeight
Expand All @@ -26,7 +28,7 @@
expect(@driver.browser).to eq(:firefox)
expect(@driver.execute_script('return navigator.userAgent')).to include 'iPhone'
expect(@driver.execute_script('return Math.max(document.documentElement.clientWidth, window.innerWidth || 0)')).to eq(375)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(559 - FIREFOXBROWSER_UICHROME_HEIGHT)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(635 - FIREFOXBROWSER_UICHROME_HEIGHT)
end

it "can create a new webdriver driver using safari and iphone 6 plus, and landscape" do
Expand All @@ -47,16 +49,16 @@
end

it "can create a new webdriver driver using chrome and iPad (landscape)" do
@driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :iPad, :orientation => :landscape)
@driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :ipad, :orientation => :landscape)
expect(@driver.browser).to eq(:chrome)
expect(@driver.execute_script('return navigator.userAgent')).to include 'iPad'
expect(@driver.execute_script('return Math.max(document.documentElement.clientWidth, window.innerWidth || 0)')).to eq(1024)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(704 - CHROMEBROWSER_UICHROME_HEIGHT)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(698 - CHROMEBROWSER_UICHROME_HEIGHT)
end

it "can create a new webdriver driver using chrome and iPad Pro (portrait)" do
# Testing the height will fail if your monitor is not tall enough.
# For instance, a 15" MacBook Pro cannot be.
# For instance, a 15" MacBook Pro at default scaled resolution cannot be.
# This will determine if your monitor is tall enough.
# Optionally, you can override with an environment variable:
# `I_HAVE_A_TALL_MONITOR=true rspec spec/this_file.rb`
Expand Down Expand Up @@ -96,7 +98,7 @@
expect(@driver.browser).to eq(:chrome)
expect(@driver.execute_script('return navigator.userAgent')).to include 'Android'
expect(@driver.execute_script('return Math.max(document.documentElement.clientWidth, window.innerWidth || 0)')).to eq(768)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(873 - CHROMEBROWSER_UICHROME_HEIGHT)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(873 - CHROMEBROWSER_UICHROME_HEIGHT_TALL)
end

it "can create a new webdriver driver using firefox and desktop (landscape)" do
Expand Down Expand Up @@ -126,11 +128,14 @@
it "can create a new webdriver driver using an existing firefox profile" do
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.startup.homepage'] = "data:text/html,<title>hello</title>"
@driver = Webdriver::UserAgent.driver(:browser => :firefox, :profile => profile)
options = Selenium::WebDriver::Firefox::Options.new
options.profile = profile
@driver = Webdriver::UserAgent.driver(:browser => :firefox, options: options)

expect(@driver.browser).to eq(:firefox)
expect(@driver.execute_script('return navigator.userAgent')).to include 'iPhone'
expect(@driver.execute_script('return Math.max(document.documentElement.clientWidth, window.innerWidth || 0)')).to eq(375)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(559 - FIREFOXBROWSER_UICHROME_HEIGHT)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(635 - FIREFOXBROWSER_UICHROME_HEIGHT)
end

it "can create a new webdriver driver using firefox and user-specified user agent" do
Expand All @@ -151,15 +156,16 @@
end

it "can create a new webdriver driver, handling for nonsense height and widths" do
@driver = Webdriver::UserAgent.driver(:viewport_width => "abc", :agent => :iphone6)
@driver = Webdriver::UserAgent.driver(:viewport_width => "abc", :agent => :iphone8)
expect(@driver.execute_script('return Math.max(document.documentElement.clientWidth, window.innerWidth || 0)')).to eq(375)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(559 - FIREFOXBROWSER_UICHROME_HEIGHT)
expect(@driver.execute_script('return Math.max(document.documentElement.clientHeight, window.innerHeight || 0)')).to eq(553 - FIREFOXBROWSER_UICHROME_HEIGHT)
end

it "can allow using selenium driver for watir browser" do
@driver = Webdriver::UserAgent.driver(:browser => :firefox, :agent => :iphone, :orientation => :portrait)
@browser = Watir::Browser.new @driver
expect(@browser.url).to include("mozilla.org")
expect(@browser).to be_a(Watir::Browser)
expect(@browser.url).to be_a(String)
end

end

0 comments on commit 9ab5302

Please sign in to comment.