Module: Vapir::Firefox::PageContainer

Includes:
Container, PageContainer
Included in:
Vapir::Firefox, Frame, ModalDialogDocument
Defined in:
lib/vapir-firefox/page_container.rb

Instance Method Summary collapse

Methods included from Container

#element_by_xpath, #element_object_by_xpath, #element_objects_by_xpath, #elements_by_xpath, #extra_for_contained, #visible_text_nodes

Instance Method Details

#execute_script(javascript, other_variables = {}) ⇒ Object

evaluates a given javascript string in the context of the browser’s content window. anything that is a top-level variable on the window will be seen as a top-level variable in the evaluated script.

returns the last evaluated expression.

raises an error if the given javascript errors.

you may specify a hash of other variables that will be available in your script. for example:

>> browser.execute_script("element.tagName + ' ' + foo", :element => browser.buttons.first.element_object, :foo => "baz")
=> "BUTTON baz"

note, however, that if the name of the variable that you use is the same as a variable on the window, the window’s variable is what will be in scope. for example:

>> browser.execute_script("typeof document", :document => "a string")
=> "object"

the type is ‘object’ (not ‘string’) because window.document is what is seen in the scope.

this function is most useful if you need to execute javascript that is only allowed to run in the context of the content window. one example of this is Flash objects - if you try to access their methods from the top-level context, you get an exception:

>> browser.element(:tag_name => 'embed').element_object.PercentLoaded()
JsshError::Error: NPMethod called on non-NPObject wrapped JSObject!

but, this method executes script in the context of the content window, so the following works:

>> browser.execute_script('element.PercentLoaded()', :element => browser.element(:tag_name => 'embed').element_object)
=> 100


45
46
47
48
49
50
51
52
# File 'lib/vapir-firefox/page_container.rb', line 45

def execute_script(javascript, other_variables={})
  sandbox=jssh_socket.Components.utils.Sandbox(content_window_object)
  sandbox.window=content_window_object.window
  other_variables.each do |name, var|
    sandbox[name]=var
  end
  return jssh_socket.Components.utils.evalInSandbox('with(window) { '+javascript+' }', sandbox)
end

#outer_htmlObject Also known as: html

Returns the html of the document



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vapir-firefox/page_container.rb', line 55

def outer_html
  jssh_socket.call_function(:document => document_object) do %Q(
    var temp_el=document.createElement('div');
    for(var i in document.childNodes)
    { try
      { temp_el.appendChild(document.childNodes[i].cloneNode(true));
      }
      catch(e)
      {}
    }
    return temp_el.innerHTML;
  )
  end
end

#textObject



9
10
11
# File 'lib/vapir-firefox/page_container.rb', line 9

def text
  document_element.textContent
end