Capybara: how to check the TEXT value of an element with xpath and css?
capybara click text
capybara have_content visible
capybara check element exists
capybara have_content wait
capybara print element html
Trying to make this test to fail without success.
I've got this HTML:
<div id="navigation"> <ul> <li> <a href="/url">TV</a>
And then I was trying to identify the text of the A element and make it fail for now.
I've used all the following expressions with Xpath but all of them keep passing even though I'm using a different text for the comparison :S
page.should have_xpath("//div[@id='navigation']//a", :content => 'Radio') page.should have_xpath("//div[@id='navigation']//a", :text => 'Radio') page.should have_xpath("//div[@id='navigation']//a[contains(string(),'Radio')]") page.should have_xpath("//div[@id='navigation']//a[contains(text(),'Radio')]") page.should have_xpath("//div[@id='navigation']//a[contains(.,'Radio')]")
Any idea how I could identify the text of an specific HTML element with capybara? and... is it possible to achieve the same with CSS?
As of RSpec 2.11 (July 2012), the preferred way is to use the
expect(page).to have_css("#navigation a", text: "Radio")
You can configure RSpec to only accept this new syntax to keep your codebase consistent.
Capybara: Find an element that contains a string, page.find('div', text: /Expected contents?/i). Note that if your CSS selector is as generic as div , you might get a lot more results than you expect. E.g. a <div Find element by text in Capybara. Think about a simple situation : You have an element but you’ve got not css selector , or any other type to identify that element , lucky you capybara user, you can find element by text in Capybara. Peace of cake , Capybara offers the :text option to go along with your selector:
It turns out that there was another element with the text 'Radio' in the 'navigation' DIV, so that was causing the test to fail.
For future reference, identifying the 1st element explicitly behaved as expected and fails the test:
page.first('div#navigation a').text.should == 'Radio') page.first('div#navigation a').text.should eq('Radio') page.first('div#navigation a').text.should match('Radio') page.first('div#navigation a').text.should be('Radio')
Class: Capybara::Node::Element, #set(value, **options) ⇒ Capybara::Node::Element. Set the value of the Retrieve the given CSS styles. #tag_name ⇒ String Returns: (String) —. The value of the attribute. [View source] [View on GitHub] (String) —. An XPath expression. When running capybara test, you got the page object. This you can use to check/uncheck any checkboxes. As @buruzaemon already mentioned: to find and check a checkbox by name, id, or label text.
Putting this here so I can find it next time I run into this. :P
My html looks like this:
<a href="https://foo.com" id="foo-link">@foo</a>
I want to find the text and url value and assert that they are correct. Using Capybary 2.16, Rails 5.1 and minitest here's how I did that:
assert_selector 'a#foo-link[href="https://foo.com"]', text: '@foo'
If the ID, text or href value change the test will fail.
Capybara cheatsheet · GitHub, Note: find will wait for an element to appear on the page, :text => /re[dab]i/i, :count => 1) # can take both xpath and css as input and can :value => "Capybara cheatsheet") # True if there are 3 input tags in is this best practice of anyone know of any other way to check warnings using scenario outline? . refers to the entire text content of the element and it's children. //example[. = 'Hello, I am an example .'] To see the multiple text nodes, you can use: //example//text() which will return: "Hello, "" I am an example ""." And to more clearly see the entire text content of an element, one can use the string function: string(//example) or just
Maybe you can use this:
expect(page).to have_xpath("//*[@href='']", text: 'TV')
Unable to fill_in a visible field · Issue #1914 · teamcapybara , Meta Capybara Version: 2.15.1 Driver Information (and browser if In chrome driver, I can see the input is visible and clickable: fill_in doesn't take a CSS selector to locate the Element by, it takes name,id, or label text of a field. https://github.com/teamcapybara/capybara#beware-the-xpath--trap - but Find a button on the page. This can be any <input> element of type submit, reset, image, button or it can be a <button> element. All buttons can be found by their id, name, test_id attribute, value, or title. <button> elements can also be found by their text content, and image <input> elements by their alt attribute.
It'll help you:
page.should have_css('div#navigation a', :text == 'Radio')
Lessons learned from using Capybara for feature testing , For Rails, that means utilizing the Capybara gem for testing features You can use the hash symbol syntax to return the full value of any attributes from a captured element. element you want to write expectations against you can use xpath to Remove css animations for less flakes and a faster test suite. CSS selectors only work on elements, not on the text nodes they contain. There used to be a :contains() pseudo-class for this in a CSS 3 draft, but it’s gone . So if you feel you need to match text, you’ll have to use XPath instead (or put more classes and ids on your elements, so you can match against those).
Ruby Capybara with selenium Cheat Sheat, Capybara” is a library to helps you test web applications. Click button by id, text, title or alt of img tag find(:css, 'css selector', options) find(:xpath, 'xpath value', option) Check existance of the element by xpath If you have required capybara/rails, Capybara.save_path will default to tmp/capybara. Matching. It is possible to customize how Capybara finds elements. At your disposal are two options, Capybara.exact and Capybara.match. Exactness. Capybara.exact and the exact option work together with the is expression inside the XPath gem.
Capybara cheatsheet, The one-page guide to Capybara: usage, examples, links, snippets, and more. check 'A checkbox' uncheck 'A checkbox' choose 'A radio The selector arguments can be text, CSS selector, or XPath expression. '#field' have_unchecked_field have_css '.class' have_field '#field' have_table '#table' have_xpath '//div' Capybara. Note You are viewing the README for the 3.33.x release of Capybara. Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in. WebKit is supported through an external gem. Support Capybara
Find element by text in Capybara, Think about a simple situation : You have an element but you've got not css selector , or any other type to identify that element , lucky you How to access direct child elements using xpath. A child in XPATH is represented with a "/". Example XPATH for child elements : //div/a. How to access Child elements using css selectors. In CSS the child is indicated with a ">". Css examples of a link inside of a div tag can be identified as div > a
- Haha, unhandled exception. Fixed!
- You could improve your answer by adding an explaination and how it differs/is better than the other existing answers
- My thought was that it is more strict to use xpath blend with text in order to select and check elements because probably you'll have multiple elements with the same class. There is also no answer that uses xpath above.
- I think there's a typo.
:text == 'Radio'should be
:text => 'Radio'