在运行某些JS后,如何使Capybara检查可见性? [英] How to make Capybara check for visibility after some JS has run?

查看:36
本文介绍了在运行某些JS后,如何使Capybara检查可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载页面后,我将根据xhr返回的数据运行,隐藏和显示各种项目的代码。

After loading a page I have code that runs and hides and shows various items based on data returned by an xhr.

我的集成测试如下所示:

My integration test looks something like this:

it "should not show the blah" do
    page.find('#blah').visible?.should be_true
end 

在此测试运行的上下文中我手动转到该页面时,没想到,不可见。我怀疑Capybara正在查看页面的初始状态(在这种情况下不可见),评估DOM的状态并在JS运行之前未通过测试。

When I manually go to the page in the context this test runs, #blah is not visible as I expect. I suspect that Capybara is looking at the initial state of the page (invisible in this case), evaluating the state of the DOM and failing the test before the JS runs.

是的,我设置了:js => true 包含在describe块中:)

Yes, I set the :js => true on the containing describe block :)

任何想法都将不胜感激!我希望我不必在这里故意拖延,那会感觉很松软并且会使速度变慢。

Any ideas would be greatly appreciated! I'm hoping I don't have to put an intentional delay in here, that feels flaky and will slow things down.

推荐答案

我认为这里的 find 语句是带有隐式等待的语句,因此Capybara将等待直到元素在页面上,但不会等待它变为

I think that the find statement here is the one with the implicit wait, so Capybara will wait until the element is on the page, but won't wait for it to become visible.

在这里,您希望Capybara等待可见元素出现,这可以通过指定 visible 选项:

Here, you would want Capybara to wait for the visible element to appear, which should be achievable by specifying the visible option:

expect(page).to have_selector('#blah', visible: true)

我没有尝试过,但是 ignore_hidden_​​elements 配置选项如果您想 find 总是等待可见的元素,在这里也可能有用。

I haven't tried it, but the ignore_hidden_elements configuration option might be useful here as well, if you wanted find to always wait for visible elements.

这篇关于在运行某些JS后,如何使Capybara检查可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