module PageObject::PagePopulator

Public Instance Methods

populate_page_with(data) click to toggle source

This method will populate all matched page TextFields, TextAreas, SelectLists, FileFields, Checkboxes, and Radio Buttons from the Hash passed as an argument. The way it find an element is by matching the Hash key to the name you provided when declaring the element on your page.

Checkboxe and Radio Button values must be true or false.

@example

class ExamplePage
  include PageObject

  text_field(:username, :id => 'username_id')
  checkbox(:active, :id => 'active_id')
end

...

@browser = Watir::Browser.new :firefox
example_page = ExamplePage.new(@browser)
example_page.populate_page_with :username => 'a name', :active => true

@param [Hash] the data to use to populate this page. The key can be either a string or a symbol. The value must be a string for TextField, TextArea, SelectList, and FileField and must be true or false for a Checkbox or RadioButton.

# File lib/page-object/page_populator.rb, line 32
def populate_page_with(data)
  data.each do |key, value|
    populate_checkbox(key, value) if is_checkbox?(key) and is_enabled?(key)
    populate_radiobuttongroup(key, value) if is_radiobuttongroup?(key)
    populate_radiobutton(key, value) if is_radiobutton?(key) and is_enabled?(key)
    populate_text(key, value) if is_text?(key) and is_enabled?(key)
  end
end

Private Instance Methods

is_checkbox?(key) click to toggle source
# File lib/page-object/page_populator.rb, line 64
def is_checkbox?(key)
  respond_to?("check_#{key}".to_sym)
end
is_enabled?(key) click to toggle source
# File lib/page-object/page_populator.rb, line 76
def is_enabled?(key)
  return false if is_radiobuttongroup?(key)
  return true if (self.send "#{key}_element").tag_name == "textarea"
  element = self.send("#{key}_element")
  element.enabled? and element.visible?
end
is_radiobutton?(key) click to toggle source
# File lib/page-object/page_populator.rb, line 68
def is_radiobutton?(key)
  respond_to?("select_#{key}".to_sym)
end
is_radiobuttongroup?(key) click to toggle source
# File lib/page-object/page_populator.rb, line 72
def is_radiobuttongroup?(key)
  respond_to?("select_#{key}".to_sym) and respond_to?("#{key}_values")
end
is_text?(key) click to toggle source
# File lib/page-object/page_populator.rb, line 60
def is_text?(key)
  respond_to?("#{key}=".to_sym)
end
populate_checkbox(key, value) click to toggle source
# File lib/page-object/page_populator.rb, line 47
def populate_checkbox(key, value)
  return self.send "check_#{key}" if value
  return self.send "uncheck_#{key}"
end
populate_radiobutton(key, value) click to toggle source
# File lib/page-object/page_populator.rb, line 52
def populate_radiobutton(key, value)
  return self.send "select_#{key}" if value
end
populate_radiobuttongroup(key, value) click to toggle source
# File lib/page-object/page_populator.rb, line 56
def populate_radiobuttongroup(key, value)
  return self.send("select_#{key}", value)
end
populate_text(key, value) click to toggle source
# File lib/page-object/page_populator.rb, line 43
def populate_text(key, value)
  self.send "#{key}=", value
end