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
-
#contains_text(target) ⇒ Object
deprecated
Deprecated.
Use “browser.text.include?(target)” or “browser.text.match(target) instead.”
-
#execute_script(source) ⇒ Object
Execute the given JavaScript string in the context of the current page.
-
#html ⇒ String
Html of the current page.
-
#text ⇒ String
Text of the page.
-
#url ⇒ String
Url of the page.
Methods included from Exception
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.
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 |
#html ⇒ String
Returns html of the current page.
39 40 41 |
# File 'lib/watir-classic/page-container.rb', line 39 def html page.outerhtml end |
#text ⇒ String
Returns text of the page.
49 50 51 |
# File 'lib/watir-classic/page-container.rb', line 49 def text page.innertext.strip end |
#url ⇒ String
Returns url of the page.
44 45 46 |
# File 'lib/watir-classic/page-container.rb', line 44 def url page.document.location.href end |