Class: Cornucopia::Capybara::FinderDiagnostics::FindAction

Inherits:
Object
  • Object
show all
Defined in:
lib/cornucopia/capybara/finder_diagnostics.rb

Overview

At the end of the day, almost everything in Capybara calls all to find the element that needs to be worked on. The main difference is if it is synchronized or not.

The FindAction class also uses all, but it is never synchronized, and its primary purpose is really to output a bunch of diagnostic information to try to help you do a postmortem on just what is happening.

A lot of things could be happening, so a lot of information is output, not all of which is relevant or even useful in every situation.

The first thing output is the error (the problem) Then what action was being tried is output (context)

In case the problem was with finding the desired element, a list of all elements which could be found using the passed in parameters is output.

In case the problem was with context (inside a within block on purpose or accidentally) a list of all other elements which could be found on the page using the passed in parameters is output.

In case the problem is something else, we output a screenshot and the page HTML.

In case the problem has now solved itself, we try the action again. (This has a very low probability of working as this basically devolves to just duplicating what Capybara is already doing, but it seems like it is worth a shot at least…)

In case the problem is a driver bug (specifically Selenium which has some known issues) and is in fact why I even bother trying to do this, try performing the action via javascript. NOTE: As noted in many blogs this type of workaround is not generally a good idea as it can

result in false-positives.  However since Selenium is buggy, this is the
best solution I have other than going to capybara-webkit or poltergeist

Defined Under Namespace

Classes: FoundElement

Constant Summary collapse

@@diagnosed_finders =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_object, report_options, support_options, function_name, *args, &block) ⇒ FindAction

Returns a new instance of FindAction.



98
99
100
101
102
103
104
105
106
107
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 98

def initialize(test_object, report_options, support_options, function_name, *args, &block)
  @test_object     = test_object
  @function_name   = function_name
  @args            = args
  @block           = block
  @support_options = support_options
  @report_options  = report_options || {}

  @report_options[:report] ||= Cornucopia::Util::ReportBuilder.current_report
end

Instance Attribute Details

#return_valueObject

Returns the value of attribute return_value.



95
96
97
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 95

def return_value
  @return_value
end

#support_optionsObject

Returns the value of attribute support_options.



96
97
98
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 96

def support_options
  @support_options
end

Class Method Details

.clear_diagnosed_findersObject

Clears the class variable @@diagnosed_finders between tests if called. This is done so that finder analysis is called at least once per test.



91
92
93
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 91

def self.clear_diagnosed_finders
  @@diagnosed_finders = {}
end

Instance Method Details

#all_elementsObject



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
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 323

def all_elements
  unless @all_elements
    if options && options.has_key?(:from)
      from_within = nil

      @report_options[:report].within_table(report_table: @report_options[:table]) do |report_table|
        Cornucopia::Util::ReportTable.new(nested_table:       report_table,
                                          nested_table_label: "Within block:") do |sub_report|
          sub_report_options         = @report_options.clone
          sub_report_options[:table] = sub_report
          from_within                = Cornucopia::Capybara::FinderDiagnostics::FindAction.
              new(@test_object,
                  sub_report_options,
                  {},
                  :find,
                  :select,
                  options[:from])

          from_within.generate_report_in_table(sub_report, nil)
        end
      end

      from_element = from_within.found_element
      if search_args[0].is_a?(Symbol)
        search_args[0] = :option
      end
      options.delete(:from)

      unless from_element
        @all_elements = []
        return @all_elements
      end
    else
      from_element = @test_object
    end

    begin
      @all_elements = from_element.all(*search_args, visible: false, __cornucopia_no_analysis: true).to_a
    rescue
      @all_elements = []
    end

    if @all_elements
      @all_elements = @all_elements.map do |element|
        FoundElement.new(element)
      end.compact
    end
  end

  @all_elements
end

#all_other_elementsObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 375

def all_other_elements
  unless @all_other_elements
    from_element = capybara_session

    return unless from_element

    begin
      @all_other_elements = from_element.all(*search_args, visible: false, __cornucopia_no_analysis: true).to_a
    rescue
      @all_other_elements = []
    end

    if @all_other_elements
      @all_other_elements = @all_other_elements.map do |element|
        FoundElement.new(element) unless all_elements.include?(element)
      end

      @all_other_elements = @all_other_elements - @all_elements
      @all_other_elements.compact!
    end
  end

  @all_other_elements
