Check element visibility that is visible for less than a second in Rails Capybara

房东的猫 提交于 2021-02-19 08:02:16

问题


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?


回答1:


You can also try: assert find('#my_element_id').visible?

from: https://rubydoc.info/gems/capybara/0.4.0/Capybara/Element#visible%3F-instance_method

It does say however:

visible? ⇒ Boolean

Whether or not the element is visible. Not all drivers support CSS, so the result may be inaccurate.

I assume you are talking about whether #my_element_id is visible.

EDIT:

If you are waiting for an element to be visible first before checking for the element with the id #my_element_id this posts might be helpful: How to make Capybara check for visibility after some JS has run?

So you could wait for the backend data to come through then check visibility. If you are try to check that it is visible before that data, I am not quite sure, it seems like it would depend on the machine's internet connection to me.




回答2:


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



来源:https://stackoverflow.com/questions/59775329/check-element-visibility-that-is-visible-for-less-than-a-second-in-rails-capybar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!