3

I have been trying to get the first error message out of the following html in Fitnesse test using BrowserTest Slim fixture.

<div class="validation-errors">
  <ul>
    <li>Enter your code.</li>
    <li>Enter your username.</li>
    <li>Enter your password.</li>
  </ul>
</div>

I have tried the following Xpath:

//div[@class='validation-errors']/ul/li[1]/text()
//div[@class='validation-errors']/ul/li[1]
//div[@class='validation-errors']/ul/li

The first xpath returns [null] where as the second and third returns [0].

The expected result is Enter your code.

If I take li out completely

//div[@class='validation-errors']/ul

then it returns

[Enter your code.
Enter your username.
Enter your password.]

So either way my test fails where it looks only for the first error message. I have tried the xpath query on online Xpath testers where it seems to work as expected.

Here is a sample fitnesse test case:

|table template|try to login                                      |
|open          |https://my.url.com/login?                             |
|enter         |@{code}    |as             |Code                      |
|enter         |@{username}|as             |Username                  |
|enter         |@{password}|as             |Password                  |
|click         |Login                                                 |
|$message=|value of                   |xpath=//div[@class='validation-errors']/ul/li[1]|



|storyboard|browser test|


|try to login                                                                        |
|code        |username           |password            |message?                          |
|            |                   |                    |Enter your code.                  |

May be I am missing something obvious but having tried to figure it out for few hours without any success I thought I will post this here in case some had a similar experience or can provide a solution to get the value of first <li> out of the three.

s-rod
  • 33
  • 5

2 Answers2

1

Unfortunately all <li> elements get an implicit 'value' attribute in HTML, and that is what BrowserTest's value of was returning, until version 2.5.1. So your second and third XPaths are absolutely correct to select the item you are looking for. The value that was returned is unfortunately not what you expect/want. If you try them again with release 2.5.1 they should work.


Background

value of was originally designed to return the value of input elements (which is in their 'value' attribute), and has fallback behavior so that if the element found does a 'value' attribute its text() is returned. This works quite nice most of the time, but is a pain for <li> which has a value attribute even if there is none in the page's source. Before 2.5.1 BrowserTest did not have a method that allows you to get the text of an <li>.

Fried Hoeben
  • 3,247
  • 16
  • 14
0

Try one of these - long one going through id and all levels or short one that goes directly:

xpath=//div[@class="validation-errors"]/ul/li[text()="Enter your code."]

xpath=//li[text()="Enter your code."]
Newcomer
  • 483
  • 6
  • 17
  • Thanks Pirre, unfortunately this doesn't work either. I am sure I had tried this but since you mentioned it gave it go again. Returns `[0] expected [Enter your code.]` – s-rod Dec 18 '15 at 15:24
  • @s-rod i edited the answer - try again. Maybe you are declaring your xpath the wrong way ? I don't know the syntax in your IDE, but these xpath selectors work in Firebug-Mozilla and Devtools-Chrome in your HTML. But it seems like it's working cause like you said it returned: [0] expected [Enter your code.] – Newcomer Dec 18 '15 at 16:08
  • As I mentioned in my original question xpath was not wrong as it did work correctly and return expected value in multiple online xpath testers that I tried it on including the ones you had suggested earlier. But the same when copied and pasted on to my Fitnesse wiki page didn't. – s-rod Dec 21 '15 at 11:18
  • Anyway, what I did manage to do was work around this issue by ignoring `
  • ` in the xpath i.e `//div[@class='validation-errors']/ul` which gave me the list of all/any one error message (as applicable). Then instead of asserting against the first error in the list I am asserting against all message which probably is the correct approach because that is what the test is, are we getting all the expected validation message or messages.
  • – s-rod Dec 21 '15 at 11:28
  • @s-rod this way you just fetched all errors, but i understood that you needed only one - the first one, but play, glad you resolved your issue. – Newcomer Dec 21 '15 at 11:55
  • Thanks @pirre for your time and effort. Yes, I think,although I didn't get the exact solution for my original question, I have a better test case scenario now covering all possible combination of messages. – s-rod Dec 21 '15 at 13:10
  • @s-rod ok i hope my answer helped you. – Newcomer Dec 21 '15 at 13:43