0

I was just starting with SitePrism and I've got a site where elements get added dynamically to the page and don't have any kind of id, name, etc... the easiest I could think of is finding them by their 'text', eg. a CONTINUE button

I was trying:

element :continue_b, :button, 'Continue'

and SitePrism fails with the following:

Capybara::Ambiguous: Ambiguous match, found 4 elements matching button "Continue"

Is there a way for me to specify with SitePrism the element I want to click? I've found a few ways to do it with Capybara itself but I didn't manage to see the equivalent with SitePrism.

mickael
  • 2,033
  • 7
  • 25
  • 38

1 Answers1

1

If there really is no difference between any of the button elements and you can't/don't want to move the element definition to a scoped section of the page, you can use the match argument to return just the first. Since all the parameters after the SitePrism element name are passed through to Capybara as find arguments it would be

element :continue_b, :button, 'Continue', match: :first
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • I just tried and got the error: 'Selenium::WebDriver::Error::ElementNotVisibleError: element not visible'. although element is visible! It seems that 3 of the 4 elements reported as ambiguous are hidden (the 1st being one of them). I would have expected SitePrism/Capybara to try and click the only visible element, but that doesn't seem to be the case. Is there a command or way to specify to just care about visible elements? The hidden class are behind: style="display: none;", I've tried: 'element :continue_b, :button, 'Continue', visible: :true' but that didn't work. – mickael Nov 29 '16 at 10:04
  • Strange, I get the ambiguous message by default, and then if I use ' Capybara.ignore_hidden_elements = true' I get this instead?! Capybara::ElementNotFound: Unable to find button "Continue" – mickael Nov 29 '16 at 11:30
  • @mickael First, using Capybara.ignore_hidden_elements = false is generally a bad idea when testing an app. Second, it sounds like all the buttons are actually hidden when you're calling `continue_b`. SitePrism disables Capybaras auto waiting behavior so it won't wait for elements to become visible. You can either reconfigure SitePrism to enable Capybaras implicit waits - https://github.com/natritmeyer/site_prism#siteprism-configuration - or you will need to call `wait_until_button_b_visible` - https://github.com/natritmeyer/site_prism#waiting-for-an-element-to-become-visible – Thomas Walpole Nov 29 '16 at 15:59
  • did you mean 'true' being a bad idea for 'Capybara.ignore_hidden_elements'? At the moment the previous tester set it up with 'false' in the env.rb file of the project, which I think it's the default anyway. I was thinking 'true' could be a good idea as I'm just dealing with elements visible in the page, aren't I? ** It doesn't seem a problem of wait. I've noticed that there's is a main DIV, then a lot of child DIVs with CONTINUE buttons. And the app works by adding 'style="display: none"' to other child DIVs, so that when you click CONTINUE, that DIV becomes hidden and next enabled. – mickael Nov 29 '16 at 22:38
  • @mickael No - I meant 'false' to be a bad idea - the default is `true` and should be what it's left at 99.9% of the time for app testing. The reason being that when testing an app you're trying to replicate a user and users can't see non-visible things. Having capybara not ignore hidden elements would bypass that completely. You can override the global setting for just your `element` call by passing `visible: true` option to the `element` method. Long term you'd be better off changing back to the default of true - however short term that may break a bunch of your tests. – Thomas Walpole Nov 29 '16 at 23:06
  • I think the creator mentioned that it was false by default: https://github.com/teamcapybara/capybara/issues/761 ("jnicklas: Oh, sorry. I was wrong. ignore_hidden_elements is actually false by default."). Although I tried today both ways and it was still failing, if I specify the parent DIV ID which is visible then it worked fine, but just trying to call the CONTINUE button it kept on failing. It's driving me crazy the way this page works. I just want to click a button inside a visible DIV (whilst there are other buttons with same value in DIVS with 'style="display: none"'. I have to retry – mickael Nov 29 '16 at 23:38
  • @mickael That changed in v2.1.0 https://github.com/teamcapybara/capybara/blob/master/History.md#version-210 because it makes more sense to be true for app testing. For any more help you'll need to add your test and the relevant section of html to your question. Remember that case of the text matters (which can be changed by CSS) when dealing with visible elements. – Thomas Walpole Nov 30 '16 at 00:04