使用 PHP Codeception 验收测试检查源代码中的单次出现
Check single-occurrence in source code with PHP Codeception acceptance test
如何检查特定的 $string
(f.e。123
)仅出现在特定的 HTML 元素内,而不出现在外部或其他任何使用 PHP/Codeception 验收测试?
可以的示例:
<html><body>foobar 234<div id="original">123</div></body></html>
应该失败的示例 #1(文本出现):
<html><body>foobar 123<div id="original">123</div></body></html>
应该失败的示例 #2(link 次):
<html>
<body>
foobar
<div id="original">123</div>
<a href="/link/123">Link</a>
</body>
</html>
除该特定页面外,我在测试中尝试过的内容:
$I->seeInPageSource($alias);
$I->dontSeeInPageSource($original);
现在我需要类似
的东西
$I->seeInPageSourceElement($original, '#original');
$I->dontSeeInPageSourceExceptElement($original, '#original');
// could be implemented like this:
$pageSourceWithoutElement = str_replace(
$I->grabPageSourceElement('#original'),
'',
$I->grabPageSource()
);
$I->assertNotContains($original, $pageSourceWithoutElement);
原因:
我有两个版本,其中一个版本是另一个版本的别名(称为 "original")。我想确保除了显示别名定义的 "show original" 页面之外的所有地方都只使用别名。
我找到了解决方案:
- 使用 jQuery 分离元素(需要测试页才能使用)
- 定期测试
- 重新附加元素
不是完美的解决方案,但它有效。
public function dontSeeInPageSourceExceptElement($text, $excludeSelector)
{
$I = $this;
// check for positive occurrence in exclude selector (optional)
// $I->see($text, $excludeSelector);
// detach the selected element
// Problem: append() just re-attaches the element, but this might not be the right position
$I->executeJs(
sprintf(
// s = subject, p = parent, bs = backup of subject
"var s = $(%s), p = s.parent(), bs = s.detach(); " .
"setTimeout(function() { p.append(bs); }, 2000);",
json_encode($excludeSelector)
)
);
// check for negative occurrence now
$I->dontSeeInPageSource($text);
// wait 2 seconds (re-attach timeout)
$I->wait(2);
}
如何检查特定的 $string
(f.e。123
)仅出现在特定的 HTML 元素内,而不出现在外部或其他任何使用 PHP/Codeception 验收测试?
可以的示例:
<html><body>foobar 234<div id="original">123</div></body></html>
应该失败的示例 #1(文本出现):
<html><body>foobar 123<div id="original">123</div></body></html>
应该失败的示例 #2(link 次):
<html>
<body>
foobar
<div id="original">123</div>
<a href="/link/123">Link</a>
</body>
</html>
除该特定页面外,我在测试中尝试过的内容:
$I->seeInPageSource($alias);
$I->dontSeeInPageSource($original);
现在我需要类似
的东西$I->seeInPageSourceElement($original, '#original');
$I->dontSeeInPageSourceExceptElement($original, '#original');
// could be implemented like this:
$pageSourceWithoutElement = str_replace(
$I->grabPageSourceElement('#original'),
'',
$I->grabPageSource()
);
$I->assertNotContains($original, $pageSourceWithoutElement);
原因: 我有两个版本,其中一个版本是另一个版本的别名(称为 "original")。我想确保除了显示别名定义的 "show original" 页面之外的所有地方都只使用别名。
我找到了解决方案:
- 使用 jQuery 分离元素(需要测试页才能使用)
- 定期测试
- 重新附加元素
不是完美的解决方案,但它有效。
public function dontSeeInPageSourceExceptElement($text, $excludeSelector)
{
$I = $this;
// check for positive occurrence in exclude selector (optional)
// $I->see($text, $excludeSelector);
// detach the selected element
// Problem: append() just re-attaches the element, but this might not be the right position
$I->executeJs(
sprintf(
// s = subject, p = parent, bs = backup of subject
"var s = $(%s), p = s.parent(), bs = s.detach(); " .
"setTimeout(function() { p.append(bs); }, 2000);",
json_encode($excludeSelector)
)
);
// check for negative occurrence now
$I->dontSeeInPageSource($text);
// wait 2 seconds (re-attach timeout)
$I->wait(2);
}