require 'testmgr' require 'sauce_whisk' module Scoutui::Commands class Strategy USER_AGENTS = { 'MAC_CHROME' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36', 'WIN81_IE11' => 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko', 'WIN7_IE10' => 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)', 'WIN10_EDGE13' => '#Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586', 'WIN10_CHROME_BETA' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.21 Safari/537.36', 'YOSE_CHROME_BETA' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36' } attr_accessor :drv attr_accessor :profile def getDriver() @drv end def loadModel(f) rc=true begin rc=Scoutui::Utils::TestUtils.instance.loadModel(f) rescue => ex rc=false Scoutui::Logger::LogMgr.instance.fatal __FILE__ + (__LINE__).to_s + " loadModel(#{f}) => Failed" end Testmgr::TestReport.instance.getReq("ScoutUI").testcase('Setup').add(rc, "Verify load model #{f}.") if !rc Scoutui::Utils::TestUtils.instance.setState(:abort, "Failed to load model file #{f}.") end rc end def isHeadlessBrowser?(browserType) !browserType.match(/(phantom|phantomjs|ghost|ghostdriver)/i).nil? end def isHeadlessCap?(caps) !caps.nil? && caps.has_key?(:browser) && isHeadlessBrowser?(caps[:browser]) end def setViewport() _viewport = Scoutui::Utils::TestUtils.instance.getViewPort() if !_viewport.nil? target_size = Selenium::WebDriver::Dimension.new(_viewport[:width], _viewport[:height]) getDriver().manage.window.size = target_size end end def navigate(url) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " [enter]:navigate(#{url})" rc = false begin processCommand('navigate(' + url + ')', nil) rc=true rescue => ex Scoutui::Logger::LogMgr.instance.warn "Error during processing: #{$!}" Scoutui::Logger::LogMgr.instance.warn "Backtrace:\n\t#{ex.backtrace.join("\n\t")}" end rc end def report() Testmgr::TestReport.instance.report() # Testmgr::TestReport.instance.generateReport() end def quit() begin Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " quit()" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " ===== Page Coverage =====" Scoutui::Utils::TestUtils.instance.getPageCoverage().each do |_c| Scoutui::Logger::LogMgr.instance.debug " #{_c}" end metrics=Testmgr::TestReport.instance.getMetrics() Scoutui::Logger::LogMgr.instance.info "Metrics => #{metrics}" if Scoutui::Utils::TestUtils.instance.sauceEnabled? job_id = @drv.session_id SauceWhisk::Jobs.change_status job_id, metrics[:rc] Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Update Sauce tests with job_id #{job_id} to #{metrics[:rc]}" end rescue => ex Scoutui::Logger::LogMgr.instance.warn "Error during processing: #{$!}" Scoutui::Logger::LogMgr.instance.warn "Backtrace:\n\t#{ex.backtrace.join("\n\t")}" end @drv.quit() end def processCommands(cmds) Scoutui::Commands::processCommands(cmds, getDriver()) end def processCommand(_action, e=nil) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " [enter]:processCommand(#{_action})" Scoutui::Commands::processCommand(_action, e, getDriver()) end def setup(dut) caps = Selenium::WebDriver::Remote::Capabilities.send(dut[:browserName]) caps.version = dut[:version] caps.platform = dut[:platform] caps[:name] = dut[:full_description] caps end def initialize(opts=nil) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Scoutui::Commands.initialize(#{opts})" @profile=nil browserType = Scoutui::Base::UserVars.instance.getBrowserType() caps = Scoutui::Utils::TestUtils.instance.getCapabilities() ## # If running on Sauce, we need the capabilities. ## if Scoutui::Utils::TestUtils.instance.sauceEnabled? caps = Scoutui::Utils::TestUtils.instance.getSauceCaps() caps = Scoutui::Utils::TestUtils.instance.getCapabilities() if caps.nil? client=nil proxy=nil Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Capabilities => #{caps.to_s}" if caps.nil? caps = { :platform => "Mac OS X 10.9", :browserName => "chrome", :version => "beta", :full_description => 'Rover Test' } elsif caps.has_key?(:platform) && caps[:platform].match(/os x/i) tmpCaps = caps if caps.has_key?(:deviceName) && caps[:deviceName].match(/iphone/i) caps = Selenium::WebDriver::Remote::Capabilities.iphone() elsif caps.has_key?(:browser) && caps[:browser].match(/chrome/i) caps = Selenium::WebDriver::Remote::Capabilities.chrome() elsif caps.has_key?(:browser) && caps[:browser].match(/firefox/i) caps = Selenium::WebDriver::Remote::Capabilities.firefox() elsif caps.has_key?(:browser) && caps[:browser].match(/safari/i) caps = Selenium::WebDriver::Remote::Capabilities.safari() end tmpCaps.each_pair do |k, v| caps[k.to_s]=v end elsif caps.has_key?(:platform) && caps[:platform].match(/windows/i) tmpCaps = caps if caps.has_key?(:browser) && caps[:browser].match(/edge/i) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Capabilities[browser] : EDGE" # https://support.saucelabs.com/customer/portal/questions/16217472-microsoft-edge-ruby-bindings if caps.has_key?(:browserName) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Ruby bindings workaround for Edge" caps={} else Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " edge()" caps = Selenium::WebDriver::Remote::Capabilities.edge() end elsif caps.has_key?(:browser) && caps[:browser].match(/chrome/i) caps = Selenium::WebDriver::Remote::Capabilities.chrome() elsif caps.has_key?(:browser) && caps[:browser].match(/firefox/i) caps = Selenium::WebDriver::Remote::Capabilities.firefox() else caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer() end tmpCaps.each_pair do |k, v| Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " caps[#{k.to_s}] = #{v}" caps[k.to_s]=v end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Remote Caps => #{caps.to_s}" elsif caps.has_key?(:deviceName) && caps[:deviceName].match(/(iphone|ipad)/i) && !caps[:deviceName].match(/simulator/i) caps = Selenium::WebDriver::Remote::Capabilities.iphone() caps['platform'] = 'OS X 10.10' caps['version'] = '9.1' caps['deviceName'] = 'iPhone 6 Plus' caps['deviceOrientation'] = 'portrait' if caps['deviceName'].match(/iphone/i) client = Selenium::WebDriver::Remote::Http::Default.new client.timeout = 360 # seconds – default is 60 end elsif caps.has_key?(:deviceName) && caps[:deviceName].match(/(iphone|ipad)/i) && caps[:deviceName].match(/simulator/i) proxyVal = "localhost:8080" proxy = Selenium::WebDriver::Proxy.new( :http => proxyVal, :ftp => proxyVal, :ssl => proxyVal ) tmpCaps = caps caps = Selenium::WebDriver::Remote::Capabilities.iphone(:proxy => proxy) tmpCaps.each_pair do |k, v| caps[k.to_s]=v end client = Selenium::WebDriver::Remote::Http::Default.new client.timeout = 360 # seconds – default is 60 end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Capabilities => #{caps.to_s}" sauce_endpoint = "http://#{ENV['SAUCE_USERNAME']}:#{ENV['SAUCE_ACCESS_KEY']}@ondemand.saucelabs.com:80/wd/hub" begin if client.nil? Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " caps => #{caps}" @drv=Selenium::WebDriver.for :remote, :url => sauce_endpoint, :desired_capabilities => caps # setup(caps) else @drv=Selenium::WebDriver.for :remote, :url => sauce_endpoint, :http_client => client, :desired_capabilities => caps # setup(caps) end rescue => ex Scoutui::Logger::LogMgr.instance.debug "Error during processing: #{$!}" Scoutui::Logger::LogMgr.instance.debug "Backtrace:\n\t#{ex.backtrace.join("\n\t")}" end # The following print to STDOUT is useful when running on JENKINS with SauceLabs plugin # Reference: # https://wiki.saucelabs.com/display/DOCS/Setting+Up+Reporting+between+Sauce+Labs+and+Jenkins puts "SauceOnDemandSessionID=#{@drv.session_id} job-name=#{caps[:name]}" elsif isHeadlessCap?(caps) || isHeadlessBrowser?(browserType) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Headless browser : ::<#{caps}, #{browserType}>" _cap=Selenium::WebDriver::Remote::Capabilities.phantomjs('phantomjs.page.settings.userAgent' => USER_AGENTS['YOSE_CHROME_BETA']) selenium_server=Scoutui::Utils::TestUtils.instance.getSeleniumServer() selenium_server='http://localhost:8001' if selenium_server.nil? if isHeadlessCap?(caps) && caps.has_key?(:useragent) _cap=Selenium::WebDriver::Remote::Capabilities.phantomjs('phantomjs.page.settings.userAgent' => caps[:useragent]) end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Headless capabilities : #{_cap}" _connected=false _tm={:start => Time.now, :end => nil } begin @drv=Selenium::WebDriver.for :remote, :url => selenium_server, :desired_capabilities => _cap _tm[:end]=Time.now _connected=true setViewport() rescue Errno::ECONNREFUSED => ex Scoutui::Logger::LogMgr.instance.fatal __FILE__ + (__LINE__).to_s + " Unable to connect to GhostDriver" Scoutui::Utils::TestUtils.instance.setState(:abort, __FILE__ + (__LINE__).to_s + " Unable to connect to GhostDriver : #{ex.class.to_s}: #{ex}") rescue => ex Scoutui::Utils::TestUtils.instance.setState(:abort, __FILE__ + (__LINE__).to_s + " : #{ex.class.to_s}: #{ex}") Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " #{ex.class.to_s} Error during processing: #{ex}" Scoutui::Logger::LogMgr.instance.warn ex.backtrace end _tm[:end]=Time.now if _tm[:end].nil? Testmgr::TestReport.instance.getReq('Execution').tc('Status').add(_connected, "Connect to GhostDriver on #{selenium_server}", _tm[:end] - _tm[:start]) else Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Create WebDriver: #{browserType.to_s}" if @profile.nil? @drv=Selenium::WebDriver.for browserType.to_sym else @drv=Selenium::WebDriver.for browserType.to_sym, :profile => @profile end setViewport() end if opts.is_a?(Hash) && opts.has_key?(:loadModel).nil? loadModel(opts[:loadModel]) end end end end