Cucumber を使用して特定のページに 0、1、2、または 3 倍の写真 ('foo.png') があるかどうかをテストしたいと考えています。
カスタムステップはどのように記述すればよいですか?
ありがとう
Cucumber を使用して特定のページに 0、1、2、または 3 倍の写真 ('foo.png') があるかどうかをテストしたいと考えています。
カスタムステップはどのように記述すればよいですか?
ありがとう
カスタム rspec 期待マッチャーを使用するカスタム cucumber ステップを作成する必要があります。
sudo コードは次のようになります。
features/page.feature
Given I am on the images page
Then I should see 3 images
機能/step_definitions/page_steps.rb
このファイルは、nokogiri を使用して特定の名前のすべての画像を収集し、rspec を使用して予想を検証します。
Then /^I should see (.+) images$/ do |num_of_images|
html = Nokogiri::HTML(response.body)
tags = html.xpath('//img[@src="/public/images/foo.png"]')
tags.length.should eql(num_of_images)
end
これは、RspecでNokogiriを使用する方法を示す実際のRspecの例です
require 'nokogiri'
describe "ImageCount" do
it "should have 4 image" do
html = Nokogiri::HTML('<html><body><div id=""><img src="/public/images/foo.png"></div> <div id=""><img src="/public/images/foo.png"></div> <div id=""><img src="/public/images/foo.png"></div> <div id=""><img src="/public/images/foo.png"></div> </html></body>')
tags = html.xpath('//img[@src="/public/images/foo.png"]')
tags.length.should eql(4)
end
it "should have 3 image" do
html = Nokogiri::HTML('<html><body><div id=""><img src="/public/images/bar.png"></div> <div id=""><img src="/public/images/foo.png"></div> <div id=""><img src="/public/images/foo.png"></div> <div id=""><img src="/public/images/foo.png"></div> </html></body>')
tags = html.xpath('//img[@src="/public/images/foo.png"]')
tags.length.should eql(3)
end
it "should have 1 image" do
html = Nokogiri::HTML('<html><body><div id=""><img src="/public/images/bar.png"></div> <div id=""><img src="/public/images/aaa.png"></div> <div id=""><img src="/public/images/bbb.png"></div> <div id=""><img src="/public/images/foo.png"></div> </html></body>')
tags = html.xpath('//img[@src="/public/images/foo.png"]')
tags.length.should eql(1)
end
end
これは、カピバラを使用した別の方法です。
Then /^(?:|I )should see (\d+) images?$/ do |count|
all("img").length.should == count.to_i
end