Module: Insite::CommonMethods

Included in:
Insite, Component, ComponentCollection, DefinedPage, Element, UndefinedPage
Defined in:
lib/insite/methods/common_methods.rb

Instance Method Summary collapse

Instance Method Details

#document(xpath = nil) ⇒ Object Also known as: nokogiri

Returns a Nokogiri document for the object ONLY. So no need to specify a relative path.



6
7
8
9
10
11
12
# File 'lib/insite/methods/common_methods.rb', line 6

def document(xpath = nil)
  if xpath
    Nokogiri::HTML(html).xpath(xpath)
  else
    Nokogiri::HTML(html)
  end
end

#process_browserObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/insite/methods/common_methods.rb', line 15

def process_browser
  if @site.browser.is_a?(Watir::Browser)
    begin
      if @site.browser.exists?
        return @site.browser
      else
        raise(
          Insite::Errors::BrowserClosedError,
          "Browser check failed. The browser is no longer present.\n\n"
        )
      end
    rescue => e
      raise(
        Insite::Errors::BrowserResponseError,
        <<~eos
        Browser check failed. The browser returned an #{e.class} (#{e}) when it was queried.
        Backtrace for the error:
        #{e.backtrace.join("\n")}

        eos
      )
    end
  elsif @site.browser.nil?
    raise(
      Insite::Errors::BrowserNotOpenError,
      "A browser has not been defined for the site. Try using Site#open to " \
      "start a browser.\n\n"
    )
  else
    raise(
      Insite::Errors::BrowserNotValidError,
      "Expected: Watir::Browser. Actual: #{@site.browser.class}.\n\n"
    )
  end

end

#update_object(hash_args = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
157
158
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
# File 'lib/insite/methods/common_methods.rb', line 53

def update_object(hash_args = {})
  rescues = [
    Watir::Exception::ObjectDisabledException,
    Watir::Exception::UnknownObjectException,
    Selenium::WebDriver::Error::ElementNotVisibleError,
    Selenium::WebDriver::Error::UnknownError
  ]
  failed = []
  hash_args.each do |k, v|
    sleep 0.2
    begin
      # k = k.to_sym
      # if self.is_a?(Component)
      #   elems = @component_elements
      # else
      #   elems = @page_elements
      # end
      #
      # # TODO: DRY
      # if self.is_a?(Insite::DefinedPage) && @page_elements.include?(k)
      elem = public_send(k)
      if elem.is_a?(Insite::Element)
        if [Insite::FileField, Insite::TextField, Insite::TextArea].include? elem.class
          elem.set v
        elsif [Insite::Select].include? elem.class
          elem.select v
        elsif [Insite::Anchor, Insite::Button].include? elem.class
          case v
          when Symbol
            elem.public_send v
          when TrueClass
            elem.click
          when FalseClass
            # Do nothing here.
          else
            raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}'"
          end
        elsif elem.is_a?(Insite::Radio)
          case v
          when Symbol
            elem.public_send v
          when TrueClass
            3.times do
              elem.set
              break if elem.set?
              sleep 0.5
            end
          when FalseClass
            raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}' \
            (You can only set a radio button, so false is not a valid argument.)"
          else
            raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}'"
          end
        elsif elem.is_a?(Insite::CheckBox)
          case v
          when Symbol
            elem.public_send v
          when TrueClass
            3.times do
              elem.set
              break if elem.set?
              sleep 0.5
            end
          when FalseClass
            3.times do
              elem.clear
              break if !elem.set?
              sleep 0.5
            end
            elem.clear
          else
            raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}'"
          end
        else
          case v
          when Symbol
            elem.public_send v
          when TrueClass
            elem.set
          when FalseClass
            elem.clear
          else
            raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}'"
          end
        end
      elsif elem.is_a?(Component) #&& @component_elements.include?(k)

        w = public_send(k)

        begin
          if w.is_a?(Component) && v.is_a?(Hash)
            w.update_object(**v)
          elsif w.is_a?(Component) && w.respond_to?(:update)
            w.update(v)
          end
        rescue => e
          begin
            if v.is_a?(Array)
              public_send(k).update(v)
            else
              public_send(k).update(*v)
            end
          rescue => e2
            raise ArgumentError, "Dynamic method call failed for #{k}.", e2.backtrace.join("\n")
          end
        end
      else
        begin
          if v.is_a?(Array)
            public_send(k, *v)
          else
            public_send(k, v)
          end
        rescue => e
          begin
            if v.is_a?(Array)
              public_send(k).update(v)
            else
              public_send(k).update(*v)
            end
          rescue => e2
            raise(
              ArgumentError,
              "Dynamic method call failed for #{k}: #{e2.try(:message)}",
              e2.backtrace
            )
          end
        end
      end
    rescue => e
      if rescues.any? { |err| e.is_a?(err) }
        unless failed.include?(k)
          puts "Rescued #{e.class} when trying to update #{k}. Sleeping 5 seconds and then trying again."
          failed << k
          sleep 5
          redo
        end
      else
        raise e, "Failure trying to update #{k} with #{v.class}: #{v}. Original message: #{e.try(:message)}\n" + e.backtrace.join("\n")
      end
    end
    sleep 0.2
  end
  sleep 1
  hash_args
end