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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真