Module: Druid

Includes:
Assist, ElementLocators, PagePopulator
Defined in:
lib/druid.rb,
lib/druid/assist.rb,
lib/druid/elements.rb,
lib/druid/accessors.rb,
lib/druid/elements/div.rb,
lib/druid/page_factory.rb,
lib/druid/elements/form.rb,
lib/druid/elements/link.rb,
lib/druid/elements/span.rb,
lib/druid/elements/image.rb,
lib/druid/elements/table.rb,
lib/druid/page_populator.rb,
lib/druid/elements/button.rb,
lib/druid/elements/option.rb,
lib/druid/nested_elements.rb,
lib/druid/element_locators.rb,
lib/druid/elements/element.rb,
lib/druid/elements/heading.rb,
lib/druid/elements/check_box.rb,
lib/druid/elements/list_item.rb,
lib/druid/elements/paragraph.rb,
lib/druid/elements/table_row.rb,
lib/druid/elements/text_area.rb,
lib/druid/elements/file_field.rb,
lib/druid/elements/table_cell.rb,
lib/druid/elements/text_field.rb,
lib/druid/elements/select_list.rb,
lib/druid/elements/hidden_field.rb,
lib/druid/elements/ordered_list.rb,
lib/druid/elements/radio_button.rb,
lib/druid/elements/unordered_list.rb

Overview

set of methods that provide access to the elements on the web pages.

Defined Under Namespace

Modules: Accessors, Assist, ElementLocators, Elements, NestedElements, PageFactory, PagePopulator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PagePopulator

#populate_page_with

Methods included from ElementLocators

#button_element, #cell_element, #checkbox_element, #div_element, #file_field_element, #form_element, #h1_element, #h2_element, #h3_element, #h4_element, #h5_element, #h6_element, #hidden_field_element, #image_element, #link_element, #list_item_element, #ordered_list_element, #paragraph_element, #radio_button_element, #select_list_element, #span_element, #table_element, #text_area_element, #text_field_element, #unordered_list_element

Methods included from Assist

#button_for, #cell_for, #cell_text_for, #check_checkbox, #checkbox_checked?, #checkbox_for, #clear_radio, #click_button_for, #click_link_for, #div_for, #div_text_for, #file_field_for, #file_field_value_set, #form_for, #h1_for, #h1_text_for, #h2_for, #h2_text_for, #h3_for, #h3_text_for, #h4_for, #h4_text_for, #h5_for, #h5_text_for, #h6_for, #h6_text_for, #hidden_field_for, #hidden_field_value_for, #image_for, #link_for, #list_item_for, #list_item_text_for, #ordered_list_for, #paragraph_for, #paragraph_text_for, #radio_button_for, #radio_selected?, #select_list_for, #select_list_value_for, #select_list_value_set, #select_radio, #span_for, #span_text_for, #table_for, #text_area_for, #text_area_value_for, #text_area_value_set, #text_field_for, #text_field_value_for, #text_field_value_set, #uncheck_checkbox, #unordered_list_for

Instance Attribute Details

#driverWatir::Browser (readonly)

include Watir::AlertHelper

Returns:

  • (Watir::Browser)

    the drvier passed to the constructor



43
44
45
# File 'lib/druid.rb', line 43

def driver
  @driver
end

Class Method Details

.included(cls) ⇒ Object



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

def self.included(cls)
  cls.extend Druid::Accessors
end

Instance Method Details

#alert(&block) ⇒ String

Override the normal alert popup so it does not occurr.

Examples:

message = @page.alert do
  @page.button_that_causes_alert
end

Parameters:

  • block

    a block that has the call that will cause the alert to display

Returns:

  • (String)

    the message that was contained in the alert



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/druid.rb', line 150

def alert(&block)
  # switch_to_frame(frame)
  yield
  value = nil
  if driver.alert.exists?
    value = driver.alert.text
    driver.alert.ok
  end
  # switch_to_default_content(frame)
  value
end

#attach_to_window(identifier, &block) ⇒ Object

Attach to a running window. You can locate the window using either the window’s title or url or index, If it failes to connect to a window it will pause for 1 second and try again.

be the entire url - it can just be the page name like index.html

Examples:

@page.attach_to_window(:title => "other window's title")

Parameters:

  • either (Hash)

    :title or :url or index of the other window. The url does not need to



218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/druid.rb', line 218

