检查在Rails Capybara中可见元素的可见性少于一秒钟 [英] Check element visibility that is visible for less than a second in Rails Capybara

查看:58
本文介绍了检查在Rails Capybara中可见元素的可见性少于一秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个集成测试,在该测试中,必须在单击按钮时检查元素的可见性.该代码在一台机器上完美运行,而在另一台机器上失败.一直显示该元素,直到数据来自后端为止.因此,它的可见性还取决于机器的速度.那是问题吗?这是代码:

  assert page.has_css?('#my_element_id')断言find('#my_element_id',可见:true) 

但是我遇到了一个错误:期望假为真.

还有其他断言元素可见性的方法吗?

解决方案

不要使用简单的 assert ,请使用Capybara提供的断言,包括重试行为

  assert_css('#my_element_id') 

默认情况下,仅检查可见元素,但是如果您设置了 Capybara.ignore_hidden_​​elements = false (不要这样做,真的不要),那么您还需要传递:可见选项

注意:如果只能在很短的时间内看到它,您可能仍然会遇到问题-在这种情况下,如果您使用的是Chrome,则可以将网络条件设置为非常慢,以增加返回数据所需的时间

I am trying to write an integration test in which I must check the visibility of an element on button click. The code works perfectly in one machine and failing in the other. The element is displayed until the data comes from the backend. So its visibility depends on the speed of the machine also. Is that the problem? This is the code:

assert page.has_css?('#my_element_id')

assert find('#my_element_id', visible: true)

But I am getting an error: expected false to be truthy.

Is there any other way to assert the visibility of the element?

解决方案

Don’t use plain assert, use the assertions provided by Capybara which include retrying behavior

assert_css(‘#my_element_id’)

By default that would check only for visible elements, but if you’ve set Capybara.ignore_hidden_elements = false (don’t do that, really dont) then you would need to also pass the :visible option

Note: you may still have issues if it’s only visible for a very short time - in that case if you’re using Chrome you can set the network conditions to very slow in order to increase the time data takes to return

这篇关于检查在Rails Capybara中可见元素的可见性少于一秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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