Class: Seleniumrc::SeleniumDriver

Inherits:
Selenium::SeleniumDriver
  • Object
show all
Includes:
WaitFor
Defined in:
lib/seleniumrc/selenium_driver.rb

Instance Attribute Summary collapse

Attributes included from WaitFor

#default_timeout

Instance Method Summary collapse

Methods included from WaitFor

#default_wait_for_time, #flunk, #time_class, #wait_for

Instance Attribute Details

#server_hostObject (readonly)

Returns the value of attribute server_host.



4
5
6
# File 'lib/seleniumrc/selenium_driver.rb', line 4

def server_host
  @server_host
end

#server_portObject (readonly)

Returns the value of attribute server_port.



4
5
6
# File 'lib/seleniumrc/selenium_driver.rb', line 4

def server_port
  @server_port
end

Instance Method Details

#browser_start_commandObject



6
7
8
# File 'lib/seleniumrc/selenium_driver.rb', line 6

def browser_start_command
  @browserStartCommand
end

#browser_urlObject



10
11
12
# File 'lib/seleniumrc/selenium_driver.rb', line 10

def browser_url
  @browserURL
end

#click(locator) ⇒ Object Also known as: wait_for_and_click



54
55
56
57
# File 'lib/seleniumrc/selenium_driver.rb', line 54

def click(locator)
  wait_for_is_element_present(locator)
  super
end

#click_and_wait(locator, wait_for = default_timeout) ⇒ Object Also known as: click_and_wait_for_page_to_load

Click a link and wait for the page to load.



66
67
68
69
# File 'lib/seleniumrc/selenium_driver.rb', line 66

def click_and_wait(locator, wait_for = default_timeout)
  click locator
  wait_for_page_to_load(wait_for)
end

#element(locator) ⇒ Object



32
33
34
# File 'lib/seleniumrc/selenium_driver.rb', line 32

def element(locator)
  SeleniumElement.new(self, locator)
end

#element_contains_text(locator, text) ⇒ Object

Does the element at locator contain the text?



91
92
93
# File 'lib/seleniumrc/selenium_driver.rb', line 91

def element_contains_text(locator, text)
  is_element_present(locator) && get_inner_html(locator).include?(text)
end

#element_does_not_contain_text(locator, text) ⇒ Object

Does the element at locator not contain the text?



96
97
98
99
# File 'lib/seleniumrc/selenium_driver.rb', line 96

def element_does_not_contain_text(locator, text)
  return true unless is_element_present(locator)
  return !get_inner_html(locator).include?(text)
end

#fast_modeObject

Speeds up each Selenese step to normal speed after this method is called.



184
185
186
187
# File 'lib/seleniumrc/selenium_driver.rb', line 184

def fast_mode
  get_eval "slowMode = false"
  get_eval 'window.document.getElementsByName("FASTMODE")[0].checked = false'
end

#get_inner_html(locator) ⇒ Object

Get the inner html of the located element.



86
87
88
# File 'lib/seleniumrc/selenium_driver.rb', line 86

def get_inner_html(locator)
  get_eval(inner_html_js(locator))
end

#go_back_and_waitObject

Click the back button and wait for the page to load.



73
74
75
76
# File 'lib/seleniumrc/selenium_driver.rb', line 73

def go_back_and_wait
  go_back
  wait_for_page_to_load
end

#inner_html_js(locator) ⇒ Object



189
190
191
# File 'lib/seleniumrc/selenium_driver.rb', line 189

def inner_html_js(locator)
  %Q|this.page().findElement("#{locator}").innerHTML|
end

#insert_javascript_file(uri) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/seleniumrc/selenium_driver.rb', line 18

def insert_javascript_file(uri)
  js = <<-USEREXTENSIONS
  var headTag = document.getElementsByTagName("head").item(0);
  var scriptTag = document.createElement("script");
  scriptTag.src = "#{uri}";
  headTag.appendChild( scriptTag );
  USEREXTENSIONS
  get_eval(js)
end

#insert_user_extensionsObject



28
29
30
# File 'lib/seleniumrc/selenium_driver.rb', line 28

def insert_user_extensions
  insert_javascript_file("/selenium/user-extensions.js")
end

#is_text_in_order(locator, *text_fragments) ⇒ Object

Does locator element have text fragments in a certain order?



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/seleniumrc/selenium_driver.rb', line 102

