require_relative './commands' module Scoutui::Commands class ClickObject < Command def _whenClicked(page_elt) return if page_elt.nil? if page_elt.is_a?(Hash) && page_elt.has_key?('when_clicked') Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Verify #{page_elt['when_clicked']}" page_elt['when_clicked'].each do |_elt| _r = _elt.keys[0].to_s _pg = _elt[_r] # _c = Scoutui::Commands::VerifyElement.new("verifyelement(" + _elt + ")") # _c.execute(@drv) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " _whenClicked -> assertions (#{_pg}, #{_r.to_s})" if _pg.is_a?(Array) _pg.each do |_pg2| isVisible=Scoutui::Base::Assertions.instance.isVisible(@drv, _pg2, _r) end elsif _pg.is_a?(String) isVisible=Scoutui::Base::Assertions.instance.isVisible(@drv, _pg, _r) else Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " => #{_pg}" end end end end def execute(drv, e=nil) retries = Scoutui::Utils::TestUtils.instance.retries() retries = 1 if retries < 1 waitTime = Scoutui::Utils::TestUtils.instance.getDefaultWait Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " click_object.execute: #{@cmd} with #{retries} retries" obj=nil @drv=drv if !drv.nil? bm=nil all_windows = @drv.window_handles.size _req = Scoutui::Utils::TestUtils.instance.getReq() _locator = @cmd.match(/^\s*click\s*\((.*)\)\s*$/i)[1].to_s.strip Scoutui::Logger::LogMgr.instance.command.info __FILE__ + (__LINE__).to_s + " clickObject => #{_locator}" # _locator = Scoutui::Base::UserVars.instance.get(_locator) _locator = Scoutui::Base::UserVars.instance.normalize(_locator) Scoutui::Logger::LogMgr.instance.command.info __FILE__ + (__LINE__).to_s + " | translate : #{_locator}" if Scoutui::Utils::TestUtils.instance.isDebug? _clicked=false _startTime=Time.now _endTime=nil _clickTime=0 isEnabled=nil begin if _locator (0..retries).each do |_i| _retry = true Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Retry #{_i} ======> click(#{_locator})" begin _startTime=Time.now wait = Selenium::WebDriver::Wait.new(:timeout => waitTime) isEnabled = wait.until { obj=Scoutui::Base::QBrowser.getElement(@drv, _locator, Scoutui::Commands::Utils.instance.getFrameSearch(), Scoutui::Commands::Utils.instance.getTimeout) obj.is_a?(Selenium::WebDriver::Element) && obj.enabled? && obj.displayed? } Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " isEnabled : #{isEnabled}" if isEnabled # && obj.is_a?(Selenium::WebDriver::Element) @drv.action.move_to(obj).perform bm=Benchmark.measure { obj.click } _endTime=Time.now _clickTime = (_endTime - _startTime) setBenchmark(bm) _retry = false _clicked=true if e.is_a?(Hash) && e.has_key?('page') && e['page'].has_key?('highlight') && e['page']['highlight'] # # page: # highlight: true # obj=Scoutui::Base::QBrowser.highlight(@drv, obj) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " highlight then click : #{obj}" end thenClause = Scoutui::Commands::ThenClause.new(@drv) doUntil = Scoutui::Commands::DoUntil.new(@drv) _tm= {:start => Time.now, :end => 0 } _bUntil = doUntil.do_until(e) _tm[:end]=Time.now Testmgr::TestReport.instance.getReq(_req).testcase('until').add(_bUntil, Scoutui::Base::UserVars.instance.normalize("Verify after click, do-until met #{_bUntil}"), (_tm[:end] - _tm[:start])) elsif obj.is_a?(Selenium::WebDriver::Element) && obj.displayed? Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " object is displayed, but not enabled" end rescue Selenium::WebDriver::Error::StaleElementReferenceError => ex Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Retry #{_i} - Selenium::WebDriver::Error::UnknownError" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + ex.backtrace.join("\n\t") _retry=true rescue Selenium::WebDriver::Error::TimeOutError => ex _retry=true rescue Selenium::WebDriver::Error::UnknownError => ex Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Retry #{_i} - Selenium::WebDriver::Error::UnknownError" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + ex.backtrace.join("\n\t") _retry=true end break if !_retry sleep(0.25) end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " clicked(#{_locator}): #{_clicked}" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " o isWebDriverElement : #{obj.is_a?(Selenium::WebDriver::Element)}" # isEnabled = obj.enabled? if obj.is_a?(Selenium::WebDriver::Element) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " o enabled : #{isEnabled}" # if obj.is_a?(Selenium::WebDriver::Element) && !obj.enabled? if !isEnabled.nil? && !isEnabled Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + "o Attemping to click a disabled element : #{_locator}" end # puts __FILE__ + (__LINE__).to_s + " Pause for debug"; STDIN.gets page_elt = Scoutui::Utils::TestUtils.instance.getPageElement(_locator) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " PageElement(#{_locator}) => #{page_elt}" if all_windows > @drv.window_handles.size Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " WINDOW was CLOSED. Activate first window as our default"; @drv.switch_to.window @drv.window_handles.first Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Switched to first window" end _whenClicked(page_elt) if e.is_a?(Hash) && e.has_key?('page') && e['page'].has_key?('then') thenClause = Scoutui::Commands::ThenClause.new(@drv) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " thenClause => #{thenClause.class}" thenClause.execute(e) end # _then(e) else Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " Unable to click object that is not displayed." end rescue => ex Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " #{ex.class.to_s} : Error during processing: #{ex.message}" Scoutui::Logger::LogMgr.instance.warn " Backtrace:\n\t#{ex.backtrace.join("\n\t")}" end Scoutui::Logger::LogMgr.instance.asserts.info "Verify object to click exists #{_locator} : #{obj.class.to_s} - #{!obj.nil?.to_s}" Scoutui::Logger::LogMgr.instance.asserts.info __FILE__ + (__LINE__).to_s + " Verify clicked #{_locator} - #{_clicked.to_s}" Testmgr::TestReport.instance.getReq(_req).testcase('click').add(!obj.nil?, "Verify object to click exists #{_locator} : #{obj.class.to_s}") Testmgr::TestReport.instance.getReq(_req).testcase('click').add(_clicked, "Verify clicked #{_locator}", _clickTime) if !_clicked Scoutui::Logger::LogMgr.instance.asserts.info __FILE__ + (__LINE__).to_s + " FAILED CLICK: #{_locator}" # @drv.save_screenshot("/tmp/clickobject.failed.png") end setResult(_clicked) end end end