Class: Cornucopia::Capybara::PageDiagnostics

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

Defined Under Namespace

Classes: WindowIterator

Constant Summary collapse

@@dumped_pages =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PageDiagnostics

Returns a new instance of PageDiagnostics.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 35

def initialize(options = {})
  @options             = options.clone
  @report              = @options.delete(:report)
  @table               = @options.delete(:table)
  @unsupported_list    ||= []
  @allow_other_windows = true

  @page_url         = "use accessor"
  @title            = "use accessor"
  @page_width       = "use accessor"
  @page_height      = "use accessor"
  @response_headers = "use accessor"
  @status_code      = "use accessor"
  @html_source      = "use accessor"
  @html_frame       = "use accessor"
  @screen_shot      = "use accessor"
  @html_file        = "use accessor"
  @browser_logs     = "use accessor"
  @browser_info     = "use accessor"
  @all_cookies      = "use accessor"
end

Instance Attribute Details

#allow_other_windows ⇒ Object

Returns the value of attribute allow_other_windows.



17
18
19
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 17

def allow_other_windows
  @allow_other_windows
end

Class Method Details

.clear_dumped_pages ⇒ Object



30
31
32
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 30

def clear_dumped_pages
  @@dumped_pages = []
end

.dump_details(options = {}) ⇒ Object

This outputs the details about the current Capybara page to the current report.



22
23
24
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 22

def dump_details(options = {})
  Cornucopia::Capybara::PageDiagnostics.new(options).dump_details
end

.dump_details_in_table(report, report_table, options = {}) ⇒ Object



26
27
28
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 26

def dump_details_in_table(report, report_table, options = {})
  Cornucopia::Capybara::PageDiagnostics.new(options.merge(report: report, table: report_table)).dump_details
end

Instance Method Details

#all_cookies ⇒ Object



191
192
193
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 191

def all_cookies
  execute_browser_function(:all_cookies, nil)
end

#browser_info ⇒ Object



187
188
189
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 187

def browser_info
  execute_browser_function(:logs, nil)
end

#browser_logs ⇒ Object



180
181
182
183
184
185
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 180

def browser_logs
  value = execute_browser_function(:logs, nil)

  types = value.available_types
  types.each_with_object({}) { |type, hash| hash[type] = value.get(type) }
end

#can_dump_details? ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 57

def can_dump_details?
  can_dump = false

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

    if (my_page && my_page.current_url.present? && my_page.current_url != "about:blank")
      can_dump = !@@dumped_pages.include?(Digest::SHA2.hexdigest(my_page.html))
    end
  end

  can_dump
end

#dump_details ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 72

def dump_details
  if can_dump_details?
    if @report && @table
      dump_details_in_table
    else
      @report       = Cornucopia::Util::ReportBuilder.current_report
      section_title = @options[:section_label] || "Page Dump:"

      @report.within_section(section_title) do
        @table = nil
        dump_details_in_table
      end
    end
  end
end

#dump_details_in_table ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 88

def dump_details_in_table
  if can_dump_details?
    @session = ::Capybara.current_session
    @driver  = @session.driver

    @current_window = execute_driver_function(:current_window_handle, nil)
    @window_handles = execute_driver_function(:window_handles, [1]).clone

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

    configured_report.add_report_objects(capybara: self)
    configured_report.generate_report(@report, report_table: @table)

    @@dumped_pages << Digest::SHA2.hexdigest(@session.html)
  end
end

#execute_browser_function(function_symbol, unsupported_value, *args) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 157

def execute_browser_function(function_symbol, unsupported_value, *args)
  value = unsupported_value

  unless @driver.browser.respond_to?(function_symbol) ||
      @driver.browser.manage.respond_to?(function_symbol)
    @unsupported_list << function_symbol unless @driver.browser.respond_to?(function_symbol)
  end

  begin
    unless @unsupported_list.include?(function_symbol)
      value = if @driver.browser.respond_to?(function_symbol)
                @driver.browser.send(function_symbol, *args)
              else
                @driver.browser.manage.send(function_symbol, *args)
              end
    end
  rescue ::Capybara::NotSupportedByDriverError
    @unsupported_list << function_symbol
  end

  value
end

#execute_driver_function(function_symbol, unsupported_value, *args) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 141

def execute_driver_function(function_symbol, unsupported_value, *args)
  value = unsupported_value

  @unsupported_list << function_symbol unless @driver.respond_to?(function_symbol)

  begin
    unless @unsupported_list.include?(function_symbol)
      value = @driver.send(function_symbol, *args)
    end
  rescue ::Capybara::NotSupportedByDriverError
    @unsupported_list << function_symbol
  end

  value
end

#html_file ⇒ Object



262
263
264
265
266
267
268
269
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 262

def html_file
  dir_name = @report.unique_folder_name("html_save_file")
  FileUtils.mkdir_p File.join(@report.report_test_folder_name, dir_name)
  ::Capybara.current_session.
      save_page(File.join(@report.report_test_folder_name, dir_name, "__cornucopia_save_page.html"))
  "<a href=\"#{File.join(dir_name, "__cornucopia_save_page.html")}\" target=\"_blank\">Saved Page</a>".
      html_safe
end

#html_frame ⇒ Object



230
231
232
233
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 230

def html_frame
  value = execute_driver_function(:html, nil)
  @report.page_frame(value) if value
end

#html_source ⇒ Object



225
226
227
228
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 225

def html_source
  value = execute_driver_function(:html, nil)
  @report.page_text(value) if value
end

#other_windows ⇒ Object



135
136
137
138
139
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 135

def other_windows
  if @allow_other_windows
    Cornucopia::Capybara::PageDiagnostics::WindowIterator.new(@window_handles, @current_window, self)
  end
end

#page_height ⇒ Object



210
211
212
213
214
215
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 210

def page_height
  if @current_window
    value = execute_driver_function(:window_size, nil, @current_window)
    value[1] if value
  end
end

#page_url ⇒ Object



195
196
197
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 195

def page_url
  execute_driver_function(:current_url, nil)
end

#page_width ⇒ Object



203
204
205
206
207
208
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 203

def page_width
  if @current_window
    value = execute_driver_function(:window_size, nil, @current_window)
    value[0] if value
  end
end

#response_headers ⇒ Object



217
218
219
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 217

def response_headers
  execute_driver_function(:response_headers, nil)
end

#screen_shot ⇒ Object



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

def screen_shot
  dir_name = File.join(@report.report_test_folder_name, "temporary_folder")

  begin
    page_name = @options[:screen_shot_name] || "screen_shot"
    page_name = page_name [Dir.pwd.length..-1] if page_name.start_with?(Dir.pwd)
    page_name = page_name [1..-1] if page_name.start_with?("/")
    page_name = page_name["features/".length..-1] if page_name.start_with?("features/")
    page_name = page_name.gsub(/[^a-z0-9_]/i, "_")
    page_name = page_name.gsub("__", "_")

    page_name = File.join(dir_name, "#{page_name}.png")

    FileUtils.mkdir_p dir_name

    execute_driver_function(:save_screenshot, nil, page_name)

    if File.exist?(page_name)
      @report.image_link(page_name)
    else
      "Could not save screen_shot."
    end
  ensure
    FileUtils.rm_rf dir_name
  end
end

#status_code ⇒ Object



221
222
223
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 221

def status_code
  execute_driver_function(:status_code, nil)
end

#title ⇒ Object



199
200
201
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 199

def title
  execute_driver_function(:title, nil)
end