使用RSpec和Capybara检查图像和图标的存在
有没有一种很好的方法来检查使用rspec和水豚的图像和favicons的存在?
我可以查看favicon和图像的DOM,但我希望能够检查这些图像是否也加载。 这对rspec和水豚有可能吗?
describe "check images and favicon" do before { visit "url/to/check") it "should have the images" do page.should have_css('img', text: "image1.jpg") it "should have the favicon" do page.should have_xpath("/html/head/link[@href='favicon.ico']" end end
# frozen_string_literal: true module Capybara module CustomMatchers include Capybara::DSL class Asset def asset_exists?(actual, src) js_script = <
查看https://gist.github.com/yovasx2/1c767114f2e003474a546c89ab4f90db明星下载
问题是要确定是否存在实际的img和favicon。 以下是检查滑块的所有图像是否存在的代码。
page.all('#carousel img').each do |img| visit img[:src] page.status_code.should be 200 end
对于使用id myimage的个人图像
visit page.find('img#myimage')[:src] page.status_code.should be 200
最简单的方法是运行以下内容
page.all('link[rel~="icon"]', visible: :any).each do |fav| visit fav[:href] page.status_code.should be 200 end