如何等待下一页加载 Capybara 和 Selenium?
How to wait for next page being loaded with Capybara and Selenium?
假设您要提交一个表单来创建 post,等待它被创建,从数据库中获取 post 并检查图像是否来自 post正在下一页显示:
visit url
fill_in the_form
click_on 'Create'
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path
但这样一来,post
将经常 nil
。因为当您从数据库中获取它时,它可能尚未创建。如何确保加载下一页?
很遗憾,您不想等待页面重新加载。因为,例如,现在 post 在页面重新加载后出现。一段时间后,它可能会在没有重新加载整页的情况下出现。如果你这样做,请说明你的情况。
我很遗憾地说,因为如果我们忽略上面所说的内容,如果我们真的因为某种原因需要等待页面重新加载,旧答案的想法比我在互联网上看到的任何其他想法都要好。
那么,您关心的是 post 已经出现在页面上了。这给我们带来了以下代码:
test 'create post' do
visit url
fill_in the_form
click_on 'Create'
assert_selector '.post'
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path
end
旧答案
受这些 great article, link and comment. According to one of capybara
's authors 的启发,这是他在 Capybara 中听说过的 wait_until
唯一合法的用例。断言模型对象,即。
def wait_until
Timeout.timeout(Capybara.default_max_wait_time) do
sleep(0.1) until value = yield
value
end
end
def wait_for_page_reload
id = find('html').native.ref
yield
wait_until { find('html').native.ref != id }
end
test 'create post' do
visit url
fill_in the_form
wait_for_page_load do
click_on 'Create'
end
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path
end
这有点老套。但它有效。它一直等到 html
元素的内部 ID 发生变化。
find
最多会等待 Capybara.default_max_wait_time
秒,以便匹配的元素出现在屏幕上。因此,要按照您的要求进行操作,请在从 DB
加载项目之前检查您希望出现在下一页上的内容
visit url
fill_in the_form
click_on 'Create'
img = page.find '.post .image'
post = Post.first
assert_equal post.file.thumb.url, URI(img[:src]).path
如果按此顺序完成,page.find
将等待元素出现在页面上,以确保 Post 已保存,以便您随后可以加载它。
假设您要提交一个表单来创建 post,等待它被创建,从数据库中获取 post 并检查图像是否来自 post正在下一页显示:
visit url
fill_in the_form
click_on 'Create'
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path
但这样一来,post
将经常 nil
。因为当您从数据库中获取它时,它可能尚未创建。如何确保加载下一页?
很遗憾,您不想等待页面重新加载。因为,例如,现在 post 在页面重新加载后出现。一段时间后,它可能会在没有重新加载整页的情况下出现。如果你这样做,请说明你的情况。
我很遗憾地说,因为如果我们忽略上面所说的内容,如果我们真的因为某种原因需要等待页面重新加载,旧答案的想法比我在互联网上看到的任何其他想法都要好。
那么,您关心的是 post 已经出现在页面上了。这给我们带来了以下代码:
test 'create post' do
visit url
fill_in the_form
click_on 'Create'
assert_selector '.post'
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path
end
旧答案
受这些 great article, link and comment. According to one of capybara
's authors 的启发,这是他在 Capybara 中听说过的 wait_until
唯一合法的用例。断言模型对象,即。
def wait_until
Timeout.timeout(Capybara.default_max_wait_time) do
sleep(0.1) until value = yield
value
end
end
def wait_for_page_reload
id = find('html').native.ref
yield
wait_until { find('html').native.ref != id }
end
test 'create post' do
visit url
fill_in the_form
wait_for_page_load do
click_on 'Create'
end
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path
end
这有点老套。但它有效。它一直等到 html
元素的内部 ID 发生变化。
find
最多会等待 Capybara.default_max_wait_time
秒,以便匹配的元素出现在屏幕上。因此,要按照您的要求进行操作,请在从 DB
visit url
fill_in the_form
click_on 'Create'
img = page.find '.post .image'
post = Post.first
assert_equal post.file.thumb.url, URI(img[:src]).path
如果按此顺序完成,page.find
将等待元素出现在页面上,以确保 Post 已保存,以便您随后可以加载它。