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.
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>
.