Search code examples
codeception

grabTextFrom finds anticipated string that assertion using Locator::contains cannot


I have an interesting case with the following HTML and a Codeception Acceptance test using PhpBrowser, which I've not been able to find a similar issue for on StackOverflow.

I am looking to assert that the content of a P tag (as part of the only table on the page) contains an anticipated phrase.

// representative code
$anticipatedValue = "updated text description";
$xpath = "//table/tbody/tr[position() = 1]/td[position() = 2]/descendant::p";
$val = $I->grabTextFrom($xpath); // this equals the value of $anticipatedValue;
echo "the value is[" .$val."][".$anticipatedValue."]"; // these match perfectly, no trimming needed
$I->see(Locator::contains($xpath,$anticipatedValue)); //this fails
$I->see(Locator::contains($xpath,$val)); //so does this

The fail HTML output does not display anything out of the ordinary. I am hoping someone with more experience of XPath in PhpBrowser can point out what I am missing.

The HTML portion I am looking at is below. This is a legacy application I am adding tests to, so the HTML is at present, as you can see, not optimal.

<table id="calendar" class="unitable" cellspacing=0 cellpadding=0>
    <thead>
        <tr>
            <th>Time</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
    <tr class="past"><td>10 Dec 00:00</td>
        <td class="entry" rowspan=2>
            <div class="time">Tue 10 09:00 - Tue 10 17:00</div>
            <div class="stage">new stage B</div>
            <p class="description">updated text description</p>
        </td>
    </tr>
    <tr class="past">
        <td>10 Dec 12:00</td>
    </tr>
    <tr class="past">
        <td>11 Dec 00:00</td>
        <td class="entry" rowspan=1>
            <div class="time">Wed 11 09:00 - Wed 11 11:30</div>
            <div class="stage">new stage C</div>
            <p class="description">this is new stage C</p>
        </td>
    </tr>
    <tr class="past">
        <td>11 Dec 12:00</td>
        <td></td>
    </tr>
    <tr class="past">
        <td>12 Dec 00:00</td>
        <td></td>
    </tr>
    <tr class="past">
        <td>12 Dec 12:00</td>
        <td class="entry" rowspan=1>
            <div class="time">Thu 12 13:30 - Thu 12 17:00</div>
            <div class="stage">new stage D</div>
            <p class="description">this is new stage D</p>
        </td>
    </tr>
</tbody>
<tfoot></tfoot>

</table>

and the failure report reads:

Failed asserting that on page ...
{short snippet of upper page HTML - not from the area under scrutiny, followed by}
[Content too long to display. See complete response in '/var/www/public/vhosts/system/tests/_output/' directory]
--> contains "//table/tbody/tr[position() = 1]/td[position() = 2]/descendant::p[contains(., 'updated text description')]".

Many thanks for your consideration of this issue.


Solution

  • Your problem is that you pass result of Locator::contains to $I->see() method, see expects to get a string as a first parameter and it is looking for that exact text in HTML.

    Start with

    $I->see('updated text description');
    

    If you want to check if the text is displayed in specific location, pass XPath expression as a second parameter:

    $I->see('updated text description', '//table/tbody/tr[position() = 1]/td[position() = 2]/descendant::p');
    

    as documented at https://codeception.com/docs/modules/PhpBrowser#see