使用 Behat 如何确保选择字段包含给定的一组选项?
我看不到任何检查此问题的核心方法。
将以下内容添加到您的
FeatureContext.php
:
/**
* @Then /^the select field "([^"]*)" should have a list containing:$/
*
* @param $locator
* string $locator input id, name or label
* @param \Behat\Gherkin\Node\PyStringNode $list
* A list of options that should be present.
*/
public function shouldHaveAListContaining($locator, PyStringNode $list): void {
$session = $this->getSession();
$page = $session->getPage();
$element = $page->findField($locator);
if ($element === NULL) {
throw new \InvalidArgumentException(sprintf('Could find element "%s".', $locator));
}
$options = [];
foreach ($element->findAll('css', 'option') as $option) {
$options[] = $option->getText();
}
$missing = array_diff($list->getStrings(), $options);
if (count($missing) > 0) {
$context = [$locator, implode(', ', $missing)];
throw new \RuntimeException(vsprintf('Element "%s" is missing these options "%s"', $context));
}
}
这样称呼它:
And the select field "YOUR_LABEL" should have a list containing:
"""
Option 1
Option 2
"""