duanlingzei0170 2017-01-20 15:43
浏览 66
已采纳

使用PHP Codeception验收测试检查源代码中的单次出现

How can I check that a specific $string (f.e. 123) only occurs within a specific HTML element but not outside or anywhere else using PHP/Codeception acceptance tests?

Example which would be fine:

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

Example which should fail #1 (text occurrence):

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

Example which should fail #2 (link occurrence):

<html>
  <body>
    foobar
    <div id="original">123</div>
    <a href="/link/123">Link</a>
  </body>
</html>

What I've tried on tests other than that specific page:

$I->seeInPageSource($alias);
$I->dontSeeInPageSource($original);

Now I would need something like

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

Reason: I have two versions, where one version is alias for another (called "original"). I want to make sure only the alias is used everywhere except on the "show original" page, where the alias defition is shown.

  • 写回答

1条回答 默认 最新

  • douzhaochan6468 2017-03-22 17:39
    关注

    I found a solution:

    • detach element with jQuery (requires the test page to use it)
    • do regular test
    • re-attach element

    Not the perfect solution, but it works.

        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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP