grabTextFrom查找使用Locator :: contains无法断言的预期字符串

问题描述 投票:0回答:1

我有一个有趣的案例,其中包含以下HTML和使用PhpBrowser的Codeception Acceptance测试,但我在StackOverflow上找不到类似的问题。

我想断言P标签的内容(作为页面上唯一表的一部分)包含预期的短语。

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

失败的HTML输出不会显示任何异常。我希望有人在PhpBrowser中对XPath有更多的经验,可以指出我所缺少的。

我正在查看的HTML部分如下。这是我要添加测试的遗留应用程序,因此,如您所见,HTML目前不是最佳的。

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

并且故障报告显示为:

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')]".

非常感谢您对这个问题的审议。

codeception
1个回答
0
投票

您的问题是将Locator::contains的结果传递给$I->see()方法,see希望将字符串作为第一个参数,并且它正在寻找HTML中的确切文本。

以]开头>

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

如果要检查文本是否显示在特定位置,请将XPath表达式作为第二个参数传递:

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

https://codeception.com/docs/modules/PhpBrowser#see所述>

© www.soinside.com 2019 - 2024. All rights reserved.