确保选择字段具有这些选项

问题描述 投票:0回答:1

使用 Behat 如何确保选择字段包含给定的一组选项?

我看不到任何检查此问题的核心方法。

php behat
1个回答
0
投票

将以下内容添加到您的

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
"""
© www.soinside.com 2019 - 2024. All rights reserved.