require 'singleton' module Scoutui::Base class Assertions include Singleton attr_accessor :drv def setDriver(_drv) @drv=_drv end def assertPage(my_driver, _pg, _req=nil) if _req.nil? _req = Scoutui::Utils::TestUtils.instance.getReq() end if !_pg.match(/^page\([\w\d]+\)/).nil? # Check if this is a form page_elt = Scoutui::Utils::TestUtils.instance.getPageElement(_pg) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " Process page request #{page_elt} => #{page_elt.class.to_s}" if Scoutui::Utils::TestUtils.instance.isDebug? sub_elts=0 if page_elt.is_a?(Hash) sub_elts = page_elt.select { |_s| page_elt[_s].has_key?("locator") if page_elt[_s].is_a?(Hash) && !page_elt[_s].nil? }.size else Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Process an element (#{page_elt})" end Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " SubElts => #{sub_elts}" if Scoutui::Utils::TestUtils.instance.isDebug? ## # Check if this is a single element. ## if page_elt.is_a?(Hash) && page_elt.has_key?('locator') ## # expected: # wait: page(abc).get(def) where this page_elt has "locator" xpath = page_elt['locator'].to_s Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Verify #{page_elt['locator']}" elsif sub_elts > 0 ## # This pageobject consists of multiple pageobjects. ## Scoutui::Utils::TestUtils.instance.addPageCoverage(_pg) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Validate form" page_elt.each_pair do |_k, _v| begin Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " k,v :: #{_k.to_s}, #{_v.to_s} (#{_v.class.to_s})" if Scoutui::Utils::TestUtils.instance.isDebug? _obj=nil if _v.is_a?(String) puts __FILE__ + (__LINE__).to_s + " #{_v} is a string - next" puts __FILE__ + (__LINE__).to_S + " Pause", gets next end if _v.has_key?('assert_when') && _v['assert_when'].match(/role\s*\=/i) _role = _v['assert_when'].match(/role\s*\=(.*)/i)[1].to_s _expected_role = Scoutui::Utils::TestUtils.instance.getRole() Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " Trigger: expected : #{_expected_role.to_s}, actual: #{_role.to_s}" if Scoutui::Utils::TestUtils.instance.isDebug? if !_expected_role.nil? && !_role.match(/#{_expected_role}/i) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " Skip assertion since conditional assertion #{_v['assert_when']} not met" if Scoutui::Utils::TestUtils.instance.isDebug? next elsif _expected_role.nil? Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " Skip role based assertion since role was not provided" if Scoutui::Utils::TestUtils.instance.isDebug? next end Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " Verify object exists since the role #{_role} matches expected role #{_expected_role.to_s}" if Scoutui::Utils::TestUtils.instance.isDebug? end if Scoutui::Base::Assertions.instance.visible_when_skip(_k, _v) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " SKIP #{_k.to_s} - #{_v.to_s}" if Scoutui::Utils::TestUtils.instance.isDebug? next end if _v.has_key?('locator') _locator = _v['locator'].to_s Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " " + _k.to_s + " => " + _locator # _locator = Scoutui::Utils::TestUtils.instance.getPageElement(_v['locator']) _obj = Scoutui::Base::QBrowser.getFirstObject(my_driver, _locator, Scoutui::Commands::Utils.instance.getTimeout()) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " HIT #{_locator} => #{!_obj.nil?}" end if Scoutui::Base::Assertions.instance.visible_when_always(_k, _v, _obj, _req) next elsif _v.has_key?('visible_when') if _v['visible_when'].match(/always/i) Scoutui::Logger::LogMgr.instance.asserts.info __FILE__ + (__LINE__).to_s + " Verify #{_k} - #{_locator} visible - #{!_obj.nil?.to_s}" if Scoutui::Utils::TestUtils.instance.assertExpected? Testmgr::TestReport.instance.getReq(_req).get_child('visible_when').add(!_obj.nil?, "Verify #{_k} - #{_locator} visible") end elsif assertPageElement(_k, _v, _obj, my_driver, _req) puts __FILE__ + (__LINE__).to_s + " Processed assertPageElement(#{_k}, #{_v})"; # gets ; # elsif visible_when_title(_k, _v, _obj, my_driver, _req) # ; # elsif _v['visible_when'].match(/never/i) # Scoutui::Logger::LogMgr.instance.asserts.info "Verify #{_k} #{_locator} never visible - #{obj.nil?.to_s}" # Testmgr::TestReport.instance.getReq(_req).get_child('visible_when').add(obj.nil?, "Verify #{_k} #{_locator} not visible") elsif _v['visible_when'].match(/role\=/i) _role = _v['visible_when'].match(/role\=(.*)/i)[1].to_s _expected_role = Scoutui::Utils::TestUtils.instance.getRole() Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " Verify object exists if the role #{_role} matches expected role #{_expected_role.to_s}" if Scoutui::Utils::TestUtils.instance.isDebug? if _role==_expected_role.to_s Scoutui::Logger::LogMgr.instance.asserts.info "Verify #{_k} #{_locator} visible when role #{_role} - #{!_obj.nil?.to_s}" if Scoutui::Utils::TestUtils.instance.assertExpected? Testmgr::TestReport.instance.getReq(_req).get_child('visible_when').add(!_obj.nil?, "Verify #{_k} #{_locator} visible when role #{_role}") end end end end rescue => ex Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " Invalid file: #{datafile} - abort processing." puts __FILE__ + (__LINE__).to_s + "Backtrace:\n\t#{ex.backtrace.join("\n\t")}" end end return elsif xpath.is_a?(Hash) xpath.each_pair do |_k, _v| Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " k,v :: #{_k.to_s}, #{_v.to_s}" if _v.has_key?('locator') _locator = _v['locator'].to_s Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " " + _k.to_s + " => " + _locator # _locator = Scoutui::Utils::TestUtils.instance.getPageElement(_v['locator']) _obj = Scoutui::Base::QBrowser.getFirstObject(my_driver, _locator, Scoutui::Commands::Utils.instance.getTimeout()) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " HIT #{_locator} => #{!_obj.nil?}" end end # next end end puts __FILE__ + (__LINE__).to_s + " Pause debug"; gets end # # { reqid : } def isVisible(my_driver, page_elt, _req=nil, _immediate=false) if _req.nil? _req = Scoutui::Utils::TestUtils.instance.getReq() end puts __FILE__ + (__LINE__).to_s + " [isVisible]: #{page_elt}, req:#{_req}" rc=false cmd='visible' _processed=false pageObject=page_elt if page_elt.is_a?(String) && page_elt.match(/^\s*(text|value)\s*\(/) # # text(page(home).get(elt)) # value(page(home).get(elt)) # _processed=true condition = page_elt.match(/(value|text)\((.*)\)/)[1].to_s tmpObj = page_elt.match(/(value|text)\((.*)\)/)[2].to_s expectedVal = page_elt.match(/(value|text)\s*\(.*\)\s*\=\s*(.*)/)[2].to_s xpath = Scoutui::Base::UserVars.instance.get(tmpObj) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " locator : #{xpath}"; #gets obj = Scoutui::Base::QBrowser.getObject(my_driver, xpath, Scoutui::Commands::Utils.instance.getTimeout) puts __FILE__ + (__LINE__).to_s + " #{condition} : #{obj}" if condition.match(/^text/) _txt = obj.text expected_regex = Regexp.new(expectedVal) rc = _!txt.to_s.match(expected_regex).nil? return rc end elsif page_elt.is_a?(String) && page_elt.match(/^\s*(url)\s*\((.*)\)/) current_url = my_driver.current_url.to_s expected_url = page_elt.match(/^\s*(url)\s*\((.*)\)/)[2].to_s puts __FILE__ + (__LINE__).to_s + " verify is URL matches #{current_url} with expected #{expected_url}" expected_regex = Regexp.new(expected_url) rc=!current_url.match(expected_regex).nil? Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " verify current url #{current_url} matches #{rc}" return rc elsif page_elt.is_a?(String) && page_elt.match(/^\s*(visible)\((.*)\)/) # # visible(page(home).get(elt)) # _processed=true cmd = page_elt.match(/(visible)\((.*)\)/)[1].to_s page_elt = page_elt.match(/(visible)\((.*)\)/)[2].to_s if !page_elt.match(/\$\{.*\}/).nil? page_elt = Scoutui::Base::UserVars.instance.normalize(page_elt) end end if cmd=='visible' && page_elt.is_a?(String) && page_elt.match(/^\s*page\s*\(/) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " Process PageElement #{page_elt}" pageObject = Scoutui::Utils::TestUtils.instance.getPageElement(page_elt) elsif !Scoutui::Commands::Utils.instance.isCSS(page_elt).nil? pageObject={ 'locator' => Scoutui::Commands::Utils.instance.isCSS(page_elt) } elsif cmd=='visible' && page_elt.is_a?(String) && page_elt.match(/^\s*\//) pageObject={ 'locator' => page_elt } else Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " [isVisible]:unknown command" end if cmd=='visible' && pageObject.is_a?(Hash) && pageObject.has_key?('locator') _processed=true ## # expected: # wait: page(abc).get(def) where this page_elt has "locator" locator = pageObject['locator'].to_s _obj = Scoutui::Base::QBrowser.getFirstObject(my_driver, locator, Scoutui::Commands::Utils.instance.getTimeout) if cmd=='visible' if !_obj.nil? rc=_obj.displayed? end if _immediate Scoutui::Logger::LogMgr.instance.debug " return immediate (#{rc})" return rc end Scoutui::Logger::LogMgr.instance.asserts.info "Verify #{page_elt} is visible - #{rc}" Testmgr::TestReport.instance.getReq(_req).get_child('visible').add(rc, "Verify #{page_elt} is visible") end ## TITLE elsif page_elt.is_a?(String) && page_elt.match(/\s*(title)\s*\(\s*(.*)\s*\)/) _processed=true current_title = my_driver.title.strip _t = page_elt.match(/\s*title\s*\(\s*(.*)\s*\)/)[1].to_s expected_title = Regexp.new(_t) rc=!current_title.match(expected_title).nil? Scoutui::Logger::LogMgr.instance.asserts.info "Verify current title, #{current_title}, matches #{expected_title})" Testmgr::TestReport.instance.getReq(_req).get_child('title').add(rc, "Verify current title, #{current_title}, matches #{expected_title}") elsif pageObject.is_a?(Hash) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " == Verify Page page_elt:#{page_elt} pageObject:#{pageObject} req:#{_req} ==" assertPage(my_driver, page_elt, _req) end rc end # { "visible_when" => "always" } def visible_when_always(_k, _v, _obj=nil, _req='UI') if _v.is_a?(Hash) && _v.has_key?('reqid') _req = _v['reqid'] end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " visible_when_always(#{_k}, #{_v}, #{_obj}, #{_req})" _locator=nil rc=false if _v.is_a?(Hash) && _v.has_key?('visible_when') && _v['visible_when'].is_a?(Array) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " visible_when_always (array) - TBD" return rc end if _v.is_a?(Hash) && _v.has_key?('locator') _locator = _v['locator'].to_s end if _v.is_a?(Hash) && _v.has_key?('reqid') _req = _v['reqid'] end if !_locator.nil? && _v.has_key?('visible_when') && _v['visible_when'].match(/always/i) Scoutui::Logger::LogMgr.instance.asserts.info "Verify #{_k} - #{_locator} always visible - #{(!_obj.nil?).to_s}" Testmgr::TestReport.instance.getReq(_req).get_child('visible_when').add(!_obj.nil?, "Verify #{_k} - #{_locator} always visible") rc=true end rc end def visible_when_title(_k, page_elt, _obj, my_driver, _req='UI') _processed=false if page_elt.is_a?(Hash) && page_elt.has_key?('reqid') _req = page_elt['reqid'] end Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " visible_when_title(#{_k}, #{page_elt}, #{_obj}, #{_req})" _req='UI' if _req.nil? || _req.empty? if page_elt.is_a?(Hash) && page_elt.has_key?('visible_when') && !page_elt['visible_when'].is_a?(Array) && page_elt['visible_when'].match(/title\(/i) _processed=true current_title = my_driver.title.strip _t = page_elt['visible_when'].match(/title\((.*)\)/)[1].to_s expected_title = Regexp.new(_t) rc=!current_title.match(expected_title).nil? Scoutui::Logger::LogMgr.instance.asserts.info "Verify #{_k} - object exists when expected title, #{expected_title}, matches actual title(#{current_title})" Testmgr::TestReport.instance.getReq(_req).get_child('visible_when').add(!_obj.nil? == rc, "Verify #{_k} - object exists when expected title, #{expected_title}, matches actual title(#{current_title}) - #{rc}") end _processed end def visible_when_never(_k, _v, _obj=nil, _req='UI') if _v.is_a?(Hash) && _v.has_key?('reqid') _req = _v['reqid'] end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " visible_when_never(#{_k}, #{_v}, #{_obj}, #{_req})" _locator=nil rc=false if _v.has_key?('locator') _locator=_v['locator'] end if !_locator.nil? && _v.has_key?('visible_when') if _v['visible_when'].is_a?(Array) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " visible_when_never (array) - TBD" elsif _v['visible_when'].match(/never/i) Scoutui::Logger::LogMgr.instance.asserts.info "Verify #{_k} #{_locator} never visible - #{_obj.nil?.to_s}" Testmgr::TestReport.instance.getReq(_req).get_child('visible_when').add(_obj.nil?, "Verify #{_k} #{_locator} never visible") rc=true end end rc end # { "visible_when" => true } def visible_when_skip(_k, _v) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " visible_when_skip : #{_v.to_s}" if Scoutui::Utils::TestUtils.instance.isDebug? rc=false if _v.is_a?(Hash) && _v.has_key?('visible_when') if _v['visible_when'].is_a?(Array) Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " visible_when_skip (array) - TBD" elsif _v['visible_when'].match(/skip/i) rc=true Scoutui::Logger::LogMgr.instance.asserts.info "Skip verify #{_k.to_s} - skipped" Testmgr::TestReport.instance.getReq('UI').get_child('visible_when').add(nil, "Skip verify #{_k.to_s}") end end rc end def visible_when_value(_k, page_elt, my_driver, _req='UI') if page_elt.is_a?(Hash) && page_elt.has_key?('reqid') _req = page_elt['reqid'] end Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " visible_when_value(#{_k}, #{page_elt}, #{_req})" _processed=false if page_elt.is_a?(Hash) && page_elt.has_key?('visible_when') && !page_elt['visible_when'].is_a?(Array) && page_elt['visible_when'].match(/^\s*(text|value)\s*\(/) begin puts __FILE__ + (__LINE__).to_s + " ==> #{page_elt['visible_when'].match(/^\s*(text|value)\s*\(/)[1]}" _processed=true rc=nil condition = page_elt['visible_when'].match(/(value|text)\((.*)\)/)[1].to_s tmpObj = page_elt['visible_when'].match(/(value|text)\((.*)\)/)[2].to_s expectedVal = page_elt['visible_when'].match(/(value|text)\s*\(.*\)\s*\=\s*(.*)/)[2].to_s Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{condition} => #{tmpObj} with expected value #{expectedVal}" xpath = Scoutui::Base::UserVars.instance.get(tmpObj) obj = Scoutui::Base::QBrowser.getObject(my_driver, xpath, Scoutui::Commands::Utils.instance.getTimeout) if !obj.nil? # Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " value : #{obj.value.to_s}" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " text : #{obj.text.to_s}" if obj.tag_name.downcase.match(/(select)/) _opt = Selenium::WebDriver::Support::Select.new(obj) opts = _opt.selected_options Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " selected => #{opts.to_s}" opts.each do |o| Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "| ::<#{o.attribute('value').to_s}, #{o.text.to_s}>" desc=nil if condition=='text' && o.text==expectedVal desc=" Verify #{_k}, #{locator}, is visible since condition (#{condition} #{xpath} is met." elsif condition=='value' && o.attribute('value').to_s==expectedVal desc=" Verify #{_k}, #{locator}, is visible since #{condition} of #{xpath} is #{expectedVal}" end if !desc.nil? locatorObj = Scoutui::Base::QBrowser.getObject(my_driver, locator, Scoutui::Commands::Utils.instance.getTimeout) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " LocatorObj : #{locatorObj} : #{!locatorObj.nil? && locatorObj.displayed?}" Testmgr::TestReport.instance.getReq('UI').tc('visible_when').add(!locatorObj.nil? && locatorObj.displayed?, desc) end end else desc=nil locator = page_elt['locator'] _rc=false if condition=='text' _rc=obj.text==expectedVal desc=" Verify #{_k}, #{locator}, is visible since condition '#{condition}' of #{xpath} is #{expectedVal}." elsif condition=='value' _rc=o.attribute('value').to_s==expectedVal desc=" Verify #{_k}, #{locator}, is visible since condition '#{condition}' of #{xpath} is #{expectedVal}" end if !desc.nil? locatorObj = Scoutui::Base::QBrowser.getObject(my_driver, locator, Scoutui::Commands::Utils.instance.getTimeout) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "#{desc} : #{locatorObj} : #{!locatorObj.nil? && locatorObj.displayed? && _rc}" Testmgr::TestReport.instance.getReq(_req).get_child('visible_when').add(!locatorObj.nil? && locatorObj.displayed? && _rc, desc) end end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " value : #{obj.attribute('value').to_s}" end rescue => ex Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " Error during processing: #{ex}" puts __FILE__ + (__LINE__).to_s + "Backtrace:\n\t#{ex.backtrace.join("\n\t")}" end # elsif page_elt.has_key?('visible_when') && page_elt['visible_when'].match(/\s*(text|value)\s*\(/) # Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " Unknown => #{page_elt}" end _processed end def assertPageElement(_k, _v, _obj, my_driver, _req) _processed = true if Scoutui::Base::Assertions.instance.visible_when_always(_k, _v, _obj, _req) ; elsif Scoutui::Base::Assertions.instance.visible_when_never(_k, _v, _obj, _req) ; elsif Scoutui::Base::Assertions.instance.visible_when_title(_k, _v, _obj, my_driver, _req) ; elsif Scoutui::Base::Assertions.instance.visible_when_value(_k, _v, my_driver, _req) ; elsif Scoutui::Base::Assertions.instance.visible_when_visible(_k, _v, _obj, my_driver, _req) ; else _processed = false end _processed end # _a : Hash # o locator => String # o visible_when => Hash def visible_when_visible(_k, page_elt, _obj, my_driver, _req='UI') Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " visible_when_visible(#{_k}, #{page_elt}, #{_obj}, mydriver, #{_req}" _processed = false if page_elt.has_key?('visible_when') && !page_elt['visible_when'].is_a?(Array) && page_elt['visible_when'].match(/\s*visible\s*\(/) _processed = true condition = page_elt['visible_when'].match(/(visible)\((.*)\)/)[1].to_s tmpObj = page_elt['visible_when'].match(/(visible)\((.*)\)/)[2].to_s expectedVal = page_elt['visible_when'].match(/(visible)\s*\(.*\)\s*\=\s*(.*)/)[2].to_s _locator = Scoutui::Base::UserVars.instance.get(tmpObj) depObj = Scoutui::Base::QBrowser.getObject(my_driver, _locator, Scoutui::Commands::Utils.instance.getTimeout) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "condition (#{condition}), tmpObj (#{tmpObj}) (#{depObj}), expectedVal (#{expectedVal}) : _obj : #{_obj.displayed?}" expectedRc = !expectedVal.match(/^\s*true\s*$/i).nil? _desc=" Verify #{_k} is " if expectedRc == !depObj.nil? _desc+="visible when visible(#{tmpObj}) is #{expectedRc}" Scoutui::Logger::LogMgr.instance.asserts.info __FILE__ + (__LINE__).to_s + _desc + " - #{!_obj.nil?}" Testmgr::TestReport.instance.getReq(_req).get_child('visible_when_visible').add(!_obj.nil?, __FILE__ + (__LINE__).to_s + _desc) else _desc+="not visible when visible(#{tmpObj}) is #{!depObj.nil?} (expected:#{expectedRc})" Scoutui::Logger::LogMgr.instance.asserts.info __FILE__ + (__LINE__).to_s + _desc + " - #{_obj.nil?}" Testmgr::TestReport.instance.getReq(_req).get_child('visible_when_visible').add(_obj.nil?, __FILE__ + (__LINE__).to_s + _desc) end end _processed end end end