1

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?

Lax_Sam
  • 1,099
  • 2
  • 14
  • 32

2 Answers2

0

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.

ekr990011
  • 724
  • 2
  • 7
  • 23
  • Yes. I want to check the visiblity of the element. – Lax_Sam Jan 16 '20 at 18:08
  • Well i think either `assert find('#my_element_id').visible?` should work or from the post i linked: `expect(page).to have_selector('#blah', visible: true)` although it does seem an awful lot like yours but he does talk about the find method compared to have_selector. – ekr990011 Jan 16 '20 at 18:13
  • Ok. I will check with `visible?` – Lax_Sam Jan 16 '20 at 18:14
0

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

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • It is automated testcase. The testcase is run on different machines. Is there anyway to slow down the data from the code itself? – Lax_Sam Jan 17 '20 at 04:33
  • 1
    If using chrome via selenium you can modify the network_conditions, as mentioned in my answer - https://github.com/teamcapybara/capybara/issues/1990. If not using Chrome you could use a proxy, like puffing_billy, to delay responses selectively – Thomas Walpole Jan 17 '20 at 05:04
  • I would however just try the correct single assertion rather than the two confused assertions you had before. Also consider if it shows up and disappears so fast your test can’t detect it, why are you bothering to test it? – Thomas Walpole Jan 17 '20 at 05:07
  • I did not use both simultaneously. I used one assertion at a time. – Lax_Sam Jan 17 '20 at 05:11
  • @Lax_Sam ok- but you were using plain `assert` which you should never be using with Capybara elements on a page that has any dynamic behavior – Thomas Walpole Jan 18 '20 at 02:17