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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lapis_lazuli/browser/error.rb', line 81

def get_http_status
        return self.browser.execute_script('
                       function getReq() {
    var req = false;
    if(window.XMLHttpRequest) {
  try {
      req = new XMLHttpRequest();
  } catch(e) {
      req = false;
  }
    } else if(window.ActiveXObject) {
  try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
  } catch(e) {
      req = false;
  }
    }
    if (! req) {
  alert("Your browser does not support XMLHttpRequest.");
    }
    return req;
}

    var req = getReq();

  try {
  req.open("GET", "' + self.browser.url + '", false);
  req.send("");
    } catch (e) {
  success = false;
  error_msg = "Error: " + e;
    }

return req.status;
  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