考虑这个简单的页面对象:
require 'watir-webdriver'
require 'page-object'
class SomePage
include PageObject
text_field :first_name, :name => "fname"
text_field :last_name, :name => "lname"
text_field :birth_date, :name => "birthday"
button :submit, :type => "submit"
end
browser = Watir::Browser.new
page = SomePage.new(browser)
有没有办法迭代所有文本字段(或任何元素)以访问其“标识符”(即:用户名、:密码或:出生)?
类似于:
page.text_fields.each do |text_field|
puts text_field.identifier.inspect
end
=> :first_name
=> :last_name
=> :birth_date
我只是想看看是否可以改变这个:
page.first_name = @user.first_name
page.last_name = @user.last_name
etc...
进入此:
page.text_fields.each do |text_field|
attribute = text_field.attribute
text_field = @user[attribute]
end
有人知道我的意思吗?
最佳答案
已经提供的答案肯定会满足您的需求,但我想添加一个额外的选项,该选项使用页面对象 gem 中内置的功能。
调用page_populator.rb中的populate_page_with()。就这样使用了。
从具有元素名称的哈希开始(我使用加载到哈希中的 CSV 文件),如页面对象上的键中所定义。哈希值包含您希望填充每个元素的值。
form_data = {first_name: @user.first_name, last_name: @user.last_name, birth_date: @user.birth_date}
@page.populate_page_with form_data
就是这样。 populate_page 方法将在页面上找到正确的元素,并使用您在源哈希中设置的任何值填充它们。这适用于复选框、单选按钮和文本。
这是 Cheezy 为我们提供的一个非常好的节省时间的方法!
谢谢奇兹!
关于ruby - Cheezy的Page对象,获取Page内定义的所有text_fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27031650/