Skip to content

Commit

Permalink
Merge pull request cheezy#457 from titusfortner/populate_to_h
Browse files Browse the repository at this point in the history
allow filling forms with any object that can be converted to a Hash
  • Loading branch information
cheezy authored Nov 21, 2018
2 parents 297e61c + dff3f23 commit 4c95cca
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/page-object/page_populator.rb
Original file line number Diff line number Diff line change
@@ -30,8 +30,8 @@ module PagePopulator
# false for a Checkbox or RadioButton.
#
def populate_page_with(data)
data.each do |key, value|
populate_section(key, value) if value.is_a?(Hash)
data.to_h.each do |key, value|
populate_section(key, value) if value.respond_to?(:to_h)
populate_value(self, key, value)
end
end
@@ -40,7 +40,7 @@ def populate_page_with(data)

def populate_section(section, data)
return unless self.respond_to? section
data.each do |key, value|
data.to_h.each do |key, value|
populate_value(self.send(section), key, value)
end
end
18 changes: 18 additions & 0 deletions spec/page-object/page_populator_spec.rb
Original file line number Diff line number Diff line change
@@ -24,6 +24,15 @@ class PageObjectTestPageObject
let(:browser) { mock_watir_browser }
let(:page_object) { PageObjectTestPageObject.new(browser) }

it "should accept any object that can be converted to a Hash" do
os = OpenStruct.new('tf' => 'value', 'sl' => 'value')
expect(page_object).to receive(:tf=).with('value')
expect(page_object).to receive(:sl=).with('value')

allow(page_object).to receive(:is_enabled?).and_return(true)
page_object.populate_page_with(os)
end

it "should set a value in a text field" do
expect(page_object).to receive(:tf=).with('value')
allow(page_object).to receive(:is_enabled?).and_return(true)
@@ -140,6 +149,15 @@ class PageObjectTestPageObject
page_object.populate_page_with('section' => {'stf' => 'value'})
end

it "populate a page section when the value responds to #to_h and it exists" do
os = OpenStruct.new('stf' => 'value', 'sl' => 'value')

expect(section).to receive(:stf=).with('value')
expect(section).to receive(:sl=).with('value')
expect(page_object).to receive(:is_enabled?).twice.and_return(true)
page_object.populate_page_with('section' => os)
end

it "should not set a value in a text field if it is not found on the page" do
expect(section).not_to receive(:text_field)
page_object.populate_page_with('section' => {'coffee' => 'value'})

0 comments on commit 4c95cca

Please sign in to comment.