Behat(+ Mink)检查某些文本,然后检查某些文本(在同级元素中) [英] Behat (+ Mink) Check for some text followed by some text (in sibling elements)

查看:114
本文介绍了Behat(+ Mink)检查某些文本,然后检查某些文本(在同级元素中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I should see...函数是Behat的一项基本功能,但是我经常发现自己想在我的场景中写这样的东西:

The I should see... function is an essential feature of Behat but I regularly find myself wanting to write something like this in my scenarios:

Then I should see "Session ID" followed by "3"

这是我人为地描述内容中彼此相邻的2条文本的方式.也就是说,第一个字符串是任何元素的内容,第二个字符串是它的下一个直接同级的内容.

Which is my humanly-parsable way of describing 2 pieces of text next to each other in the content. That is to say the first string is the content of any element and the second is the content of it's next immediate sibling.

这对于检查标签-值类型布局很有用:

This would be useful for checking label - value type layouts:

或者如果我要检查列表数据中的标头-单元格值类型布局:

Or if I want to check header - cell value type layouts in tabulated data:

甚至定义标题-定义.

Or even definition title - definition.

很明显,我可以向要测试的每个元素添加"id"属性,但是在一个复杂的页面中,该页面的许多部分都需要测试,这开始使我感觉像是在使用一次性属性来夸大标记.

Obviously I could add 'id' attributes to every element I want to test but in a complicated page where many parts of the content need testing this starts to feel like I am bloating the markup with single-use attributes.

推荐答案

能够使用...

Then I should see "Session ID" followed by "3"

在您的FeatureContext.php文件中添加以下方法:

Add the following methods to your FeatureContext.php file:

/**
 * @Then I should see :textA followed by :textB
 */
public function iShouldSeeFollowedBy($textA, $textB)
{
    $content = $this->getSession()->getPage()->getContent();

    // Get rid of stuff between script tags
    $content = $this->removeContentBetweenTags('script', $content);

    // ...and stuff between style tags
    $content = $this->removeContentBetweenTags('style', $content);

    $content = preg_replace('/<[^>]+>/', ' ',$content);

    // Replace line breaks and tabs with a single space character
    $content = preg_replace('/[\n\r\t]+/', ' ',$content);

    $content = preg_replace('/ {2,}/', ' ',$content);

    if (strpos($content,$textA) === false) {
        throw new Exception(sprintf('"%s" was not found in the page', $textA));
    }

    $seeking = $textA . ' ' . $textB;
    if (strpos($content,$textA . ' ' . $textB) === false) {
        // Be helpful by finding the 10 characters that did follow $textA
        preg_match('/' . $textA . ' [^ ]+/',$content,$matches);
        throw new Exception(sprintf('"%s" was not found, found "%s" instead', $seeking, $matches[0]));
    }
}


/**
 * @param string $tagName - The name of the tag, eg. 'script', 'style'
 * @param string $content
 *
 * @return string
 */
private function removeContentBetweenTags($tagName,$content)
{
    $parts = explode('<' . $tagName, $content);

    $keepers = [];

    // We always want to keep the first part
    $keepers[] = $parts[0];

    foreach ($parts as $part) {
        $subparts = explode('</' . $tagName . '>', $part);
        if (count($subparts) > 1) {
            $keepers[] = $subparts[1];
        }
    }

    return implode('', $keepers);
}

这篇关于Behat(+ Mink)检查某些文本,然后检查某些文本(在同级元素中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