end

#can_dump_details?(attempt_retry) ⇒ Boolean

def can_dump_details?(attempt_retry, attempt_alternate_retry)

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 135

def can_dump_details?(attempt_retry)
  can_dump = false

  if (Object.const_defined?("Capybara"))
    can_dump = !@@diagnosed_finders.keys.include?(dump_detail_args(attempt_retry))
  end

  can_dump
end

#capybara_sessionObject



145
146
147
148
149
150
151
152
153
154
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 145

def capybara_session
  if Object.const_defined?("::Capybara") &&
      ::Capybara.send(:session_pool).present?
    my_page = ::Capybara.current_session

    my_page if (my_page && my_page.current_url.present? && my_page.current_url != "about:blank")
  end
rescue StandardError
  nil
end

#dump_detail_args(attempt_retry) ⇒ Object

def dump_detail_args(attempt_retry, attempt_alternate_retry)



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 157

def dump_detail_args(attempt_retry)
  check_args = search_args.clone
  my_page    = capybara_session

  check_args << options.clone
  check_args << !!attempt_retry
  # check_args << !!attempt_alternate_retry

  if (my_page && my_page.current_url.present? && my_page.current_url != "about:blank")
    check_args << Digest::SHA2.hexdigest(my_page.html)
  end

  check_args
end

#found_elementObject

def alternate_action_with_found_element report, report_table

return_result = false

result = "Could not attempt to try the action through an alternate method."
if found_element &&
    ::Capybara.current_session.respond_to?(:evaluate_script)
  begin
    native_id = get_attribute found_element, "id"
    if (native_id)
      case @function_name.to_s
        when "click_link_or_button", "click_link", "click_button"
          @return_value = ::Capybara.current_session.evaluate_script("$(\"\##{native_id}\")[0].click()")
        when "fill_in"
          @return_value = ::Capybara.current_session.evaluate_script("$(\"\##{native_id}\")[0].val(\"#{options[:with]}\")")
        when "choose", "check"
          @return_value = ::Capybara.current_session.evaluate_script("$(\"\##{native_id}\")[0].val(\"checked\", true)")
        when "uncheck"
          @return_value = ::Capybara.current_session.evaluate_script("$(\"\##{native_id}\")[0].val(\"checked\", false)")
        when "select"
          @return_value = ::Capybara.current_session.evaluate_script("$(\"\##{native_id}\")[0].val(\"selected\", true)")
        when "unselect"
          @return_value = ::Capybara.current_session.evaluate_script("$(\"\##{native_id}\")[0].val(\"selected\", false)")
        else
          result = "Could not decide what to do with #{@function_name}"
          raise new Exception("unknown action")
      end

      return_result = true
    end
  rescue
    result ||= "Still couldn't do the action - #{$!.to_s}."
  end
end

report_table.write_stats "Trying alternate action:", result if report_table
return_result

end



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 311

def found_element
  if @function_name.to_sym == :all
    all_elements.map(&:found_element)
  else
    if all_elements && all_elements.length == 1
      all_elements[0].try(:found_element)
    else
      nil
    end
  end
end

#generate_report(message, error = nil, &block) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 212

def generate_report(message, error = nil, &block)
  if @report_options[:report] && @report_options[:table]
    generate_report_in_table @report_options[:table], error, &block
  else
    @report_options[:report] ||= Cornucopia::Util::ReportBuilder.current_report
    @report_options[:table]  = nil

    @report_options[:report].within_section(message) do |report|
      generate_report_in_table @report_options[:table], error, &block
    end
  end
end

#generate_report_in_table(table, error = nil, &block) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 225

def generate_report_in_table(table, error = nil, &block)
  @report_options[:table] = table

  init_search_args
  all_elements
  all_other_elements
  guessed_types

  configured_report = Cornucopia::Util::Configuration.report_configuration(:capybara_finder_diagnostics)

  configured_report.add_report_objects(finder: self, exception: error)
  configured_report.generate_report(@report_options[:report], report_table: @report_options[:table], &block)
end

#guessed_typesObject

