Module: LapisLazuli::BrowserModule::Error

Included in:
LapisLazuli::Browser
Defined in:
lib/lapis_lazuli/browser/error.rb

Overview

Module with error handling related functionality (World)

Instance Method Summary collapse

Instance Method Details

#get_html_errorsObject

Retrieve errors from HTML elements, using the error_strings config variable



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lapis_lazuli/browser/error.rb', line 42

def get_html_errors
  result = []
  # Need some error strings
  if world.has_env_or_config?("error_strings")
    begin
      # Get the HTML of the page
      page_text = @browser.html
      # Try to find all errors
      world.env_or_config("error_strings").each {|error|
        if page_text.include? error
          # Add to the result list
          result.push error
        end
      }
    rescue RuntimeError => err
      # An error?
      world.log.debug "Cannot read the html for page #{@browser.url}: #{err}"
    end
  end
  # By default we don't have errors
  return result
end

#get_http_statusObject

If the proxy is supported, use it get the HTTP status code.



81
82
83
84
85
86
87
88
89
# File 'lib/lapis_lazuli/browser/error.rb', line 81

def get_http_status
  return self.browser.execute_script <<-JS
    try{
      return lapis_lazuli.http.statusCode;
    } catch(err){
      return null;
    }
  JS
end

#get_js_errorsObject

If the proxy is supported, use it to retrieve JS errors.



68
69
70
71
72
73
74
75
76
# File 'lib/lapis_lazuli/browser/error.rb', line 68

def get_js_errors
  return self.browser.execute_script <<-JS
    try {
      return lapis_lazuli.errors;
    } catch(err){
      return null;
    }
  JS
end

#has_error?Boolean

Does this page have errors? Checks the pagetext for error_strings that are specified in the config

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lapis_lazuli/browser/error.rb', line 18

def has_error?
  errors = self.get_html_errors
  js_errors = self.get_js_errors
  if not js_errors.nil?
    errors += js_errors
  end

  if errors.length > 0 or self.get_http_status.to_i > 299
    errors.each do |error|
      if error.is_a? Hash
        world.log.debug("#{error["message"]} #{error["url"]} #{error["line"]} #{error["column"]}\n#{error["stack"]}")
      else
        world.log.debug("#{error}")
      end
    end
    return true
  end
  return false
end