6

Cucumberでは、次のようなステップを作成しようとしています。

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を持つ入力を見つけます。しかし、これまでCucumberで見たセレクターは、次のとおりです。

within("input:nth-child(#{pos.to_i}")

page.should have_content('foo')

Webrat / Capybaraセレクター構文を使用して解決策を提案できる人はいますか?

4

2 に答える 2

12

理解した

を使用して、ラベルテキストで入力を見つけることができます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
于 2010-11-11T19:15:18.970 に答える
0

これも機能します。ここでの違いは、ラベルテキストの部分的な一致に基づいてフィールドを検索することです。私たちのフィールドの末尾には「:」が付いていますが、必ずしも正しいとは限らないため、ラベル値全体を一致させたくありませんでした...

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、cucumberを使用する最初の日であり、私は実際に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
于 2010-12-23T01:08:57.980 に答える