Reputation: 33
Trying to verify if an element contains a specific text, but the keyword Element Should Contain
does not find the text even if I explicitly put in the xpath attribute that it contains the text. However the keyword Wait Until Page Contains Element
works.
Ran the script below and got the following results
#this was successful
Wait until Page Contains Element xpath://div[@id="header"]//span[contains(text(), "text")]
#this resulted to: should have contained text 'text' but its text was ''
Element Should Contain xpath://div[@id="header"]//span[contains(text(), "text")] text
Upvotes: 3
Views: 18580
Reputation: 13
When trying to check for text if it's correct you can do something like this:
Run Keyword And Continue On Failure Element Text Should Be XPATH TEXT
You can also leave the first part behind if you want the test to stop if the text is not ok.
Element Text Should Be XPATH TEXT
Upvotes: 0
Reputation: 33
Was able to solve this problem. The script was actually correct, the only problem was that there was a similar xpath that also appears whenever I perform a 'Mouse Over' (which I do because I was trying to verify a tooltip). Thus, the result was it was returning 2 elements which robot framework cannot verify unless distinguished.
Thanks for everyone's inputs.
Upvotes: 0
Reputation: 775
There is a difference between the Element Should Contain
and Wait until Page Contains Element
.
The first will only check if at the moment the DOM contains the element with a substring of that text, while the last one will wait (by default) up to 5 seconds for that element to appear in DOM in order to validate if it exists. Or, to rephrase: the first one checks if the condition is correct, the other one listens for changes in the DOM to detect the needed value. Maybe your text is shown after some loading time?
From the documentation:
Element Should Contain
:
Verifies that element locator contains text expected. ... Use Element Text Should Be if you want to match the exact text, not a substring.
Wait Until Page Contains Element
:
Waits until element locator appears on current page. Fails if timeout expires before the element appears.
Upvotes: 1