Class: OperaWatir::Window

Inherits:
Object show all
Includes:
Deprecated
Defined in:
lib/operawatir/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ Object

Creates a new window in the browser. Note that Opera does not differentiate between windows and tabs. To directly create a Window object, pass in the Browser.

Examples:

window = browser.url = 'http://example.org/'
=> OperaWatir::Window

Parameters:

  • browser (Object)

    The Browser object.



16
17
18
# File 'lib/operawatir/window.rb', line 16

def initialize(browser)
  self.browser = browser
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(tag, *args) ⇒ Object



183
184
185
186
187
188
# File 'lib/operawatir/window.rb', line 183

def method_missing(tag, *args)
  OperaWatir::Collection.new(self).tap do |c|
    c.selector.tag_name tag
    c.add_selector_from_arguments args
  end
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



4
5
6
# File 'lib/operawatir/window.rb', line 4

def browser
  @browser
end

Instance Method Details

#backObject

Navigates back one step in the browser’s history.



24
25
26
# File 'lib/operawatir/window.rb', line 24

def back
  driver.navigate.back
end

#closeObject

Closes the currently open window.



39
40
41
# File 'lib/operawatir/window.rb', line 39

def close
  driver.close  # FIXME?
end

#documentElement

Finds the root element of the document. For HTML document, this is the element with the tagName “HTML”.

Returns:

  • (Element)

    The root element of the document.



169
170
171
# File 'lib/operawatir/window.rb', line 169

def document
  find_by_css(':root')
end

#elementsCollection

Finds all elements in document.

Returns:



179
180
181
# File 'lib/operawatir/window.rb', line 179

def elements
  find_by_tag('*')
end

#execute_script(js) ⇒ String Also known as: eval_js

Executes the given JavaScript on the current window.

Parameters:

  • js (String)

    The script to be executed.

Returns:

  • (String)

    Optionally returns the JS result.



111
112
113
# File 'lib/operawatir/window.rb', line 111

def execute_script(js)
  object = driver.executeScript(js, [].to_java(:string))
end

#exists?Boolean

Checks if the window is still open. True if the window is still present, false otherwise.

Returns:

  • (Boolean)

    Whether the window is open or not.



102
103
104
105
# File 'lib/operawatir/window.rb', line 102

def exists?
  # TODO: Expose window querying from driver
  url != ''
end

#forwardObject

Navigates forward one step in the browser’s history.



29
30
31
# File 'lib/operawatir/window.rb', line 29

def forward
  driver.navigate.forward
end

#htmlString

Retrieves the HTML/source code of the currently loaded document.

Returns:

  • (String)

    The page source code.



94
95
96
# File 'lib/operawatir/window.rb', line 94

def html
  driver.getPageSource
end

#refreshObject

Refreshes the page.



44
45
46
# File 'lib/operawatir/window.rb', line 44

def refresh
  driver.navigate.refresh
end

#screenshot(filename = nil) ⇒ Object, String

Creates a Screenshot interface or saves screenshot to specified location if a file path is given.

Parameters:

  • file_name (String)

    The absolute path to the location where you want the screenshot saved.

Returns:

  • (Object)

    A Screenshot object.

  • (String)

    Filename to the saved file.



132
133
134
135
136
# File 'lib/operawatir/window.rb', line 132

def screenshot(filename=nil)
  # TODO: This should call document.screenshot instead, but that
  # requires a generic ScreenShotReply interface in OperaDriver.
  filename.nil? ? OperaWatir::Screenshot.new(self) : OperaWatir::Screenshot.new(self).save(filename)
end

#stopObject

Stops the currently loading page.



34
35
36
# File 'lib/operawatir/window.rb', line 34

def stop
  driver.stop
end

#textString

Retrieves the text without the DOM or source code from the currently loaded document.

Returns:

  • (String)

    The text of the document.



87
88
89
# File 'lib/operawatir/window.rb', line 87

def text
  document.text
end

#titleString

Gets the title of the document.

Returns:

  • (String)

    The title of the current document.



51
52
53
# File 'lib/operawatir/window.rb', line 51

def title
  driver.getTitle
end

#urlString

Gets the URL of the document.

Returns:

  • (String)

    The URL of the current document.



61
62
63
# File 'lib/operawatir/window.rb', line 61

def url
  driver.getCurrentUrl
end

#url=(url) ⇒ Object Also known as: goto

Navigates to a new URL.



69
70
71
# File 'lib/operawatir/window.rb', line 69

def url=(url)
  driver.navigate.to(url)
end

#visual_hashString

Returns a visual hash sum of the document. It takes a screenshot of the page and generates an MD5 hash sum of it.

Returns:



145
146
147
# File 'lib/operawatir/window.rb', line 145

def visual_hash
  document.visual_hash
end