dongxia2068 2018-01-30 10:03
浏览 51
已采纳

如何使用Behat查找带有正则表达式作为通配符的字符串?

I would like to create a Behat rule that allows me to find text with wildcards, since I couldn't find anything close to it.

I sometimes have to create some contents in Drupal projects, but the title isn't defined by the user so I can't know before running the test what the title will be when saved. I.E., in my situation, I would like to check if I can see the following text in the page

The content 'N° 1', of type 'ISAI', has been created.

Here, I would like to put N°1 in a wildcard, so I only have to look for

"The content '/^[A-Z]{1}°[0-9]+$/', of type 'ISAI', has been created."

I started to create a function that looks like the following:

And I should see the text "The content 'WILDCARD', of type 'ISAI', has been created" with wildcard matching "/[a-zA-Z °]+$/"

/**
* @Then I should see the text :arg1 with wildcard matching :arg2
*/
public function iShouldSeeTheTextWithWildcardMatching($text, $regex){
  $string = str_replace('WILDCARD', $regex, $text);
  $result = $this->getSession()->getPage()->find('css', sprintf('div.alert-success:contains("%s")', $string));
  echo $result; // is null
}

However, it fails because the selector can't find anything with "/" (since it's a regex).

My question is: is it possible to find some text with a regex, and what should I do to make my method work?

  • 写回答

1条回答 默认 最新

  • douju2599 2018-01-30 13:13
    关注

    Some of the options would be:

    A
    Create a selector based on the 2 partial text and exclude what you would have in the wildcard.

    //div[@class='alert-success'][contains(text(), 'The content')][contains(text(), 'of type \'ISAI\', has been created.')]
    

    B
    Another solution would be to see if you have a match in the text from this element.

    Steps:
    - define a regex
    - wait for success message and get text
    - check if you have a match in the saved text using the defined regex

    Your string/regex can be created using variables if needed. Use echo to mage sure your selector is valid. Make sure the page is loaded.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?