def is_text_in_order(locator, *text_fragments)
  container = Hpricot(get_text(locator))

  everything_found = true
  wasnt_found_message = "Certain fragments weren't found:\n"

  everything_in_order = true
  wasnt_in_order_message = "Certain fragments were out of order:\n"

  text_fragments.inject([-1, nil]) do |old_results, new_fragment|
    old_index = old_results[0]
    old_fragment = old_results[1]
    new_index = container.inner_html.index(new_fragment)

    unless new_index
      everything_found = false
      wasnt_found_message << "Fragment #{new_fragment} was not found\n"
    end

    if new_index < old_index
      everything_in_order = false
      wasnt_in_order_message << "Fragment #{new_fragment} out of order:\n"
      wasnt_in_order_message << "\texpected '#{old_fragment}'\n"
      wasnt_in_order_message << "\tto come before '#{new_fragment}'\n"
    end

    [new_index, new_fragment]
  end

  wasnt_found_message << "\n\nhtml follows:\n #{container.inner_html}\n"
  wasnt_in_order_message << "\n\nhtml follows:\n #{container.inner_html}\n"

  unless everything_found && everything_in_order
    yield(everything_found, wasnt_found_message, everything_in_order, wasnt_in_order_message)
  end
end

#open(url) ⇒ Object Also known as: open_and_wait

Open the home page of the Application and wait for the page to load.



79
80
81
82
# File 'lib/seleniumrc/selenium_driver.rb', line 79

def open(url)
  super
  wait_for_page_to_load
end

#pageObject



36
37
38
# File 'lib/seleniumrc/selenium_driver.rb', line 36

def page
  SeleniumPage.new(self)
end

#reloadObject

Reload the current page that the browser is on.



50
51
52
# File 'lib/seleniumrc/selenium_driver.rb', line 50

def reload
  get_eval("selenium.browserbot.getCurrentWindow().location.reload()")
end

#select(select_locator, option_locator) ⇒ Object



60
61
62
63
# File 'lib/seleniumrc/selenium_driver.rb', line 60

def select(select_locator, option_locator)
  wait_for_is_element_present(select_locator)
  super
end

#show_log(log_level = "debug") ⇒ Object

Open the log window on the browser. This is useful to diagnose issues with Selenium Core.



173
174
175
# File 'lib/seleniumrc/selenium_driver.rb', line 173

def show_log(log_level = "debug")
  get_eval "LOG.setLogLevelThreshold('#{log_level}')"
end

#slow_modeObject

Slow down each Selenese step after this method is called.



178
179
180
181
# File 'lib/seleniumrc/selenium_driver.rb', line 178

def slow_mode
  get_eval "slowMode = true"
  get_eval 'window.document.getElementsByName("FASTMODE")[0].checked = true'
end

#timeout_in_millisecondsObject



14
15
16
# File 'lib/seleniumrc/selenium_driver.rb', line 14

def timeout_in_milliseconds
  @timeout
end

#type(locator, value) ⇒ Object

Type text into a page element



44
45
46
47
# File 'lib/seleniumrc/selenium_driver.rb', line 44

def type(locator, value)
  wait_for_is_element_present(locator)
  super
end

#wait_for_element_to_contain(locator, text, message = nil, timeout = default_wait_for_time) ⇒ Object Also known as: wait_for_element_to_contain_text



165
166
167
168
169
# File 'lib/seleniumrc/selenium_driver.rb', line 165

def wait_for_element_to_contain(locator, text, message=nil, timeout=default_wait_for_time)
  wait_for(:message => message, :timeout => timeout) do
    element_contains_text(locator, text)
  end
end

#wait_for_is_element_not_present(locator, params = {}) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/seleniumrc/selenium_driver.rb', line 149

def wait_for_is_element_not_present(locator, params={})
  params = {
    :message => "Expected element '#{locator}' to be absent, but it was not"
  }.merge(params)
  wait_for(:message => params[:message]) do
    !is_element_present(locator)
  end
end

#wait_for_is_element_present(locator, params = {}) ⇒ Object

—– Waiting for conditions



140
141
142
143
144
145
146
147
# File 'lib/seleniumrc/selenium_driver.rb', line 140

def wait_for_is_element_present(locator, params={})
  params = {
    :message => "Expected element '#{locator}' to be present, but it was not"
  }.merge(params)
  wait_for(params) do
    is_element_present(locator)
  end
end

#wait_for_page_to_load(timeout = default_timeout) ⇒ Object



158
159
160
161
162
163
# File 'lib/seleniumrc/selenium_driver.rb', line 158

def wait_for_page_to_load(timeout=default_timeout)
  super
  if get_title.include?("Exception caught")
    flunk "We got a new page, but it was an application exception page.\n\n" + get_html_source
  end
end