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

ただし、input_field の代わりに select_index オブジェクトを使用すると、次のようになります。

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

しかし、これは「undefined method `state_select_element.option(index, 1).select'」というエラーを出しています

これはどのように行うことができますか?

4

1 に答える 1

1

を使用する場合send、最初の引数は単一のメソッドである必要があります。を 3 つのメソッド呼び出しsendに分割しません。state_select_element.option(index, 1).select

最初のメソッド呼び出しのみが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

ただし、メソッドoptionselectメソッドは減価償却の警告を出します。エラーを防ぐために、おそらくメソッドを次のように書き直します。

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 に答える