Class: Selenium::WebPage

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/web_page.rb

Overview

Class that models a web page with a title

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser, expected_title = nil) ⇒ WebPage

Returns a new instance of WebPage.



18
19
20
21
22
23
# File 'lib/selenium/web_page.rb', line 18

def initialize(browser, expected_title = nil)
  @browser = browser
  @expected_title = expected_title
  @timeout = 60
  @interval_millis = 100
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



15
16
17
# File 'lib/selenium/web_page.rb', line 15

def browser
  @browser
end

#interval_millisObject

Returns the value of attribute interval_millis.



16
17
18
# File 'lib/selenium/web_page.rb', line 16

def interval_millis
  @interval_millis
end

#timeoutObject

Returns the value of attribute timeout.



16
17
18
# File 'lib/selenium/web_page.rb', line 16

def timeout
  @timeout
end

Instance Method Details

#alertObject



62
63
64
# File 'lib/selenium/web_page.rb', line 62

def alert
  Alert.new(self)
end

#alert_messageObject



160
161
162
# File 'lib/selenium/web_page.rb', line 160

def alert_message
  @browser.get_alert
end

#alert_present?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/selenium/web_page.rb', line 134

def alert_present?
  @browser.is_alert_present
end

#button(how, what = nil) ⇒ Object



122
123
124
# File 'lib/selenium/web_page.rb', line 122

def button(how, what=nil)
  Button.new(self, element_locator(how, what))
end

#capture_page(file) ⇒ Object



201
202
203
# File 'lib/selenium/web_page.rb', line 201

def capture_page(file)
  @browser.capture_entire_page_screenshot(file)
end

#capture_screen(file) ⇒ Object



205
206
207
# File 'lib/selenium/web_page.rb', line 205

def capture_screen(file)
  @browser.capture_screenshot(file)
end

#click(locator) ⇒ Object



114
115
116
# File 'lib/selenium/web_page.rb', line 114

def click(locator)
  @browser.click(locator)
end

#click_wait(locator) ⇒ Object



146
147
148
149
# File 'lib/selenium/web_page.rb', line 146

def click_wait(locator)
  click(locator)
  wait_for_load
end

#closeObject



110
111
112
# File 'lib/selenium/web_page.rb', line 110

def close
  @browser.stop
end

#context_menu(locator) ⇒ Object



164
165
166
# File 'lib/selenium/web_page.rb', line 164

def context_menu(locator)
  @browser.context_menu(locator)
end

#double_click(locator) ⇒ Object



142
143
144
# File 'lib/selenium/web_page.rb', line 142

def double_click(locator)
  @browser.double_click(locator)
end

#element(how, what = nil) ⇒ Object



85
86
87
# File 'lib/selenium/web_page.rb', line 85

def element(how, what=nil)
  HtmlElement.new(self, element_locator(how,what))
end

#element_locator(how, what = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/selenium/web_page.rb', line 89

def element_locator(how, what=nil)
  locator = how
  if (not what.nil?)
    if (how == :xpath or how == :name or how == :id)
      locator = "#{how}=#{what}"
    else
      raise "couldn't understand how to build locator using #{how} with #{what}"
    end
  end
  locator
end

#element_present?(locator) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/selenium/web_page.rb', line 130

def element_present?(locator)
  @browser.is_element_present(locator)
end

#ensure_presentObject



34
35
36
# File 'lib/selenium/web_page.rb', line 34

def ensure_present
  title.should == @expected_title
end

#enter(locator, text) ⇒ Object

enter the text to the element found by locator



152
153
154
# File 'lib/selenium/web_page.rb', line 152

def enter(locator, text)
  @browser.type(locator, text)
end

#file_upload(how, what) ⇒ Object



101
102
103
# File 'lib/selenium/web_page.rb', line 101

def file_upload(how, what)
  FileUpload.new(self, element_locator(how, what))
end

#fire_event(locator, event) ⇒ Object



168
169
170
# File 'lib/selenium/web_page.rb', line 168

def fire_event(locator, event)
  @browser.fire_event(locator, event)
end

#focus(locator) ⇒ Object



