Class: Scoutui::Base::QBrowser

Inherits:
Object
  • Object
show all
Defined in:
lib/scoutui/base/q_browser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQBrowser

Returns a new instance of QBrowser.



9
10
# File 'lib/scoutui/base/q_browser.rb', line 9

def initialize()
end

Instance Attribute Details

#driverObject

Returns the value of attribute driver.



7
8
9
# File 'lib/scoutui/base/q_browser.rb', line 7

def driver
  @driver
end

Class Method Details

.click(element) ⇒ Object



915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
# File 'lib/scoutui/base/q_browser.rb', line 915

def self.click(element)
  rc=false

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " click(#{element})"

  if element.is_a?(Selenium::WebDriver::Element)

    begin
      element.click
      rc=true
    rescue => ex
      Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Error during processing: #{$!}"
      Scoutui::Logger::LogMgr.instance.debug "Backtrace:\n\t#{ex.backtrace.join("\n\t")}"
    end

  end

  rc
end

.existsAlert?(drv) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/scoutui/base/q_browser.rb', line 94

def self.existsAlert?(drv)
  rc=false

  begin
    alert=drv.switch_to.alert

    if alert.is_a?(Selenium::WebDriver::Alert)
      rc=true
    end

  rescue Selenium::WebDriver::Error::NoSuchAlertError
    ;

  rescue Selenium::WebDriver::Error::WebDriverError
    # Support for phantomjs/ghostdriver per switch_to.alert()
    ;
  end

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " existsAlert?() => #{rc}"
  rc
end

.findElement(drv, _locator, _frames, _timeout = nil) ⇒ Object



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'lib/scoutui/base/q_browser.rb', line 659

def self.findElement(drv, _locator, _frames, _timeout=nil)

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " findElement(#{_locator}, #{Scoutui::Commands::Utils.instance.getFrameSearch})"

  if Scoutui::Commands::Utils.instance.isSwitchFrame?()

    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " FrameSwitch"
    e={ 'page' => { 'frames' => Scoutui::Commands::Utils.instance.getFrameSearch() } }
    switch_frame(drv, e)
    obj = Scoutui::Base::QBrowser.getObject(drv, _locator, _timeout)

  elsif Scoutui::Commands::Utils.instance.isFrameSearch?
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " isFrameSearch"
    hits = Scoutui::Base::QBrowser.frame_getObject(drv, _locator, _timeout)

    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " hits => #{hits.class} : #{hits}"
    if hits.is_a?(Array) && !hits.empty?
      obj=hits[0]
    elsif hits.is_a?(Selenium::WebDriver::Element)
      obj=hits
    end

  else

    puts __FILE__ + (__LINE__).to_s + " Analyze locator : #{_locator}"

    _parsed = Scoutui::Base::QBrowser.parseLocator(_locator)

    puts __FILE__ + (__LINE__).to_s + " _parsed(#{_locator}) => #{_parsed}"

    if _parsed.has_key?('frame') && _parsed.has_key?('locator')
      obj = Scoutui::Base::QBrowser.findElementwithinFrame(drv, _parsed['locator'], _parsed['frame'], _timeout)
    else
      obj = Scoutui::Base::QBrowser.getObject(drv, _locator, _timeout)
    end

  end


  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " findElement(#{_frames}, #{_locator}) => #{obj}"
  obj

end

.findElements(opts) ⇒ Object

5150



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/scoutui/base/q_browser.rb', line 446

