キュウリ:ラベルテキストxで入力を見つけますか?
-
08-10-2019 - |
質問
キュウリでは、このようなステップを作成しようとしています。
Then I should see "Example business name" in the "Business name" input
「ビジネス名」入力を「ラベルにテキスト「ビジネス名」の入力」として定義したいと思います。
これが私がこれまでにステップに載っているものです:
Then /^I should see "([^"]*)" in the "([^"]*)" input$/ do |content, labeltext|
# Not sure what to put here
end
jQueryでは、そのテキストのラベルを探し、その「for」属性を見て、そのIDで入力を見つけます。しかし、私がこれまでキュウリで見た唯一のセレクターはこれらです。
within("input:nth-child(#{pos.to_i}")
と
page.should have_content('foo')
Webrat / Capybaraセレクターの構文を使用してソリューションを提案する人はいますか?
解決
理解した
あなたはそのラベルテキストで入力を見つけることができます find_field(labeltext)
.
# Example:
# 'I should see "Howdy" in the "Greeting" input' ("Greeting" is the label text)
Then /^I should see "([^"]*)" in the "([^"]*)" input$/ do |content, labeltext|
find_field("#{labeltext}").value.should == content
end
他のヒント
これも機能します。ここでの違いは、ラベルテキストの部分的な一致に基づいてフィールドを見つけることです。私たちのフィールドにはそれらの終わりに「:」がありますが、それによって常に正しいとは限らないので、ラベル全体の値に合わせたくありませんでした...
When /^i enter "([^"]*)" in the "([^"]*)" field$/i do |value, fieldname|
@thefield = all("label").detect { |l| l.has_content?(fieldname) }
if @thefield.nil? then
raise Exception.new("Couldn't find field #{fieldname}")
end
fill_in @thefield[:for], :with=>value
end
私はまだこれをケースに鈍感にするために拡張したいと思っています。これはRSPEC、キュウリとの私の最初の日であり、私は本当にRubyを使用することはないので、「Rubyish/RSPECよりも少ない」コードを許してください。しかし、それはうまくいくようです。
アップデート
以下は、ケースの不感の一致を介して部分ラベルに基づいてフィールドを見つけることができます。これは私のニーズの魅力のように機能します。
When= /^i enter "([^"]*)" in the "([^"]*)" field$/i do |value, fieldname|
@thefield = all("label").detect { |l| (l.text =~ /#{fieldname}/i).nil? == false }
if @thefield.nil? then
raise Exception.new("Couldn't find field '#{fieldname}'")
end
fill_in @thefield[:for], :with=>value
end
所属していません StackOverflow