testing - Behat 从测试步骤传递一个值

标签 testing behat mink

我试图断言在一个字段中输入的随机文本出现在下一页(确认)

我是这样做的

When I fill in "edit-title" with random value of length "8"

/**
     * Fills in form field with specified id|name|label|value with random string
     * Example: And I fill in "bwayne" with random value of length "length"
     *
     * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with random value of length "(?P<length>(?:[^"]|\\")*)"$/
     */
    public function fillFieldWithRandomValue($field, $length)
    {
        $field = $this->fixStepArgument($field);
        $value = $this->generateRandomString($length);
        $this->getSession()->getPage()->fillField($field, $value);
    }

比我想做出断言 - 像这样:

Then I should see text matching "<RANDOM VALUE ENTERED IN THE PREVIOUS STEP>"

这可能吗?

更新:

但是如果我想多次使用一个 generateRandomString 方法,然后一个接一个地获取这个方法的值,那么使用 setter 和 getter 会是什么样子呢?我是否必须为每个测试步骤创建变量和函数?像这样:

When I fill in "x" with random value of length "8"
And I fill in "y" with random value of length "12"
And I go to other page
Then I should see text matching "VALUE ENTERED TO X"
And I should see text matching "VALUE ENTERED TO Y"

最佳答案

您可以创建一个属性并在上一步中设置它。并在下一个中使用它,但如果它有值(value)则断言它。 此外,使用适当的可见性类型定义该属性会很好且可读

/**
 * @var string
 */
private randomString;

/**
 * Fills in form field with specified id|name|label|value with random string
 * Example: And I fill in "bwayne" with random value of length "length"
 *
 * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with random value of length "(?P<length>(?:[^"]|\\")*)"$/
 */
public function fillFieldWithRandomValue($field, $length)
{
    $field = $this->fixStepArgument($field);
    $this->randomString = $this->generateRandomString($length);
    $this->getSession()->getPage()->fillField($field, $this->randomString);
}

/**
 *
 * @Then /^(?:|I )should see that page contains random generated text$/
 */
public function assertPageContainsRandomGeneratedText()
{
    //Assertion from phpunit
    $this->assertNotNull($this->randomString);

    $this->assertPageContainsText($this->randomString);
}

注意:根据您的 behat 设置 - 来自 phpunit 的断言可能不起作用。

关于testing - Behat 从测试步骤传递一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41020319/

相关文章:

php - API 测试工具

android - Cucumber:是否可以从 Android 设备上的文件夹中读取 .feature 文件?

testing - SAML2 测试客户端

selenium - [Vagrant][Jenkins][Behat][Selenium]如何组合它们?

php - 如何使用 Behat 和 Mink PHP 处理浏览器窗口或选项卡

session-cookies - 如何重新启动浏览器以保留 Behat 中的 cookie?

windows - 自动化 Win32 驱动程序测试

php - 格式错误的字段路径 ""(InvalidArgumentException)

selenium - 不同浏览器下运行behat测试