4

如何等待使用 Capybara 加载图像?这是我的应用程序代码:

-- <img src="data:image/jpeg.*" class="some class" alt="Image">

我尝试过wait_for使用这些classimg属性的方法,但没有运气。它从不等待该图像加载。

4

1 回答 1

2

img 元素有一个完整的属性,指示图像是否已加载。因此,类似于

start = Time.now
while true
  break if find('img.some_class')['complete']
  if Time.now > start + 5.seconds
    fail "Image still not loaded"
  end
  sleep 0.1
end

假设 img 元素已经在页面上,最多将等待 5 秒以完成图像加载。

这也可以实现为自定义选择器,其内容类似于

Capybara.add_selector(:loaded_image) do
  css { |locator_class| "img.#{locator_class}" }
  filter(:loaded, default: true, boolean: true) { |node, value| not(value ^ node['complete']) }
end

然后可以称为

find(:loaded_image, 'some_class')

*** 注意:我还没有实际尝试过自定义选择器代码,但它应该足够接近以提供指导

于 2015-12-22T22:20:28.157 回答