Module: Castanaut::Plugin::Safari

Defined in:
lib/plugins/safari.rb

Overview

This module provides actions for controlling Safari. It’s tested against Safari 5.1.2 on Mac OS X 10.7.2.

Instance Method Summary collapse

Instance Method Details

#ensure_window_for_safariObject

An applescript fragment by the Movie launch method to determine whether a Safari browser window is open or not.



10
11
12
# File 'lib/plugins/safari.rb', line 10

def ensure_window_for_safari
  "if (count(windows)) < 1 then make new document"
end

#new_tab(str = nil) ⇒ Object

Create a new tab in the front Safari window.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/plugins/safari.rb', line 26

def new_tab(str = nil)
  if str.nil?
    execute_applescript %Q`
      tell front window of application "Safari"
          set the current tab to (make new tab)
      end tell
    `
  else
    execute_applescript %Q`
      tell front window of application "Safari"
          set newTab to make new tab
          set the URL of newTab to "#{str}"
          set the current tab to newTab
      end tell
    `
  end
end

#positioning_for_safariObject

An applescript fragment by the Movie launch method to position the window.



16
17
18
# File 'lib/plugins/safari.rb', line 16

def positioning_for_safari
  nil
end

#to_element(selector, options = {}) ⇒ Object

Get the co-ordinates of an element in the front Safari tab. Use this with Castanaut::Movie#cursor to send the mouse cursor to the element.

Options include:

  • :index - an integer (n) that gets the *n*th element matching the selector. Defaults to the first element.

  • :area - whereabouts in the element do you want the coordinates. Valid values are: left, center, right, and top, middle, bottom. Defaults to [“center”, “middle”]. If single axis is given (eg “left”), the other axis uses its default.

  • :timeout - maximum seconds to wait for the element to appear. Useful if you’re waiting for a page load or AJAX call to complete Defaults to 0.



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
117
118
119
# File 'lib/plugins/safari.rb', line 84

def to_element(selector, options = {})
  pos = options.delete(:area)
  if options[:timeout]
    wait_for_element(selector, options)
    options.delete(:timeout)
  end
  coords = element_coordinates(selector, options)

  x_axis, y_axis = [:center, :middle]
  [pos].flatten.first(2).each do |p|
    p = p.to_s.downcase
    x_axis = p.to_sym if %w[left center right].include?(p)
    y_axis = p.to_sym if %w[top middle bottom].include?(p)
  end

  edge_offset = options[:edge_offset] || 3
  case x_axis
  when :left
    x = coords[0] + edge_offset
  when :center
    x = (coords[0] + coords[2] * 0.5).to_i
  when :right
    x = (coords[0] + coords[2]) - edge_offset
  end

  case y_axis
  when :top
    y = coords[1] + edge_offset
  when :middle
    y = (coords[1] + coords[3] * 0.5).to_i
  when :bottom
    y = (coords[1] + coords[3]) - edge_offset
  end

  result = { :to => { :left => x, :top => y } }
end

#url(str) ⇒ Object

Open a URL in the front Safari tab.



21
22
23
# File 'lib/plugins/safari.rb', line 21

def url(str)
  execute_javascript("location.href = '#{str}'");
end

#wait_for_element(selector, options = {}) ⇒ Object

Sleep until the specified element appears on-screen. Use this if you want to wait until the a page or AJAX request has finished loading before proceding to the next command.

Options include:

  • :timeout - maximum number of seconds to wait for the element to

    appear. Defaults to 10.
    
  • :index - an integer (n) that gets the *n*th element matching the

    selector. Defaults to the first element.
    appear. Defaults to 10.
    
  • :index - an integer (n) that gets the *n*th element matching the

    selector. Defaults to the first element.
    


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/plugins/safari.rb', line 57

def wait_for_element(selector, options = {})
  timeout = Time.now.to_i + (options[:timeout] || 10).to_i
  while true
    begin
      coords = element_coordinates(selector, options)
      return coords unless coords.nil?
    rescue Castanaut::Exceptions::ElementNotFound > e
      raise e if Time.now.to_i > timeout
    end
    sleep 0.3
  end
end