我正在尝试在没有显式 height
和 width
属性的情况下测试 img 标签的尺寸,但我不确定如何准确执行此操作。
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
describe "gravatar image" do
it "should exist" do
expect(page.first(".gravatar")).not_to be nil
end
it "should be 50x50" do
gravatar = page.first(".gravatar")
expect(gravatar[:height]).to eq(50)
expect(gravatar[:width]).to eq(50)
end
end
end
存在性测试有效,但尺寸测试告诉我属性 height
和 width
是 nil
,我可以排序理解,因为我从未明确设置它们。但如何获得图像的实际高度/宽度?
最佳答案
您可以结合使用 evaluate_script
和客户端 JavaScript 来获取结果:
page.evaluate_script("$('img')[0].clientHeight")
这需要您的测试通过 JS 运行时(如poltergeist 等)执行。
关于ruby-on-rails - 使用 RSpec/Capybara 检查图像的尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20196146/