我正在使用Behat/Mink测试Drupal 8网站。
我有一个带有IP地址的输入字段,我想确保已记录IP地址。我不想检查特定的IP地址,因为它可能会更改,因此我只想确保该字段具有一定的价值。
基于假设IP地址将包含一个期限,我尝试了以下方式:
Then the "#edit-field-ip-0-value" element should contain "."
,但这会失败,错误:
The string "." was not found in the HTML of the element matching css "#edit-field-ip-0-value". (BehatMinkExceptionElementHtmlException)
但是,字段的值为:
172.19.0.1
所以有一个时期;我不明白为什么"包含"看不到它。
我也尝试过这样的检查:
Then the "#edit-field-ip-0-value" element should contain "0"
,但失败的错误(在元素的HTML中找不到字符串)。
编辑
这是我试图针对的HTML:
<input class="js-text-full text-full form-text" data-drupal-selector="edit-field-ip-0-value" type="text" id="edit-field-ip-0-value" name="field_ip[0][value]" value="172.19.0.1" size="60" maxlength="255" placeholder="">
这是我最终使用的代码:
/**
* @Then the :element element should contain an IP address
*
* Checks that the element with the specified CSS contains IP address value.
*/
public function assertElementContainsIpAddress($element) {
$page = $this->getSession()->getPage();
// Alternately, substitute with getText() for the label.
$element_value = $page->find('css', "$element")->getValue();
$valid_ip_address = filter_var($element_value, FILTER_VALIDATE_IP);
if ($valid_ip_address === FALSE) {
throw new Exception("Valid IP address not found in element $element, which had a value of $element_value.");
}
}