1

我想动态使用页面对象。像这样的东西:

text_field(:company_name_field, id: 'company_directory_name')
select_list(:state_select, id: 'company_directory_workflow_state')


def input_text_field (page_object)
    sample_text = Faker::Lorem.paragraph
    $text_array.push(sample_text)
    wait_until{send("#{page_object}_field?")}
    send("#{page_object}_field=", sample_text)
end

但是,使用 select_index 对象,而不是 input_field:

def input_select_list(page_object)
    wait_until{send("#{page_object}_select?")}
    x = rand(0..send("#{page_object}_select_element.options.length"))
    send("#{page_object}_select_element.option(#{:index}, #{x})).select")
end

但这给了我“未定义的方法`state_select_element.option(index,1).select'”的错误

如何才能做到这一点?

4

1 回答 1

1

使用时send,第一个参数必须是单个方法。send不会分解state_select_element.option(index, 1).select成 3 个方法调用。

由于只需state_select_element要从字符串中评估第一个方法调用,因此只需使用send它即可。其余的可以称为正常。将此应用于您的方法给出:

def input_select_list(page_object)
    wait_until{send("#{page_object}?")}
    x = rand(0..send("#{page_object}_element").options.length) - 1
    send("#{page_object}_element").option(:index, x).select
end

但是,optionandselect方法会给出贬值警告。为了防止错误,我可能会将方法重写为:

def input_select_list(page_object)
    select = send("#{page_object}_element")
    select.when_present
    select.select(send("#{page_object}_options").sample)
end
于 2014-02-11T17:22:25.253 回答