a list of guesses as to what kind of object is being searched for



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 428

def guessed_types
  unless @guessed_types
    if search_args.length > 0
      if search_args[0].is_a?(Symbol)
        @guessed_types = [search_args[0]]
      else
        @guessed_types = [:id, :css, :xpath, :link_or_button, :fillable_field, :radio_button, :checkbox, :select, :option,
                          :file_field, :table, :field, :fieldset, :content].select do |test_type|
          begin
            @test_object.all(test_type, *search_args, visible: false, __cornucopia_no_analysis: true).length > 0
          rescue
            # Normally bad form, but for this function, we just don't want this to throw errors.
            # We are only concerned with whatever actually succeeds.
            false
          end
        end
      end
    end
  end

  @guessed_types
end

#init_search_argsObject



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 410

def init_search_args
  unless @search_args
    @search_args = @args.clone
    @options     = {}

    if @search_args.last.is_a? Hash
      @options = @search_args.pop
    end
    if guessed_types.length > 0 && @search_args[0] != guessed_types[0]
      @search_args.insert(0, guessed_types[0])
    end
    if guessed_types.length <= 0 && @search_args[0] != ::Capybara.default_selector
      @search_args.insert(0, ::Capybara.default_selector)
    end
  end
end

#optionsObject



405
406
407
408
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 405

def options
  init_search_args
  @options
end

#perform_analysis(attempt_retry) ⇒ Object

def perform_analysis(attempt_retry, attempt_alternate_retry)



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 188

def perform_analysis(attempt_retry)
  retry_successful = false

  time = Benchmark.measure do
    puts "  Cornucopia::FinderDiagnostics::perform_analysis" if Cornucopia::Util::Configuration.benchmark

    if can_dump_details?(attempt_retry)
      generate_report "An error occurred while processing \"#{@function_name.to_s}\":",
                      $! do |report, report_table|
        retry_successful = perform_retry(attempt_retry, report, report_table)
      end

      dump_args                      = dump_detail_args(attempt_retry)
      @@diagnosed_finders[dump_args] = { tried: true }
    else
      retry_successful = perform_retry(attempt_retry, nil, nil)
    end
  end

  puts "  Cornucopia::FinderDiagnostics::perform_analysis time: #{time}" if Cornucopia::Util::Configuration.benchmark

  retry_successful
end

#perform_retry(attempt_retry, report, report_table) ⇒ Object

def perform_retry(attempt_retry, attempt_alternate_retry, report, report_table)



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 173

def perform_retry(attempt_retry, report, report_table)
  retry_successful = false

  if attempt_retry && retry_action_with_found_element(report, report_table)
    retry_successful = true
    # else
    #   if attempt_alternate_retry && alternate_action_with_found_element(report, report_table)
    #     retry_successful = true
    #   end
  end

  retry_successful
end

#retry_action_with_found_element(report, report_table) ⇒ Object



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
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 239

def retry_action_with_found_element report, report_table
  return_result = false
  result        = "Failed"

  case @function_name.to_s
    when "assert_selector"
      if found_element
        @return_value = true
        return_result = true
        result        = "Found"
      end

    when "assert_no_selector"
      unless found_element
        @return_value = true
        return_result = true
        result        = "Not Found"
      end

    when "find", "all"
      if found_element
        result = "Success"

        @return_value = found_element

        return_result = true
      end
  end

  report_table.write_stats "Retrying action:", result if report_table && return_result

  return_result
end

#runObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 109

def run
  begin
    simple_run
  rescue
    error = $!
    if perform_analysis(support_options[:__cornucopia_retry_with_found])
      # Cornucopia::Util::Configuration.alternate_retry)
      @return_value
    else
      raise error
    end
  end
end

#search_argsObject



400
401
402
403
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 400

def search_args
  init_search_args
  @search_args
end

#simple_run(cornucopia_args = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/cornucopia/capybara/finder_diagnostics.rb', line 123

def simple_run(cornucopia_args = {})
  simple_run_args    = @args.clone
  simple_run_options = {}

  if simple_run_args.last.is_a? Hash
    simple_run_options = simple_run_args.pop
  end

  @test_object.send(@function_name, *simple_run_args, simple_run_options.merge(cornucopia_args))
end