def self.findElements(opts)
  puts __FILE__ + (__LINE__).to_s + " findElements(#{opts})"
  driver=opts[:driver]
  locator=opts[:locator]
  locateBy=:xpath
  hits=nil


  if locator.match(/^\s*css\s*\=\s*/i)
    locateBy = :css
    locator = locator.match(/css\s*\=\s*(.*)/i)[1].to_s
  elsif locator.match(/^#/i)
    locateBy = :css
  end

  puts __FILE__ + (__LINE__).to_s + "  driver  : #{driver.class}"
  puts __FILE__ + (__LINE__).to_s + "  locator : #{locator}"

  begin
  _timeout=Scoutui::Commands::Utils.instance.getTimeout
  Selenium::WebDriver::Wait.new(timeout: _timeout).until { hits=driver.find_elements( locateBy => locator) }

  rescue => ex
    puts __FILE__ + (__LINE__).to_s + "  #{ex.class}"
    puts ex.backtrace

  end
  hits
end

.findElementwithinFrame(drv, _locator, _frames, _timeout = nil) ⇒ Object



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/scoutui/base/q_browser.rb', line 561

def self.findElementwithinFrame(drv, _locator, _frames, _timeout=nil)

  puts __FILE__ + (__LINE__).to_s + " frameElementWithinFrame(#{_locator}, #{_frames}"

  # 5150

  obj=nil
  if _frames.nil?
    obj=findElement(drv, _locator, _frames, _timeout)
  else
    # Switch to default window
    drv.switch_to.default_content
    if switch_frame(drv, _frames)

      (0..3).each do |_i|
        obj = Scoutui::Base::QBrowser.getObject(drv, _locator, _timeout)
        if !obj.nil?
          break
        end
      end

    end

  end

  puts __FILE__ + (__LINE__).to_s + " [return]:frameElementsWithinFrame(#{_locator}, #{_frames}) => #{obj.class}"
  obj
end

.frame_getObject(drv, _locator, _timeout = nil) ⇒ Object



703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/scoutui/base/q_browser.rb', line 703

def self.frame_getObject(drv, _locator, _timeout=nil)

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " [enter]:frame_getObject : #{_locator}"

  rc = Array.new()

  frames = drv.find_elements(:xpath, '//iframe')

  if frames.size == 0
    frames = drv.find_elements(:xpath, '//frame')
  end

  if frames.size > 0
    rc.concat frames
  end

  _obj = getObject(drv, _locator, _timeout)

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " frames => #{frames}  :  #{_obj}"

  if _obj.nil? || (_obj.is_a?(Array) && _obj.empty?)

    for i in 0 .. (frames.size - 1)

      _madeSwitch=false

      begin

        _tag = frames[i].attribute('name')

        if _tag.empty?
          _tag = frames[i].attribute('id')
        end

        if _tag.empty?
          _tag = i
        end


        Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Process frame[#{i}] : #{_tag}"

        # https://bugs.chromium.org/p/chromedriver/issues/detail?id=107
        if drv.browser.to_s.match(/chrome/)
          drv.switch_to.frame frames[i]
        else

          Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s +  " switch_to.frame #{_tag}"
          drv.switch_to.frame _tag
        end

        _madeSwitch=true

        # Recurse into sub-frames
        _hits = frame_getObject(drv, _locator, 3)
      rescue => ex
        Scoutui::Logger::LogMgr.instance.debug "Error during processing: #{$!}"
        Scoutui::Logger::LogMgr.instance.debug "Backtrace:\n\t#{ex.backtrace.join("\n\t")}"
      end

      Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " hits => #{_hits}"

      if _hits.is_a?(Array) && _hits.size > 0
        _obj=_hits[0]

        break
      elsif _hits.is_a?(Selenium::WebDriver::Element)
        _obj = _hits
        break
      end

      Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " switch_to.parent_frame()"
      drv.switch_to.parent_frame if _madeSwitch

    end

  end


  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " _obj => #{_obj}"

  _obj

end

.getElement(drv, _locator, _frames, _timeout = nil) ⇒ Object



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/scoutui/base/q_browser.rb', line 381

def self.getElement(drv, _locator, _frames, _timeout=nil)

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "== getElement(#{_locator}) =="
  _isntStale=true
  obj = Scoutui::Base::QBrowser.findElement(drv,
                                            _locator,
                                            _frames,
                                            _timeout)

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " | locator : #{_locator}"
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " | obj         : #{obj}"
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " | stale       : " + Scoutui::Base::QBrowser.isStale?(obj).to_s
  if obj.is_a?(Selenium::WebDriver::Element)
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " | displayed : " + obj.displayed?.to_s
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " | enabled   : " + obj.enabled?.to_s
  end


  cnt=0
  if obj.is_a?(Selenium::WebDriver::Element) && Scoutui::Base::QBrowser.isStale?(obj) && !_locator.nil?

      _isntStale=false

      (0..7).each do |i|

        cnt+=1
        begin
          wait = Selenium::WebDriver::Wait.new(:timeout =>  Scoutui::Commands::Utils.instance.getTimeout)
          _isntStale = wait.until {
              _rc=Scoutui::Base::QBrowser.isStale?(obj)
              Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "  stale(#{_locator}) : #{_rc}"
              if _rc
                obj = Scoutui::Base::QBrowser.findElement(drv, _locator,
                                                          _frames,
                                                          _timeout)
              end
            !_rc
          }
        rescue Selenium::WebDriver::Error::TimeOutError
          ;
        end


        Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{i} ==> stale(#{_locator}) : #{_isntStale}"

        if !_isntStale
          Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{i} => obj is STALE : #{_locator}"
            obj = Scoutui::Base::QBrowser.findElement(drv, _locator,
                                                      _frames,
                                                      _timeout)
        else
          break
        end

    end


  end

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " getElement(#{_locator}) : #{obj}, cnt:#{cnt}, isntStale:#{_isntStale}"
  obj
end

.getFirstObject(drv, _locator, _timeout = nil) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/scoutui/base/q_browser.rb', line 116

def self.getFirstObject(drv, _locator, _timeout=nil)


  if existsAlert?(drv)
    Scoutui::Logger::LogMgr.instance.info __FILE__ + (__LINE__).to_s + " ALERT currently exists!"; # STDIN.gets()
    return nil
  end

  if _timeout.nil?
    _timeout = Scoutui::Utils::TestUtils.instance.getDefaultWait
  end
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " getFirstObject(#{_locator}, #{_timeout})"

  if _timeout > 30
    Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + "  A #{_timeout} second timeout is set high, may slow down tests"
  end

  rc=nil
  locator=_locator

  begin
    locateBy=:xpath

    if _locator.match(/^css\=/i)
      locateBy = :css
      locator = _locator.match(/css\=(.*)/i)[1].to_s
    elsif _locator.match(/^#/i)
      locateBy = :css
    end

    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " locator => #{locator.to_s}"  if Scoutui::Utils::TestUtils.instance.isDebug?
    Selenium::WebDriver::Wait.new(timeout: _timeout).until { drv.find_elements(locateBy => locator).size > 0 }
    rc=drv.find_elements(locateBy => locator)[0]
  rescue => ex
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{$!} - #{ex.message} getFirstObject.Exception: #{locator.to_s} - #{ex}"
    ;
  end

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{_locator} => #{rc}"
  rc
end

.getObject(drv, obj, _timeout = nil) ⇒ Object



788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/scoutui/base/q_browser.rb', line 788

def self.getObject(drv, obj, _timeout=nil)


  if obj.nil?
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " getObject() - null obj passed."
    return nil
  end

  if _timeout.nil?
    _timeout = Scoutui::Utils::TestUtils.instance.getDefaultWait
  end

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " getObject(#{obj})  class:#{obj.class.to_s}   hash : #{obj.is_a?(Hash)}" if Scoutui::Utils::TestUtils.instance.isDebug?
  rc=nil
  visible_when=nil
  locator=obj
  locateBy=:xpath
  locator=nil
  _frame=nil


  begin

    if obj.is_a?(Hash)


      if obj.has_key?('visible_when')
        visible_when=!obj['visible_when'].match(/always/i).nil?
      end

      locator = obj['locator'].to_s


    elsif !obj.match(/^page\([\w\d]+\)/).nil?

      elt = Scoutui::Utils::TestUtils.instance.getPageElement(obj)

      if elt.is_a?(Hash)
        Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " JSON or Hash => #{elt}"  if Scoutui::Utils::TestUtils.instance.isDebug?
        locator = elt['locator'].to_s
        _frame = elt['frame'] if elt.has_key?('frame')
      else
        locator=elt.to_s
      end

      Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Process page request #{obj} => #{locator}" if Scoutui::Utils::TestUtils.instance.isDebug?

    else
      locator=obj.to_s
    end

    if !locator.match(/\$\{.*\}/).nil?
      _x = Scoutui::Base::UserVars.instance.get(locator)
      Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " User specified user var : #{locator} ==> #{_x}"
      if !_x.nil?


        if !_x.match(/^page\([\w\d]+\)/).nil?
          elt = Scoutui::Utils::TestUtils.instance.getPageElement(_x)

          if elt.is_a?(Hash)
            Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " JSON or Hash => #{elt}" if Scoutui::Utils::TestUtils.instance.isDebug?
            locator = elt['locator'].to_s
          else
            locator=elt.to_s
          end
        else # Update locator to the defined user var.
          locator = _x
        end

      end

    end

    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " locator : #{locator}" if Scoutui::Utils::TestUtils.instance.isDebug?

    if locator.match(/^\s*css\s*\=\s*/i)
      locateBy = :css
      locator = locator.match(/css\s*\=\s*(.*)/i)[1].to_s
    elsif locator.match(/^#/i)
      locateBy = :css
    #  locator = locator.match(/\#(.*)/)[1].to_s
    end

    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " By      => #{locateBy.to_s}"
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Frame   => #{_frame.to_s}"
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Locator => #{locator}"
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Visible_When => #{visible_when}"
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Timeout => #{_timeout}"

    current_window=drv.window_handle

    if !_frame.nil?
      switch_frame(drv, _frame)
    end
    Selenium::WebDriver::Wait.new(timeout: _timeout).until { rc=drv.find_element( locateBy => locator) }


    ## The following line would render the found element as Stale (TBD).
    ## The caller is responsible to switching (back) to the target window.
    if false && !_frame.nil?
      drv.switch_to.window(current_window)
    end


  rescue Selenium::WebDriver::Error::TimeOutError
      Scoutui::Logger::LogMgr.instance.debug __FILE__  + (__LINE__).to_s + " TimeoutError: #{locator} time out."
      rc=nil

  rescue Selenium::WebDriver::Error::NoSuchElementError
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " NoSuchElementError: #{locator} not found."
    rc=nil

  rescue => ex
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " Error during processing: #{$!}"
    Scoutui::Logger::LogMgr.instance.debug "Backtrace:\n\t#{ex.backtrace.join("\n\t")}"
  end

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " getObject(#{locator}) => #{rc.to_s}" if Scoutui::Utils::TestUtils.instance.isDebug?

  rc
end

.hasParent?(element, parent_xpath) ⇒ Boolean

Returns:

  • (Boolean)


476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/scoutui/base/q_browser.rb', line 476

def self.hasParent?(element, parent_xpath)

  parent=nil
  if element.is_a?(Selenium::WebDriver::Element)
    begin
      parent = element.find_element(:xpath, parent_xpath)
    rescue Selenium::WebDriver::Error::NoSuchElementError
      ;
    end

  end

  parent
end

.highlight(drv, locator, style = {"color" => 'rgb(255, 16, 16)', "border" => 2}, fAncestors = true) ⇒ Object



935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
# File 'lib/scoutui/base/q_browser.rb', line 935

def self.highlight(drv, locator, style={"color" => 'rgb(255, 16, 16)', "border" => 2}, fAncestors=true)
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " highLight(#{locator} with style #{style})"

  rc=false

#     element = find_element(method, locator)

  if locator.is_a?(Selenium::WebDriver::Element)
    element = locator
  else
    element = getObject(drv, locator)
  end

  if element.nil?
    return rc
  end


  if style.has_key?("color")

    color=style.has_key?("color")? style["color"] : 'rgb(255, 16, 16)'

    _c = style["color"]

    # TODO: refactor with command 'highlight.rb'

    if _c.match(/\s*blue/i)
      color='rgb(0, 0, 255)'
    elsif _c.match(/\s*red/i)
      color='rgb(255, 0, 0)'
    elsif _c.match(/\s*yellow/i)
      color='rgb(255, 255, 0)'
    elsif _c.match(/\s*green/i)
      color='rgb(0, 255, 0)'
    elsif _c.match(/\s*gray/i)
      color='rgb(128, 128, 128)'
    end

  end


  border=style.has_key?("border")? style["border"] : 1

  _js = "hlt = function(c) { c.style.border='solid #{border}px rgb(#{color}, 16, 16)'; }; return hlt(arguments[0]);"

#     drv.execute_script("hlt = function(c) { c.style.border='solid #{border}px rgb(#{color}, 16, 16)'; }; return hlt(arguments[0]);", element)
  drv.execute_script("hlt = function(c) { c.style.border='solid #{border}px #{color}'; }; return hlt(arguments[0]);", element)
  parents = ""

  if fAncestors
    ancestors=style.has_key?("ancestors")? style["ancestors"] : 0

    ancestors.times do
      parents << ".parentNode"
      color -= (12*8 / ancestors)
      drv.execute_script("hlt = function(c) { c#{parents}.style.border='solid 1px rgb(#{color}, 0, 0)'; }; return hlt(arguments[0]);", element)
    end
  else
    puts __FILE__ + (__LINE__).to_s + " SKIP ANCES"
  end

#      Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " PAUSE on highlight"; gets
  element
end

.isAccessible(opts) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/scoutui/base/q_browser.rb', line 493

def self.isAccessible(opts)

  puts __FILE__ + (__LINE__).to_s + " == isAccessible(#{opts}) =="
  hits = Scoutui::Base::QBrowser.findElements(opts)

  puts __FILE__ + (__LINE__).to_s + " HITS => #{hits.length}"


  i=0
  begin
    hits.each do | obj |
        if obj.displayed?
          puts __FILE__ + (__LINE__).to_s + " #{i}. #{obj.tag_name}"

          tagName=obj.tag_name.to_s.strip

          ## Verify labels are present for user input elements
          if tagName.match(/(textarea|input)/) && obj.attribute('id')
            id=obj['id'].to_s

           # parent_li = obj.find_element(:xpath, './../label')
            parent_li = Scoutui::Base::QBrowser.hasParent?(obj, "./../label[@for='#{id}']")

            Testmgr::TestReport.instance.getReq('WCAG').testcase(tagName).add(!parent_li.nil?, "Verify matching label (id=#{id.to_s}) exists for #{tagName}.")

            if parent_li
              forId = parent_li['for']
              puts __FILE__ + (__LINE__).to_s + "    parent: #{parent_li.tag_name}"
              puts __FILE__ + (__LINE__).to_s + "    assert <id, for>: #{id}, #{forId}"

              Testmgr::TestReport.instance.getReq('WCAG').testcase(tagName).add(id==forId, "Verify matching label for #{tagName} has same id (#{id}) and for (#{forId}) values")
            else
              puts __FILE__ + (__LINE__).to_s + "  => Missing associated label for #{tagName}"
            end


          #---------------------------------------------------#

          elsif tagName.match(/(img)/)

            src=obj.attribute('src')
            alt=obj.attribute('alt')
            longdesc=obj.attribute('longdesc')

            puts __FILE__ + (__LINE__).to_s + " ** #{i}. process #{tagName}  <alt, longdesc>::<#{alt.nil?}, #{longdesc.nil?}, #{src}> **"

            Testmgr::TestReport.instance.getReq('WCAG').testcase(tagName).add(!(alt.nil? && longdesc.nil?), "Verify alt/longdesc (#{alt.to_s} exists for img[#{i}] (#{src})")


          else
            puts __FILE__ + (__LINE__).to_s + " == #{tagName} out of scope for Acccessibility"

          end



          i+=1
        end
    end

  rescue => ex
    puts __FILE__ + (__LINE__).to_s + " #{ex.class}"
    puts ex.backtrace
  end


end

.isChrome?(drv) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/scoutui/base/q_browser.rb', line 12

def self.isChrome?(drv)
  drv.browser.to_s.match(/chrome/i)
end

.isDisplayed?(_obj, _timeout = Scoutui::Commands::Utils.instance.getTimeout) ⇒ Boolean

Check if the object (not a String) is an Element that is displayed.

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/scoutui/base/q_browser.rb', line 53

def self.isDisplayed?(_obj, _timeout=Scoutui::Commands::Utils.instance.getTimeout)
  rc=false

  if !_obj.nil? && _obj.is_a?(Selenium::WebDriver::Element)
    begin
      wait = Selenium::WebDriver::Wait.new(:timeout =>  _timeout)
      rc = wait.until { _obj.displayed? }
    rescue Selenium::WebDriver::Error::TimeOutError
      ;
    end

  end

  rc
end

.isEnabled?(_obj, my_driver = nil, _locator = nil) ⇒ Boolean

Returns:

  • (Boolean)


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/scoutui/base/q_browser.rb', line 321

def self.isEnabled?(_obj, my_driver=nil, _locator=nil)
  rc=false

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " isEnabled?(#{_obj})"

  begin
    if _obj.is_a?(Selenium::WebDriver::Element)

      (0..2).each do |i|

        _isStale = Scoutui::Base::QBrowser.isStale?(_obj)

        Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{i} => isStale?(#{_obj}, #{_locator}),  isStale: #{_isStale}"



        if _isStale  && !_locator.nil?

          wait = Selenium::WebDriver::Wait.new(:timeout =>  Scoutui::Commands::Utils.instance.getTimeout)
          isDisplayed = wait.until {
            if Scoutui::Base::QBrowser.isStale?(_obj)
              Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " ** refetch #{_locator} **"
              _obj=Scoutui::Base::QBrowser.findElement(my_driver, Scoutui::Base::UserVars.instance.normalize(_locator), Scoutui::Commands::Utils.instance.getFrameSearch(),  Scoutui::Commands::Utils.instance.getTimeout)
            end
            true if _obj.enabled?
          }

        end

        Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " enabled => #{_obj.enabled?}"

        if _obj.enabled?
          _disabled=_obj.attribute("disabled")
          _rdonly = _obj.attribute("readonly")

          Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " disabled: #{_disabled}  nil: #{_disabled.nil?}"
          Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " readonly: #{_rdonly}    nil: #{_rdonly.nil?}"
          Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " enabled:  #{_obj.enabled?}"

          rc = _obj.enabled? && !(_obj.attribute("disabled") || _obj.attribute("readonly") )

          rc = _obj.enabled? && (_disabled.nil? || !_disabled)
          rc=false if rc.nil?
          next
        end
      end

    else
      Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " isEnabled?(not Element)"
    end

  rescue => ex
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " #{ex}"
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + ex.backtrace
  end

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " isEnabled?(#{_obj}) : #{rc}"
  rc
end

.isFileType?(obj) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/scoutui/base/q_browser.rb', line 81

def self.isFileType?(obj)
  rc=false
  begin
    if obj.is_a?(Selenium::WebDriver::Element)
      rc=!obj.attribute('type').match(/file/i).nil?
    end
  rescue => ex
    ;
  end

  rc
end

.isStale?(_obj) ⇒ Boolean

Returns:

  • (Boolean)


288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/scoutui/base/q_browser.rb', line 288

def self.isStale?(_obj)
  _noSuch=false
  _isStale=false

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " isStale?(#{_obj})"

  begin

    if _obj.is_a?(Selenium::WebDriver::Element)
      _obj.enabled?
      _obj.tag_name
      _obj.attribute('type')
    else
      Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " | isStale? => nil"
      return nil
    end

  rescue Selenium::WebDriver::Error::NoSuchElementError
    _noSuch=true

  rescue Selenium::WebDriver::Error::StaleElementReferenceError
    _isStale=true

  rescue => ex
    Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " #{ex}"
    puts ex.backtrace
  end

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " | isStale?(#{_obj}) : nosuch=#{_noSuch},  stale=#{_isStale}"
  return (_noSuch || _isStale)
end

.parseLocator(_locator) ⇒ Object



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/scoutui/base/q_browser.rb', line 591

def self.parseLocator(_locator)
  # TODO: If its a pageObject reference (page(one).get().get()), then retrieve it.
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " parseLocator(#{_locator}"

  if _locator.is_a?(Hash)

    if _locator.has_key?('frame') && _locator.has_key?('locator')

      _locator="#{_locator['frame']}.locator(#{_locator['locator']})"
    elsif _locator.has_key?('locator')
      _locator = _locator['locator']
    end

  end

  return _locator if !_locator.is_a?(String)

  locator=_locator

  if Scoutui::ApplicationModel::QModel.isPageObject?(_locator) #  _locator.match(/^page\([\w\d]+\)/)
    pg = Scoutui::Utils::TestUtils.instance.getPageElement(_locator)
    puts __FILE__ + (__LINE__).to_s + " [parseLocator]: pageObject(#{_locator}) => #{pg}"

    if pg.is_a?(Hash)

      if pg.has_key?('frame') && pg.has_key?('locator')
        locator="#{pg['frame']}.locator(#{pg['locator']})"
      elsif pg.has_key?('locator')

        if pg['locator'].match((/^\s*([(frame\([^\(]*?\)\.)]*)\.(locator\((.*)\))\s*$/))
          locator=pg['locator']
        else
          locator="locator(#{pg['locator']})"
        end

      end

    end
  end

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " | locator => #{locator}"


  rc=locator.match(/^\s*([(frame\([^\(]*?\)\.)]*)\.(locator\((.*)\))\s*$/)
  Scoutui::Logger::LogMgr.instance.debug  __FILE__ + (__LINE__).to_s +  " parseIt:  #{locator} => #{rc}";


  if rc
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "o rc[1] : #{rc[1].to_s}"
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "o rc[2] : #{rc[2]}"
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "o rc[3] : #{rc[3]}"
    return { 'frame' => rc[1], 'locator' => rc[3] }
  end


  rc=locator.match(/^\s*(locator\((.*)\))\s*$/)
  Scoutui::Logger::LogMgr.instance.debug  __FILE__ + (__LINE__).to_s +  " parseIt2:  #{locator} => #{rc}"

  if rc
    puts "o rc[1] : #{rc[1]}"
    puts "o rc[2] : #{rc[2]}"
    return { 'locator' => rc[2] }
  end

  return { 'locator' => locator }
end

.saveScreenShot(drv, fullPath) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/scoutui/base/q_browser.rb', line 16

def self.saveScreenShot(drv, fullPath)
  rc=false
  begin
    drv.save_screenshot(fullPath)
    rc=true
  rescue => ex
    ;
  end
  rc
end

.switch_frame(drv, e) ⇒ Object

e : Hash : Command stanza from YML s : String

format: frame(<string>)*



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/scoutui/base/q_browser.rb', line 221

def self.switch_frame(drv, e)

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "\n\n== self.switch_frame(#{e}) =="
  frames=nil
  if e.is_a?(Hash) && e.has_key?('page') && e['page'].has_key?('frames')
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " frames => #{e['page']['frames']}";

    frames=e['page']['frames']
  elsif e.is_a?(String)
    frames=e
  end


  if !frames.nil?
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " [self.switch_frame]: frames => #{frames}";

#   frame_list=frames.split(/(frame\(.*\))\.(?=[\w])/)
    frame_list=frames.split(/\.(?=frame)/)


    frame_list.each do |_f|
      Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " processing #{_f}"

      if !_f.empty?
        _id = _f.match(/frame\((.*)\)/)[1]

        Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " [self.switch_frame]: switch_to.frame #{_id}"

        # Swtich based on browser type

        if isChrome?(drv)
          if switch_into_frame(drv, _id).nil?
            Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " Frame with name/id #{_id} not found"
            break
          else
            Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " Sucessfully switched frame into #{_id}"
          end
        else
          Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " [firefox]: switch_to.frame #{_id}"
          drv.switch_to.frame _id
        end

        if false

          if drv.browser.to_s.match(/firefox/i)
            Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " [firefox]: switch_to.frame #{_id}"
            drv.switch_to.frame _id
          else

            if switch_into_frame(drv, _id).nil?
              Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " Frame with name/id #{_id} not found"
              break
            else
              Scoutui::Logger::LogMgr.instance.warn __FILE__ + (__LINE__).to_s + " Sucessfully switched frame into #{_id}"
            end
          end

        end


      end

    end

  end
end

.switch_into_frame(drv, id) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/scoutui/base/q_browser.rb', line 159

def self.switch_into_frame(drv, id)
  _fcnId=" [switch_into_frame]"
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "#{_fcnId}: (#{id})"

  hit=nil

  if isChrome?(drv)

    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "#{_fcnId}: switch on Chrome browser"
    bframes = drv.find_elements(:xpath, '//iframe')

    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "#{_fcnId}: //iframe : size #{bframes.size}"

    if bframes.size == 0
      bframes = drv.find_elements(:xpath, '//frame')

      Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "#{_fcnId}: //frame : #{bframes.size}"
    end

    for i in 0 .. bframes.size - 1
      begin

        _tag = bframes[i].attribute('name')

        if !_tag.nil? && _tag.empty?
          _tag = bframes[i].attribute('id')
        end


        Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "[switch_into_frame.chrome]: <tag, id> :: <#{_tag}, #{id} >"

        if !_tag.empty? && id==_tag

          hit = bframes[i]
          drv.switch_to.frame hit

          Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "#{_fcnId}: swtichframe to #{i} - #{_tag}"
          break
        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

    end

  else
    # Firefox, IE
    Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + "#{_fcnId}: drv.switch_to.frame(#{id.to_s}";

    hit = drv.switch_to.frame(id.to_s.strip)
  end

  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " switch_into_frame(#{id}) => #{hit}"
  hit
end

.wait_for_displayed(drv, locator, _timeout = 30) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/scoutui/base/q_browser.rb', line 41

def self.wait_for_displayed(drv, locator, _timeout=30)
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " wait_for_displayed(#{locator}"
  rc=nil
  begin
    Selenium::WebDriver::Wait.new(timeout: _timeout).until { rc=drv.find_element(:xpath => locator).displayed? }
  rescue => ex
    ;
  end
  rc
end

.wait_for_exist(drv, xpath, _timeout = 30) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scoutui/base/q_browser.rb', line 69

def self.wait_for_exist(drv, xpath, _timeout=30)
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " wait_for_exist(#{xpath}"
  rc=nil
  begin
    Selenium::WebDriver::Wait.new(timeout: _timeout).until { drv.find_element(:xpath => xpath) }
    rc=drv.find_element(:xpath => xpath)
  rescue => ex
    ;
  end
  rc
end

.wait_for_not_displayed(drv, locator, _timeout = 30) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/scoutui/base/q_browser.rb', line 27

def self.wait_for_not_displayed(drv, locator, _timeout=30)
  Scoutui::Logger::LogMgr.instance.debug __FILE__ + (__LINE__).to_s + " wait_for_not_displayed(#{locator}"
  rc=nil
  begin
    Selenium::WebDriver::Wait.new(timeout: _timeout).until {
      rc=drv.find_element(:xpath => locator).displayed?
      obj=getObject(drv, obj, _timeout=nil)
    }
  rescue => ex
    ;
  end
  rc
end

Instance Method Details

#wait_for(seconds) ⇒ Object



911
912
913
# File 'lib/scoutui/base/q_browser.rb', line 911

def wait_for(seconds)
  Selenium::WebDriver::Wait.new(timeout: seconds).until { yield }
end