Module: Watir::PageContainer

Includes:
Exception
Included in:
Browser, Frame, ModalDialog
Defined in:
lib/watir-classic/page-container.rb

Overview

A PageContainer contains an HTML Document. In other words, it is a what JavaScript calls a Window.

Instance Method Summary collapse

Methods included from Exception

message_for_unable_to_locate

Instance Method Details

#contains_text(target) ⇒ Object

Deprecated.

Use “browser.text.include?(target)” or “browser.text.match(target) instead.”



54
55
56
57
58
59
60
61
62
63
# File 'lib/watir-classic/page-container.rb', line 54

def contains_text(target)
  Kernel.warn "Deprecated(Element#contains_text) - use \"browser.text.include?(target)\" or \"browser.text.match(target)\" instead."
  if target.kind_of? Regexp
    self.text.match(target)
  elsif target.kind_of? String
    self.text.index(target)
  else
    raise ArgumentError, "Argument #{target} should be a string or regexp."
  end
end

#execute_script(source) ⇒ Object

Note:

It is needed to call return inside of the JavaScript if the value is needed at Ruby side.

Execute the given JavaScript string in the context of the current page.

Examples:

browser.execute_script "var a=1; var b=a+1; return b"
browser.execute_script("return {a: 1, b: 2}")["b"] # => 1

Returns:

  • (Object)

    appropriate type of the object, which is returned from the JavaScript via “return” keyword or nil when “return” is omitted.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/watir-classic/page-container.rb', line 22

def execute_script(source)
  result = nil
  begin
    source = with_json2_if_needed source
    result = document.parentWindow.eval(source)
  rescue WIN32OLERuntimeError, NoMethodError #if eval fails we need to use execScript(source.to_s) which does not return a value, hence the workaround
    escaped_src = source.gsub(/\r?\n/, "\\n").gsub("'", "\\\\'")
    wrapper = "_watir_helper_div_#{SecureRandom.uuid}"
    cmd = "var e = document.createElement('DIV'); e.style.display='none'; e.id='#{wrapper}'; e.innerHTML = eval('#{escaped_src}'); document.body.appendChild(e);"
    document.parentWindow.execScript(cmd)
    result = document.getElementById(wrapper).innerHTML
  end

  MultiJson.load(result)["value"] rescue nil
end

#htmlString

Returns html of the current page.

Returns:

  • (String)

    html of the current page.



39
40
41
# File 'lib/watir-classic/page-container.rb', line 39

def html
  page.outerhtml
end

#textString

Returns text of the page.

Returns:

  • (String)

    text of the page.



49
50
51
# File 'lib/watir-classic/page-container.rb', line 49

def text
  page.innertext.strip
end

#urlString

Returns url of the page.

Returns:

  • (String)

    url of the page.



44
45
46
# File 'lib/watir-classic/page-container.rb', line 44

def url
  page.document.location.href
end