Class: TerminusSpec::Platforms::SeleniumWebDriver::PlatformObject

Inherits:
Object
  • Object
show all
Defined in:
lib/terminus_spec/platform_selenium/platform_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ PlatformObject

Returns a new instance of PlatformObject.



9
10
11
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 9

def initialize(browser)
  @browser = browser
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



7
8
9
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 7

def browser
  @browser
end

Instance Method Details

#attach_to_window(identifier, &block) ⇒ Object

Platform method to attach to a displayed window. See TerminusSpec#attach_to_window



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 110

def attach_to_window(identifier, &block)
  value = identifier.values.first
  key = identifier.keys.first
  handles = @browser.window_handles
  handles.each do |handle|
    @browser.switch_to.window handle
    if (key == :title and value == @browser.title) or (key == :url and value == @browser.current_url)
      return @browser.switch_to.window handle, &block
    end
  end
end

#backObject

Platform method to go to the preceding page in history. See TerminusSpec#back



51
52
53
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 51

def back
  @browser.navigate.back
end

#clear_cookiesObject

Platform method to clear the cookies from the browser. See TerminusSpec#clear_cookies



63
64
65
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 63

def clear_cookies
  @browser.manage.delete_all_cookies
end

#click_button_for(locator) ⇒ Object

Platform method to click a button object. See TerminusSpec::Generators#button



174
175
176
177
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 174

def click_button_for(locator)
  key, value = get_platform_locator_for(locator, WebObjects::Button, 'input', :type => 'submit')
  @browser.find_element(key, value).click
end

Platform method to click a link object. See TerminusSpec::Generators#link



133
134
135
136
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 133

def click_link_for(locator)
  key, value = get_platform_locator_for(locator, WebObjects::Link, 'a')
  @browser.find_element(key, value).click
end

#current_urlObject

Platform method to get the current URL. See TerminusSpec#current_url



39
40
41
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 39

def current_url
  @browser.current_url
end

#forwardObject

Platform method to go to the succeeding page in history. See TerminusSpec#forward



57
58
59
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 57

def forward
  @browser.navigate.forward
end

#get_button_for(locator) ⇒ Object

Platform method to return a button object. Button objects are of type: TerminusSpec::WebObjects::Button See TerminusSpec::Generators#button



166
167
168
169
170
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 166

def get_button_for(locator)
  key, value = get_platform_locator_for(locator, WebObjects::Button, 'input', :type => 'submit')
  web_object = @browser.find_element(key, value)
  WebObjects::Button.new(web_object, :platform => :selenium_webdriver)
end

Platform method to return a link object. Link objects are of type: TerminusSpec::WebObjects::Link See TerminusSpec::Generators#link



125
126
127
128
129
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 125

def get_link_for(locator)
  key, value = get_platform_locator_for(locator, WebObjects::Link, 'a')
  web_object = @browser.find_element(key, value)
  WebObjects::Link.new(web_object, :platform => :selenium_webdriver)
end

#get_text_field_for(locator) ⇒ Object

Platform method to return a text field object. Text field objects are of type: TerminusSpec::WebObjects::TextField See TerminusSpec::Generators#text_field



141
142
143
144
145
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 141

def get_text_field_for(locator)
  key, value = get_platform_locator_for(locator, WebObjects::TextField, 'input', :type => 'text')
  web_object = @browser.find_element(key, value)
  WebObjects::TextField.new(web_object, :platform => :selenium_webdriver)
end

#get_text_field_value_for(locator) ⇒ Object

Platform method to get the value in a text field object. See TerminusSpec::Generators#text_field



149
150
151
152
153
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 149

def get_text_field_value_for(locator)
  key, value = get_platform_locator_for(locator, WebObjects::TextField, 'input', :type => 'text')
  value = @browser.find_element(key, value).attribute('value')
  value
end

#htmlObject

Platform method to retrieve the HTML markup for the current page. See TerminusSpec#html



33
34
35
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 33

def html
  @browser.page_source
end

Platform method to navigate to a specified URL. See TerminusSpec#navigate_to



15
16
17
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 15

def navigate_to(url)
  @browser.navigate.to url
end

#refreshObject

Platform method to refresh the page. See TerminusSpec#refresh



45
46
47
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 45

def refresh
  @browser.navigate.refresh
end

#save_screenshot(file_name) ⇒ Object

Platform method to save the current screenshot to a file. See TerminusSpec#save_screenshot



69
70
71
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 69

def save_screenshot(file_name)
  @browser.save_screenshot(file_name)
end

#set_text_field_value_for(locator, text) ⇒ Object

Platform method to set a value in a text field object. See TerminusSpec::Generators#text_field



157
158
159
160
161
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 157

def set_text_field_value_for(locator, text)
  key, value = get_platform_locator_for(locator, WebObjects::TextField, 'input', :type => 'text')
  @browser.find_element(key, value).clear
  @browser.find_element(key, value).send_keys(text)
end

#textObject

Platform method to retrieve text from the current page. See TerminusSpec#text



27
28
29
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 27

def text
  @browser.find_element(:tag_name, 'body').text
end

#titleObject

Platform method to retrieve the title for the current page. See TerminusSpec#title



21
22
23
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 21

def title
  @browser.title
end

#wait_until(timeout, message, &block) ⇒ Object

Platform method to wait for a condition on a page to be true. See TerminusSpec#wait_until



75
76
77
78
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 75

def wait_until(timeout, message, &block)
  wait = Selenium::WebDriver::Wait.new({:timeout=>timeout, :message=>message})
  wait.until(&block)
end

#will_alert(&block) ⇒ Object

Platform method to handle an alert popup message box. See TerminusSpec#alert



82
83
84
85
86
87
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 82

def will_alert(&block)
  @browser.execute_script "window.alert = function(msg) { window.__lastWatirAlert = msg; }"
  yield
  value = @browser.execute_script "return window.__lastWatirAlert"
  value
end

#will_confirm(response, &block) ⇒ Object

Platform method to handle an confirmation popup message box. See TerminusSpec#will_confirm



91
92
93
94
95
96
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 91

def will_confirm(response, &block)
  @browser.execute_script "window.confirm = function(msg) { window.__lastWatirConfirm=msg; return #{!!response} }"
  yield
  value = @browser.execute_script "return window.__lastWatirConfirm"
  value
end

#will_prompt(response, &block) ⇒ Object

Platform method to handle a prompt popup message box. See TerminusSpec#will_prompt



100
101
102
103
104
105
106
# File 'lib/terminus_spec/platform_selenium/platform_object.rb', line 100

def will_prompt(response, &block)
  @browser.execute_script "window.prompt = function(text, value) { window.__lastWatirPrompt = { message: text, default_value: value }; return #{response.to_json}; }"
  yield
  result = @browser.execute_script "return window.__lastWatirPrompt"
  result && result.dup.each_key { |k| result[k.to_sym] = result.delete(k) }
  result
end