【问题标题】:Check single-occurrence in source code with PHP Codeception acceptance test使用 PHP Codeception 验收测试检查源代码中的单次出现
【发布时间】:2017-01-20 15:43:36
【问题描述】:

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

示例:

<html><body>foobar 234<div id="original">123</div></body></html>

应该失败的示例 #1(文本出现):

<html><body>foobar 123<div id="original">123</div></body></html>

应该失败的示例 #2(链接出现):

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

原因: 我有两个版本,其中一个版本是另一个版本的别名(称为“原始”)。我想确保只有别名在所有地方都使用,除了显示别名定义的“显示原始”页面。

【问题讨论】:

    标签: php codeception acceptance-testing


    【解决方案1】:

    我找到了解决办法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2015-03-08
      • 2016-07-08
      • 2017-03-25
      • 2016-11-03
      • 1970-01-01
      • 2015-12-29
      相关资源
      最近更新 更多