使用PHP Codeception验收测试检查源代码中的单个事件

使用PHP Codeception验收测试检查源代码中的单个事件,php,codeception,acceptance-testing,Php,Codeception,Acceptance Testing,如何使用PHP/Codeception验收测试检查特定的$string(f.e.123)是否仅出现在特定的HTML元素中,而不出现在外部或任何其他地方 举个好例子: <html><body>foobar 234<div id="original">123</div></body></html> 现在我需要像这样的东西 $I->seeInPageSourceElement($original, '#original')

如何使用PHP/Codeception验收测试检查特定的
$string
(f.e.
123
)是否仅出现在特定的HTML元素中,而不出现在外部或任何其他地方

举个好例子:

<html><body>foobar 234<div id="original">123</div></body></html>
现在我需要像这样的东西

$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);
原因: 我有两个版本,其中一个版本是另一个版本的别名(称为“原始”)。我想确保除显示别名定义的“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);
    }
我找到了一个解决方案:

  • 使用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);
    }
$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);
    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);
    }