How can I check whether or not a data-testid exists on my page using Ruby ? I ask this because I see that there are some ways to search for selectors, but I don't know exactly if this is the correct way or not. I'm trying to do it like this
expect(page).should have_no_selector("[data-testid='prepay-enroll-now-button']");
expect(page).should have_selector("[data-testid='prepay-enroll-now-button']");
CodePudding user response:
What you are doing is valid for a CSS attribute selector, however you shouldn't be using should
with RSpecs expect syntax. Also, since have_no_selector
can be changed from the normal default of CSS to XPath (or any other supported selector type) I'd recommend using have_css
or have_no_css
when you are using CSS.
expect(page).not_to have_css("[data-testid='prepay-enroll-now-button']")
or
expect(page).to have_no_css("[data-testid='prepay-enroll-now-button']")
Also, if you are widely using data-testid
attributes you may want set Capybara.test_id = 'data-testid'
which will then let you use the data-testid as the locator in other Capybara methods. For instance if 'prepay-enroll-now-button' is an actual link or button (rather than just a div with behaviors attached) you could do things like
click_on('prepay-enroll-now-button')
or
expect(page).not_to have_selector(:link_or_button, 'prepay-enroll-now-button')