Html 在其父div在behat中具有类形式的输入字段中填充数据
我想填充数据输入字段,该字段是div的子字段,其类是behat中的form 要素上下文文件:Html 在其父div在behat中具有类形式的输入字段中填充数据,html,behat,Html,Behat,我想填充数据输入字段,该字段是div的子字段,其类是behat中的form 要素上下文文件: /** * @When /^(?:|I )filled in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" element$/ */ public function fillField($field, $values) { $field = $this->fixStepArgument($
/**
* @When /^(?:|I )filled in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" element$/
*/
public function fillField($field, $values)
{
$field = $this->fixStepArgument($field);
$value = $this->fixStepArgument($value);
$element = $this->getSession()->getPage()->find('xpath','//div[@class="form"]//input[@id="'.$field.'"]');
$this->getSession()->getPage()->fillField($element, $value);
}
如果要基于父级进行选择,则需要使用css/xpath选择器 您的css将是:
div.form #voucher-code
或XPath
//div[@class='form']//input[@id='voucher-code']
您可以删除FixStep参数行。
您的方法应该类似于:
/**
* @When /^I fill "(?P[^"]*)" with "(?P[^"]*)"$/
*/
public function fillWith($selector, $text)
{
$element = $this->getSession()->getPage()->find('css', $selector);
if($element === null){
throw new Exception("Element $selector not found");
}else{
$element->setValue($text);
}
}
您应该避免在step实现中使用硬编码值,如//div[@class=form]//input,这将降低仅来自类表单的div元素的可用性。请澄清您的问题所在,您尝试了什么,在哪里遇到了问题。我想在id为凭证代码的输入字段中填写7f2904204489727e,但选择它的父div。您使用的是Behat 2还是3?
//div[@class='form']//input[@id='voucher-code']
/**
* @When /^I fill "(?P[^"]*)" with "(?P[^"]*)"$/
*/
public function fillWith($selector, $text)
{
$element = $this->getSession()->getPage()->find('css', $selector);
if($element === null){
throw new Exception("Element $selector not found");
}else{
$element->setValue($text);
}
}