Class: Cornucopia::Capybara::PageDiagnostics

Inherits:
Object
  • Object
show all
Defined in:
lib/cornucopia/capybara/page_diagnostics.rb,
lib/cornucopia/capybara/page_diagnostics/window_iterator.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.



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

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_windowsObject

Returns the value of attribute allow_other_windows.



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

def allow_other_windows
  @allow_other_windows
end

Class Method Details

.clear_dumped_pagesObject



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

def clear_dumped_pages
  @@dumped_pages = []
end

.dump_details(options = {}) ⇒ Object

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



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

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

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



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

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_cookiesObject



164
165
166
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 164

def all_cookies
  execute_browser_function(:all_cookies, nil)
end

#browser_infoObject



160
161
162
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 160

def browser_info
  execute_browser_function(:logs, nil)
end

#browser_logsObject



153
154
155
156
157
158
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 153

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)


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

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
rescue StandardError
  false
end

#dump_detailsObject



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

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_tableObject



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

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 130

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



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

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_fileObject



235
236
237
238
239
240
241
242
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 235

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_frameObject



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

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

#html_sourceObject



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

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

#other_windowsObject



108
109
110
111
112
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 108

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

#page_heightObject



183
184
185
186
187
188
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 183

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

#page_urlObject



168
169
170
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 168

def page_url
  execute_driver_function(:current_url, nil)
end

#page_widthObject



176
177
178
179
180
181
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 176

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

#response_headersObject



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

def response_headers
  execute_driver_function(:response_headers, nil)
end

#screen_shotObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 208

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_codeObject



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

def status_code
  execute_driver_function(:status_code, nil)
end

#titleObject



172
173
174
# File 'lib/cornucopia/capybara/page_diagnostics.rb', line 172

def title
  execute_driver_function(:title, nil)
end