水豚 - 带有 `within_window` 的规范在没有 `sleep` 的情况下不起作用
Capybara - spec with `within_window` does not work without `sleep`
我有一个点击按钮的规范,它会触发一些 JS 生成 url 并在新的 window:
中打开它
new_window = window_opened_by { find('#search_postcode').click }
within_window new_window do
expect(page.current_url).to include('postcode.nl')
end
这不起作用。我收到以下错误:
Failure/Error: expect(page.current_url).to include('postcode.nl')
expected "about:blank" to include "postcode.nl"
但是,当我添加 sleep 1
时,规范通过了:
within_window new_window do
sleep 1
expect(page.current_url).to include('postcode.nl')
end
是否可以在没有 sleep
的情况下使它工作?
您可以在新 windows html 中检查选择器。 Capybara 然后应该等待 default_waittime 元素出现。喜欢:
within_window new_window do
expect(page).to have_selector('.your_class_here')
expect(page.current_url).to include('postcode.nl')
end
无需测试元素是否存在,您可以使用提供的 have_current_path
匹配器,其中包括 waiting/retrying 行为,通常(99.9% 的时间)比使用 RSpec 为文本匹配器提供了 current_url
的结果
within_window new_window do
expect(page).to have_current_path(/postcode\.nl/, url: true)
end
我有一个点击按钮的规范,它会触发一些 JS 生成 url 并在新的 window:
中打开它new_window = window_opened_by { find('#search_postcode').click }
within_window new_window do
expect(page.current_url).to include('postcode.nl')
end
这不起作用。我收到以下错误:
Failure/Error: expect(page.current_url).to include('postcode.nl')
expected "about:blank" to include "postcode.nl"
但是,当我添加 sleep 1
时,规范通过了:
within_window new_window do
sleep 1
expect(page.current_url).to include('postcode.nl')
end
是否可以在没有 sleep
的情况下使它工作?
您可以在新 windows html 中检查选择器。 Capybara 然后应该等待 default_waittime 元素出现。喜欢:
within_window new_window do
expect(page).to have_selector('.your_class_here')
expect(page.current_url).to include('postcode.nl')
end
无需测试元素是否存在,您可以使用提供的 have_current_path
匹配器,其中包括 waiting/retrying 行为,通常(99.9% 的时间)比使用 RSpec 为文本匹配器提供了 current_url
within_window new_window do
expect(page).to have_current_path(/postcode\.nl/, url: true)
end