require 'date' module Scoutui::Commands class Type < Command KEYBOARD_LIST={ '__DOWN__' => lambda { |obj| obj.send_keys(:arrow_down) }, '__TAB__' => lambda { |obj| obj.send_keys(:tab) }, '__CLEAR__' => lambda { |obj| obj.clear } } def _whenTyped(page_elt) if page_elt.is_a?(Hash) && page_elt.has_key?('when_typed') 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 puts __FILE__ + (__LINE__).to_s + " => #{_pg}" end # Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " IsVisible #{isVisible} - PAUSE"; gets end end end def execute(drv=nil, pageElt=nil) @drv=drv if !drv.nil? _isKb=false _rc=false _req = Scoutui::Utils::TestUtils.instance.getReq() begin if @cmd.strip.match(/^\s*type\s*\(\s*__TAB__\s*\)\s*$/) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " HIT TAB key" @drv.action.send_keys(:tab).perform _rc=true _isKb=true elsif @cmd.strip.match(/^\s*type\s*\(\s*__ENTER__\s*\)\s*$/) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " ENTER" @drv.action.send_keys(:enter).perform _rc=true _isKb=true else # _xpath = @cmd.match(/^\s*type[\!]*\(([^,]*),\s*/)[1].to_s # _val = @cmd.match(/^\s*type[\!]*\([^,]*,\s*(.*)\)/)[1].to_s if @cmd.match(/^\s*type[\!]*\((.*),\s*'(.*)\s*'\)\s*$/) _xpath = @cmd.match(/^\s*type[\!]*\((.*),\s*'(.*)\s*'\)\s*$/)[1] _val = @cmd.match(/^\s*type[\!]*\((.*),\s*'(.*)\s*'\)\s*$/)[2] else _xpath = @cmd.match(/^\s*type[\!]*\((.*),\s*(.*)\s*\)\s*$/)[1] _val = @cmd.match(/^\s*type[\!]*\((.*),\s*(.*)\s*\)\s*$/)[2] end Scoutui::Logger::LogMgr.instance.commands.debug __FILE__ + (__LINE__).to_s + "Process TYPE #{_val} into #{_xpath}" obj = Scoutui::Base::QBrowser.findElement(@drv, _xpath, Scoutui::Commands::Utils.instance.getFrameSearch(), Scoutui::Commands::Utils.instance.getTimeout) wait = Selenium::WebDriver::Wait.new(:timeout => Scoutui::Commands::Utils.instance.getTimeout) isDisplayed = wait.until { if Scoutui::Base::QBrowser.isStale?(obj) obj = Scoutui::Base::QBrowser.findElement(@drv, _xpath, Scoutui::Commands::Utils.instance.getFrameSearch(), Scoutui::Commands::Utils.instance.getTimeout) end true if obj.displayed? } Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " type(#{_val})" Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " tag : #{obj.tag_name}" if obj.is_a?(Selenium::WebDriver::Element) if !obj.nil? && (obj.tag_name.downcase=='body' || !obj.attribute('type').downcase.match(/(file|input|text|textarea|password|email)/).nil? ) isDisplayed=false # FileUpload dialog is not 'displayed' if !Scoutui::Base::QBrowser.isFileType?(obj) begin wait = Selenium::WebDriver::Wait.new(:timeout => Scoutui::Commands::Utils.instance.getTimeout) isDisplayed = wait.until { if Scoutui::Base::QBrowser.isStale?(obj) obj = Scoutui::Base::QBrowser.findElement(@drv, _xpath, Scoutui::Commands::Utils.instance.getFrameSearch(), Scoutui::Commands::Utils.instance.getTimeout) end true if obj.displayed? } rescue Selenium::WebDriver::Error::TimeOutError => ex Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Timeout waiting for display?: #{_xpath}" end end if @cmd.match(/^\s*type\!/i) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " clear()" obj.clear if isDisplayed end Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " send_keys(#{_val})" if isDisplayed || Scoutui::Base::QBrowser.isFileType?(obj) if KEYBOARD_LIST.has_key?(_val.strip) KEYBOARD_LIST[_val.strip].call(obj) elsif _val.match(/^\s*__ENTER__\s*$/) obj.send_keys("") @drv.action.send_keys(:enter).perform else if Scoutui::Base::UserVars.instance.isTestData?(_val) _v2=Scoutui::Base::UserVars.instance.normalize(_val) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " getData ==> #{_v2}" else _uservars = _val.scan(/\$\{(.*?)\}/) _v2 = _val for i in 0 .. (_uservars.size - 1) _t = Scoutui::Base::UserVars.instance.get(_uservars[i][0]) puts __FILE__ + (__LINE__).to_s + " translate(#{_uservars[i][0]}) => #{_t}" if !_t.nil? && _uservars[i][0]!=_t _v2.gsub!("\$\{#{_uservars[i][0]}\}", _t) end puts __FILE__ + (__LINE__).to_s + " value => #{_v2}" end end ## Global replaces _text_to_type = Scoutui::Base::UserVars.instance.get(_v2) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " rawText : #{_text_to_type}" _text_to_type = Scoutui::Commands::Utils.instance.expandMacro(_text_to_type) Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Text to type : #{_text_to_type}" if Scoutui::Base::QBrowser.isStale?(obj) obj = Scoutui::Base::QBrowser.findElement(@drv, _xpath, Scoutui::Commands::Utils.instance.getFrameSearch(), Scoutui::Commands::Utils.instance.getTimeout) end obj.send_keys(_text_to_type) if !pageElt.nil? && pageElt['page'].has_key?('then') pageElt['page']['then'].each do |_subcmd| if _subcmd.is_a?(String) puts __FILE__ + (__LINE__).to_s + " | then => #{_subcmd}" if _subcmd.match(/^\s*press\(__TAB__\)$/) @drv.action.send_keys(:tab).perform elsif _subcmd.match(/^\s*press\(__DOWN__\)$/) @drv.action.send_keys(:arrow_down).perform elsif _subcmd.match(/^\s*press\(__UP__\)$/) @drv.action.send_keys(:arrow_up).perform elsif _subcmd.match(/^\s*press\(__ENTER__\)\s*$/) @drv.action.send_keys(:enter).perform end end end end if !obj.nil? && false arr=_text_to_type.split(/__ENTER__/) i=0 arr.each do |_t| # puts __FILE__ + (__LINE__).to_s + " => #{_t}"; STDIN.gets; if i > 0 obj.send_keys("") @drv.action.send_keys(:enter).perform puts __FILE__ + (__LINE__).to_s + " ENTER"; STDIN.gets() obj.send_keys("") end obj.send_keys(_t) i+1 end end end _rc=true end else Scoutui::Logger::LogMgr.instance.commands.warn __FILE__ + (__LINE__).to_s + " Unable to process command TYPE => #{obj.to_s}" end end rescue => ex Scoutui::Logger::LogMgr.instance.debug "Error during processing: #{ex.class}" puts __FILE__ + (__LINE__).to_s + "Backtrace:\n\t#{ex.backtrace.join("\n\t")}" end if !_isKb Testmgr::TestReport.instance.getReq(_req).testcase('type').add(!obj.nil?, "Verify object #{_xpath} to type #{_val} exists : #{obj.class.to_s}") Testmgr::TestReport.instance.getReq(_req).testcase('type').add(_rc, "Verify typed data #{_rc}") end setResult(_rc) end end end