3

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  
E.AS.P.
  • 33
  • 1
  • 1
  • 4
  • 1
    Does that element contain exactly the four characters 'text', or does it have more information than that? – Bryan Oakley Mar 14 '19 at 13:41
  • The element does contain more characters like "this is a text." But this shouldn't matter right? Since `Element Should Contain`, based on my understanding, verifies only that the element contains the text rather than verifying if its exactly the text. – E.AS.P. Mar 15 '19 at 00:45
  • @E.AS.P. According to the SeleniumLibrary docs: "Use 'Element Text Should Be' if you want to match the exact text, not a substring.", you're right. – Nomce Mar 15 '19 at 15:33

3 Answers3

1

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.

Nomce
  • 738
  • 1
  • 6
  • 18
  • Hi, you're right. I'm actually trying to verify a text in a tooltip which appears after i perform a Mouse Over. So what I did was perform `Mouse Over`, `Wait Until Page Contains Element`, then `Element Should Contain`. – E.AS.P. Mar 18 '19 at 01:56
0

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.

E.AS.P.
  • 33
  • 1
  • 1
  • 4
0

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
Jay
  • 13
  • 1
  • 6