188
189
190
# File 'lib/selenium/web_page.rb', line 188

def focus(locator)
  @browser.focus(locator)
end

#htmlObject



38
39
40
# File 'lib/selenium/web_page.rb', line 38

def html
  @browser.get_html_source
end

#key(key) ⇒ Object



126
127
128
# File 'lib/selenium/web_page.rb', line 126

def key(key)
  Key.new(self, key)
end

#key_down(key) ⇒ Object

Raises:



172
173
174
175
176
# File 'lib/selenium/web_page.rb', line 172

def key_down(key)
  message = "#{key}_key_down"
  raise NoKeyError.new(key.to_s) unless @browser.respond_to? message
  @browser.send message
end

#key_press(locator, key) ⇒ Object



184
185
186
# File 'lib/selenium/web_page.rb', line 184

def key_press(locator, key)
  @browser.key_press(locator, key)
end

#key_up(key) ⇒ Object

Raises:



178
179
180
181
182
# File 'lib/selenium/web_page.rb', line 178

def key_up(key)
  message = "#{key}_key_up"
  raise NoKeyError.new(key.to_s) unless @browser.respond_to? message
  @browser.send message
end


66
67
68
69
70
71
72
73
74
75
# File 'lib/selenium/web_page.rb', line 66

def link(how, what=nil)
  if (how == :text)
    locator = "link=#{what}"
  elsif (how == :href)
    locator = "xpath=//a[@href='#{what}']"
  else
    locator = element_locator(how, what)
  end
  Link.new(self, locator)
end

#open_page(url) ⇒ Object



105
106
107
108
# File 'lib/selenium/web_page.rb', line 105

def open_page(url)
  @browser.open(url)
  wait_for_load
end

#present?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/selenium/web_page.rb', line 30

def present?
  title == @expected_title
end

#speedObject



46
47
48
# File 'lib/selenium/web_page.rb', line 46

def speed
  @browser.get_speed.to_i
end

#speed=(value) ⇒ Object



42
43
44
# File 'lib/selenium/web_page.rb', line 42

def speed=(value)
  @browser.set_speed(value)
end

#text(locator) ⇒ Object



118
119
120
# File 'lib/selenium/web_page.rb', line 118

def text(locator)
  @browser.get_text(locator)
end

#text_area(how, what = nil) ⇒ Object



81
82
83
# File 'lib/selenium/web_page.rb', line 81

def text_area(how, what=nil)
  TextArea.new(self, element_locator(how, what))
end

#text_field(how, what = nil) ⇒ Object



77
78
79
# File 'lib/selenium/web_page.rb', line 77

def text_field(how, what=nil)
  TextField.new(self, element_locator(how, what))
end

#text_present?(text) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/selenium/web_page.rb', line 138

def text_present?(text)
  @browser.is_text_present(text)
end

#titleObject



25
26
27
28
# File 'lib/selenium/web_page.rb', line 25

def title
  raise 'nil as browser' unless @browser
  @browser.get_title
end

#to_sObject



209
210
211
# File 'lib/selenium/web_page.rb', line 209

def to_s
  "#{self.class}('#{@expected_title}') - #{browser.to_s}"
end

#upload(locator, file) ⇒ Object



192
193
194
195
196
197
198
199
# File 'lib/selenium/web_page.rb', line 192

def upload(locator, file)
#      @browser.attach_file(locator, file)
  @browser.click(locator)
  require 'auto_it'
  autoit = AutoIt.load
  window = AutoItWindow.wait_for(autoit, 'File Upload')
  puts window
end

#value(locator) ⇒ Object



156
157
158
# File 'lib/selenium/web_page.rb', line 156

def value(locator)
  @browser.get_value(locator)
end

#wait_for_loadObject



50
51
52
# File 'lib/selenium/web_page.rb', line 50

def wait_for_load
  @browser.wait_for_page_to_load
end

#wait_until(message, &block) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/selenium/web_page.rb', line 54

def wait_until(message, &block)
  Timeout::timeout(timeout, "Timeout waiting for : #{message}") do
    while(not yield message)
      sleep interval_millis / 1000.0
    end
  end
end