質問

I am dealing with a problem and not able to find a smart solution. In brief: imagine we have many text field elements defined in page-object as:

 text_field(:txt_field_1, id: 'field1')
 text_field(:txt_field_2, id: 'field2')
 ....

I can easily fill one field with:

def fill_field1(content)
 self.txt_field_1 = content
end

...calling the method:

fill_field1('John Doe')

What i'm looking for is to use a unique method to fill several fields, using the page-object element name as method parameter, for example:

def fill_fields(field, content)
 self.field = content
end

...and calling the method this way:

fill_fields('txt_field_1', 'John Doe')

The above example fails with an "undefined method" error. I tried to use the send() method like self.send('field') = 'John Doe' unsuccessfully. I'm pretty sure there is a way to accomplish that... can you please shed some light on the issue?

役に立ちましたか?

解決

Based on the example, you could use the page-object gem's built in populate_page_with method. Assuming the text fields are defined in the page object already, then you can do:

example_page.populate_page_with :txt_field_1 => 'John Doe'

Or do multiple fields like:

example_page.populate_page_with :txt_field_1 => 'John Doe', :txt_field_2 => 'Jane Doe'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top