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

Inherits:
Object
  • Object
show all
Defined in:
lib/cornucopia/capybara/finder_diagnostics/find_action.rb,
lib/cornucopia/capybara/finder_diagnostics/find_action/found_element.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, **options, &block) ⇒ FindAction

Returns a new instance of FindAction.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 57

def initialize(test_object, report_options, support_options, function_name, *args, **options, &block)
  @test_object     = test_object
  @function_name   = function_name
  @args            = args
  @block           = block
  @support_options = support_options
  @options         = 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.



54
55
56
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 54

def return_value
  @return_value
end

#support_optionsObject

Returns the value of attribute support_options.



55
56
57
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 55

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.



50
51
52
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 50

def self.clear_diagnosed_finders
  @@diagnosed_finders = {}
end

Instance Method Details

#all_elementsObject



279
280
281
282
283
284
285
286
287
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
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 279

def all_elements
  unless @all_elements
    all_options = options.clone

    if all_options && all_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,
                  all_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
      all_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, **all_options.merge(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



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

def all_other_elements
  unless @all_other_elements
    from_element = capybara_session
    all_options  = options.clone
    all_options.delete :from

    return unless from_element

    begin
      @all_other_elements = from_element.all(*search_args, **all_options.merge(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)


91
92
93
94
95
96
97
98
99
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 91

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



101
102
103
104
105
106
107
108
109
110
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 101

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)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 113

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



267
268
269
270
271
272
273
274
275
276
277
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 267

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



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 168

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



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 181

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



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 384

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 = %i[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, **options.merge(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



370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 370

def init_search_args
  unless @search_args
    @search_args = args.clone

    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



365
366
367
368
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 365

def options
  init_search_args
  @options
end

#perform_analysis(attempt_retry) ⇒ Object

def perform_analysis(attempt_retry, attempt_alternate_retry)



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 144

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)



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 129

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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 195

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



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 69

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



360
361
362
363
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 360

def search_args
  init_search_args
  @search_args
end

#simple_run(cornucopia_args = {}) ⇒ Object



83
84
85
86
87
88
# File 'lib/cornucopia/capybara/finder_diagnostics/find_action.rb', line 83

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

  test_object.send(function_name, *simple_run_args, **simple_run_options)
end