如何使用 Cucumber 测试页面中的一张、多张或无图像?

发布于 2024-09-10 14:36:00 字数 90 浏览 7 评论 0原文

我想用 Cucumber 测试某个页面中是否有 0、1、2 或 3 次的图片('foo.png')。

我应该如何编写自定义步骤?

谢谢

I want to test if I have 0, 1, 2 or 3 times a pictures ('foo.png') in a certain page with Cucumber.

How should I write the custom step?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2024-09-17 14:36:01

这是使用水豚的另一种方式:

Then /^(?:|I )should see (\d+) images?$/ do |count|
  all("img").length.should == count.to_i
end

This is another way using capybara:

Then /^(?:|I )should see (\d+) images?$/ do |count|
  all("img").length.should == count.to_i
end
塔塔猫 2024-09-17 14:36:00

您需要编写一个使用自定义 rspec 期望匹配器的自定义 Cucumber 步骤。

sudo 代码看起来像这样。

features/page.feature

Given I am on the images page
Then I should see 3 images

features/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

You need to write a custom cucumber step that uses a custom rspec expectation matcher.

The sudo code would look something like this.

features/page.feature

Given I am on the images page
Then I should see 3 images

features/step_definitions/page_steps.rb

This file would use nokogiri to collect all the images with a given name and then use rspec to validate your expectation.

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

Here is a working Rspec example that shows how to use Nokogiri with 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文