gpt4 book ai didi

ruby-on-rails - 如何使用 Cucumber 在页面中测试一个、多个或无图像?

转载 作者:数据小太阳 更新时间:2023-10-29 08:02:04 25 4
gpt4 key购买 nike

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

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

谢谢

最佳答案

您需要编写一个使用自定义 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

关于ruby-on-rails - 如何使用 Cucumber 在页面中测试一个、多个或无图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3289961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com