def attach_to_window(identifier, &block)
  if identifier.keys.first == :url
    win_id = {identifier.keys.first => /#{Regexp.escape(identifier.values.first)}/}
  else
    win_id = {identifier.keys.first => identifier.values.first}
  end
  begin
    driver.window(win_id).use &block
  rescue
    sleep 1
    driver.window(win_id).use &block
  end
end

#backObject

Go back to the previous page



113
114
115
# File 'lib/druid.rb', line 113

def back
  driver.back
end

#call_block(&block) ⇒ Object

def switch_to_frame(frame_identifiers)

unless frame_identifiers.nil?
  frame_identifiers.each do |frame|
    frame_id = frame.values.first
    value = frame_id.values.first
    driver.wd.switch_to.frame(value)
  end
end

end



324
325
326
# File 'lib/druid.rb', line 324

def call_block(&block)
  block.arity == 1 ? block.call(self) : self.instance_eval(&block)
end

#clear_cookiesObject

Clear the cookies from the browser



259
260
261
# File 'lib/druid.rb', line 259

def clear_cookies
  driver.clear_cookies
end

#confirm(response, &block) ⇒ String

Override the normal confirm popup so it does not occurr

Examples:

message = @popup.confirm(true) do
  @page.button_that_causes_confirm
end

Parameters:

  • what (boolean)

    response you want to return back from the confirm popup

  • block

    a block that has the call that will cause the confirm to display

Returns:

  • (String)

    the message that was contained in the confirm



174
175
176
177
178
179
180
181
182
# File 'lib/druid.rb', line 174

def confirm(response, &block)
  yield
  value = nil
  if driver.alert.exists?
    value = driver.alert.text
    response ? driver.alert.ok : driver.alert.close
  end
  value
end

#current_urlObject

get the current page url



78
79
80
# File 'lib/druid.rb', line 78

def current_url
  driver.url
end

#forwardObject

Go forward to the next page



120
121
122
# File 'lib/druid.rb', line 120

def forward
  driver.forward
end

#htmlObject

Returns the html of the current page



92
93
94
# File 'lib/druid.rb', line 92

def html
  driver.html
end

#in_frame(identifier, frame = nil, &block) ⇒ Object

Identify an element as existing within a frame or iframe. A frame parameter is passed to the block and must be passed to the other calls to Druid. You can nest calls to in_frame by passing the frame to the next level.

Examples:

@page.in_frame(:id => 'frame_id') do |frame|
  @page.text_field_element(:id=> 'fname', :frame => frame)
end

Parameters:

  • identifier (Hash)

    how we find the frame. The valid keys are:

    • :id

    • :index

    • :name

  • block

    that contains the calls to elements that exist inside the frame.



286
287
288
289
290
# File 'lib/druid.rb', line 286

def in_frame(identifier, frame=nil, &block)
  frame = [] if frame.nil?
  frame << {frame: identifier}
  block.call(frame)
end

#in_iframe(identifier, frame = nil, &block) ⇒ Object

Identify an element as existing within a frame or iframe. A frame parameter is passed to the block and must be passed to the other calls to Druid. You can nest calls to in_frame by passing the frame to the next level.

Examples:

@page.in_iframe(:id => 'frame_id') do |frame|
  @page.text_field_element(:id=> 'fname', :frame => frame)
end

Parameters:

  • identifier (Hash)

    how we find the frame. The valid keys are:

    • :id

    • :index

    • :name

  • block

    that contains the calls to elements that exist inside the frame.



308
309
310
311
312
# File 'lib/druid.rb', line 308

def in_iframe(identifier, frame=nil, &block)
  frame = [] if frame.nil?
  frame << {iframe: identifier}
  block.call(frame)
end

#initialize(driver, visit = false) ⇒ Object

Construct a new druid. Upon initialization of the page it will call a method named initialize_page if it exists

Parameters:

  • the (Watir::Browser)

    driver to use

  • open (bool)

    the page if page_url is set



51
52
53
54
55
56
57
58
59
# File 'lib/druid.rb', line 51

def initialize(driver, visit=false)
  if driver.is_a? Watir::Browser
    @driver ||= driver
    initialize_page if respond_to?(:initialize_page)
    goto if visit && respond_to?(:goto)
  else
    raise ArgumentError, "expect Watir::Browser"
  end
end

Override the normal showModalDialog call is it opens a window instead of a dialog. You will need to attach to the new window in order to continue.

Examples:

@page.modal_dialog do
  @page.action_that_spawns_the_modal
end

Parameters:

  • block

    a block that contains the call that will cause the modal dialog.



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/druid.rb', line 243

def modal_dialog(&block)
  script =
  %Q{
    window.showModalDialog = function(sURL, vArguments, sFeatures) {
      window.dialogArguments = vArguments;
      modalWin = window.open(sURL, 'modal', sFeatures);
      return modalWin;
    }
  }
  driver.execute_script script
  yield if block_given?
end

navigate to the provided url

Parameters:

  • the (String)

    full url to navigate to



71
72
73
# File 'lib/druid.rb', line 71

def navigate_to url
  driver.goto url
end

#prompt(answer, &block) ⇒ String

Override the normal prompt popup so it does not occurr

Examples:

message = @popup.prompt("Some Value") do
  @page.button_that_causes_prompt
end

Parameters:

  • the (String)

    value will be setted in the prompt field

  • block

    a block that has the call that will cause the prompt to display

Returns:

  • (String)

    the message that was contained in the prompt



196
197
198
199
200
201
202
203
204
205
# File 'lib/druid.rb', line 196

def prompt(answer, &block)
  yield
  value = nil
  if driver.alert.exists?
    value = driver.alert.text
    driver.alert.set answer
    driver.alert.ok
  end
  value
end

#refreshObject

Refresh current page



106
107
108
# File 'lib/druid.rb', line 106

def refresh
  driver.refresh
end

#save_screenshot(file_name) ⇒ Object

Save the current screenshot to the provided path. File is saved as a png file.



266
267
268
# File 'lib/druid.rb', line 266

def save_screenshot(file_name)
  driver.screenshot.save(file_name)
end

#textObject

Returns the text of the current page



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

def text
  driver.text
end

#titleObject

Returns the title of the current page



99
100
101
# File 'lib/druid.rb', line 99

def title
  driver.title
end

#wait_until(timeout = 30, message = nil, &block) ⇒ Object

Wait until the block returns true or times out

Examples:

@page.wait_until(5, 'Success not found on page') do
  @page.text.include? 'Success'
end

Parameters:

  • the (Numeric)

    amount of time to wait for the block to return true

  • the (String)

    message to include with the error if we exceed the timeout duration

  • block

    the block to execute. It should return true when successful.



135
136
137
# File 'lib/druid.rb', line 135

def wait_until(timeout = 30, message = nil, &block)
  driver.wait_until(timeout, message, &block)
end