Testing Behat传递测试步骤中的值
我试图断言在一个字段中输入的随机文本会出现在下一页(确认) 我是这样做的Testing Behat传递测试步骤中的值,testing,behat,mink,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
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);
}
可以创建属性并在上一步中进行设置。并在下一个中使用它,但如果它有值,则断言它。 另外,用适当的可见性类型定义该属性也很好,可读性也很好
/**
* @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);
}
/**
*@var字符串
*/
私有随机字符串;
/**
*使用随机字符串用指定的id | name | label |值填写表单字段
*示例:我用长度“length”的随机值填写“bwayne”
*
*@When/^(?:| I)用长度的随机值(?P(?:[^“]\\”)*)填写“(?P(?:[^“]\\”)”$/
*/
公共函数fillFieldWithRandomValue($field,$length)
{
$field=$this->fixStepArgument($field);
$this->randomString=$this->generateRandomString($length);
$this->getSession()->getPage()->fillField($field,$this->randomString);
}
/**
*
*@Then/^(?:| I)应该看到该页面包含随机生成的文本$/
*/
公共函数AssertPageContainesRandomGeneratedText()
{
//来自phpunit的断言
$this->assertNotNull($this->randomString);
$this->assertPageContainsText($this->randomString);
}
注意:根据您的行为设置,phpunit的断言可能不起作用。您可以创建一个属性,并在上一步中进行设置。并在下一个中使用它,但如果它有值,则断言它。 另外,用适当的可见性类型定义该属性也很好,可读性也很好
/**
* @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);
}
/**
*@var字符串
*/
私有随机字符串;
/**
*使用随机字符串用指定的id | name | label |值填写表单字段
*示例:我用长度“length”的随机值填写“bwayne”
*
*@When/^(?:| I)用长度的随机值(?P(?:[^“]\\”)*)填写“(?P(?:[^“]\\”)”$/
*/
公共函数fillFieldWithRandomValue($field,$length)
{
$field=$this->fixStepArgument($field);
$this->randomString=$this->generateRandomString($length);
$this->getSession()->getPage()->fillField($field,$this->randomString);
}
/**
*
*@Then/^(?:| I)应该看到该页面包含随机生成的文本$/
*/
公共函数AssertPageContainesRandomGeneratedText()
{
//来自phpunit的断言
$this->assertNotNull($this->randomString);
$this->assertPageContainsText($this->randomString);
}
注意:根据您的行为设置-来自phpunit的断言可能不起作用。由于您将在多个位置调用
generateRandomString
方法,因此您还应该有一个类似于setter和getter的获取此值的方法
我的建议是创建一个类,该类包含处理所有数据的相关方法,而不是在每个使用数据的地方保存变量,生成+保存并从需要的任何地方读取数据
提示:您可以更灵活地定义步骤,并为随机字符串设置默认长度,以防未提供一个长度
高级别示例:
class Data
{
public static $data = array();
public static function generateRandomString($length = null, $name = null)
{
if ($name = null) {
$name = 'random';
};
if ($length = null) {
$length = 8;
};
// generate string like $string =
return self::$data[$name] = $string;
}
public static function getString($name = null)
{
if ($name = null) {
$name = 'random';
};
// exception handling
if (array_key_exists($name, self::$data) === false) {
return null;
}
return self::$data[$name];
}
}
这是一个高级示例,您可能需要进行一些调整
如果验证在同一个类中,那么也可以在同一个类中进行所有验证,这意味着
generateRandomString
和getString
与步骤在同一个类中。由于您将在多个位置调用generateRandomString
方法,因此您还应该有一个类似于setter和getter的获取此值的方法
我的建议是创建一个类,该类包含处理所有数据的相关方法,而不是在每个使用数据的地方保存变量,生成+保存并从需要的任何地方读取数据
提示:您可以更灵活地定义步骤,并为随机字符串设置默认长度,以防未提供一个长度
高级别示例:
class Data
{
public static $data = array();
public static function generateRandomString($length = null, $name = null)
{
if ($name = null) {
$name = 'random';
};
if ($length = null) {
$length = 8;
};
// generate string like $string =
return self::$data[$name] = $string;
}
public static function getString($name = null)
{
if ($name = null) {
$name = 'random';
};
// exception handling
if (array_key_exists($name, self::$data) === false) {
return null;
}
return self::$data[$name];
}
}
这是一个高级示例,您可能需要进行一些调整
如果验证在同一个类中,那么也可以在同一个类中进行验证,这意味着generateRandomString
和getString
与步骤在同一个类中
/**
* @Then /^I fill in "x" with random value as (.*?)( and length (\d+))?$/
*/
public function iFillInWithRandomValue($selector, $name, $length = null){
$string = Data::generateRandomString($length, $name);
// fill method
}
/**
* @Then /^I should see text matching "first name"$/
*/
public function iShouldSeeTextMatching($variableName){
$string = Data::getString($variableName);
// assert/check method
}