本文介绍了如何断言文本在Mink中仅存在1次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做一个小项目,所以我决定和Behat/Mink在一起玩一会儿,我遇到了我一个人无法解决的第一个问题.

Im currently working on a small project and i decided to play around with Behat/Mink a little and i came across my first problem i can not solve alone.

我具有此功能,并且按预期运行

I have this feature and it is working as expected

Scenario: Create Customer
  Given I am on "/login"
  When  I fill in "username" with "testuser"
  And   I fill in "password" with "123"
  And   I press "Login"
  And   I follow "Customers"
  And   I follow "Create"
  Then  I should be on "/customer/new"
  And   I fill in "email" with "[email protected]"
  And   I press "Save"
  Then  I should be on "/customer/"
  And   I should see "[email protected]"

当我按保存"时,该项目然后检查电子邮件是否已经存在.如果确实如此,它只会重定向到/customer.我怎么能断言,我的页面上的文字只有一次(不是两次或更多)?

When i press on "Save" the project then checks, if the email already exists. If it does it just redirects to /customer. How can i assert, that a text is only once (not twice or more) on my page?

推荐答案

您可以编写一个新步骤,例如:

You could write a new step, something like :

/**
  * @Then /^I should see "([^"]*)" exactly "([^"]*)" times$/
  */
public function iShouldSeeTextSoManyTimes($sText, $iExpected)
{
    $sContent = $this->getSession()->getPage()->getText();
    $iFound = substr_count($sContent, $sText);
    if ($iExpected != $iFound) {
        throw new \Exception('Found '.$iFound.' occurences of "'.$sText.'" when expecting '.$iExpected);
    }
}

然后有一个诸如以下的场景:

And then have a scenario such as :

Scenario: Some many times
Given I am on "http://en.wikipedia.org"
Then I should see "Welcome" exactly "1" times

这篇关于如何断言文本在Mink中仅存在1次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 10:27