From 9ab530274d14f669c1864dbba520b1fa07db5394 Mon Sep 17 00:00:00 2001 From: Sam Nissen Date: Thu, 9 Aug 2018 17:18:48 +0100 Subject: [PATCH] Update Ruby, devices, and deprecated Selenium and Safari methods --- .ruby-version | 2 +- lib/device-info/devices.yaml | 89 +++++++++++++++------ lib/webdriver-user-agent/browser_options.rb | 28 ++++--- lib/webdriver-user-agent/driver.rb | 34 +++----- spec/webdriver-user-agent_spec.rb | 36 +++++---- 5 files changed, 113 insertions(+), 76 deletions(-) diff --git a/.ruby-version b/.ruby-version index 58f65ad..55bc983 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-2.2.1 +ruby-2.4.1 diff --git a/lib/device-info/devices.yaml b/lib/device-info/devices.yaml index f163938..e31cb72 100644 --- a/lib/device-info/devices.yaml +++ b/lib/device-info/devices.yaml @@ -11,44 +11,59 @@ :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 @@ -56,22 +71,44 @@ :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: diff --git a/lib/webdriver-user-agent/browser_options.rb b/lib/webdriver-user-agent/browser_options.rb index f0abcb4..8fea028 100644 --- a/lib/webdriver-user-agent/browser_options.rb +++ b/lib/webdriver-user-agent/browser_options.rb @@ -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]) @@ -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 @@ -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. diff --git a/lib/webdriver-user-agent/driver.rb b/lib/webdriver-user-agent/driver.rb index 69c278b..59a2ed5 100644 --- a/lib/webdriver-user-agent/driver.rb +++ b/lib/webdriver-user-agent/driver.rb @@ -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) diff --git a/spec/webdriver-user-agent_spec.rb b/spec/webdriver-user-agent_spec.rb index f85ee7e..98664b2 100644 --- a/spec/webdriver-user-agent_spec.rb +++ b/spec/webdriver-user-agent_spec.rb @@ -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 @@ -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 @@ -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` @@ -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 @@ -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,hello" - @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 @@ -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