代码之家  ›  专栏  ›  技术社区  ›  vijay pujar

如何使用Behat/Mink从下拉列表中验证所选选项

  •  7
  • vijay pujar  · 技术社区  · 10 年前

    页面上有几个过滤器,我想验证默认的过滤器值。 我无法让选择器工作。 有人能帮忙吗。

    下面是代码段:

    <div class="filter-widget">
    <form name="" method="post" action="">
    <div class="pure-g-r">
        <div class="pure-u-1-4">
        <div><label for="day" class="required">Day</label><select id="day" name="day">    <option value="all">all</option><option value="Sunday">Sunday</option><option value="Monday">Monday</option><option value="Tuesday">Tuesday</option><option value="Wednesday">Wednesday</option><option value="Thursday" selected="selected">Thursday</option><option value="Friday">Friday</option><option value="Saturday">Saturday</option></select></div>
    </div>
    <div class="pure-u-1-4">
        <div><label for="month" class="required">Month</label><select id="month" name="month"><option value="January">January</option><option value="February">February</option><option value="March">March</option><option value="April" selected="selected">April</option><option value="May">May</option><option value="June">June</option><option value="July">July</option><option value="August">August</option><option value="September">September</option><option value="October">October</option><option value="November">November</option><option value="December">December</option></select></div>
    </div>
    </div>
    

    如上所述,过滤器默认为当前日期/月份,我需要能够验证这些值。但是下面使用的css选择器失败了。Behat是否支持它?

    $page = $this->getSession()->getPage();
    $defaultFilterDay = $page->find('css', '#day select option:selected')->getText();
    $defaultFilterMonth = $page->find('css', '#month select option:selected')->getText();
    $currentDay = new \DateTime('now')->format('l'); //This returns current Day
    $currentMonth = new \DateTime('now')->format('F'); //This returns current Month
    
    assertEquals(currentDay, $defaultFilterDay);
    assertEquals(currentMonth, $defaultFilterMonth);
    
    6 回复  |  直到 10 年前
        1
  •  11
  •   Bram John Bollinger    3 年前

    我也有同样的问题,我想使用选项值和选择css选择器来确定所选选项的值。这就是我最后所做的:

    /**
     * @Then /^the option "([^"]*)" from select "([^"]*)" is selected$/
     */
    public function theOptionFromSelectIsSelected($optionValue, $select)
    {
        $selectField = $this->getSession()->getPage()->find('css', $select);
        if (NULL === $selectField) {
            throw new \Exception(sprintf('The select "%s" was not found in the page %s', $select, $this->getSession()->getCurrentUrl()));
        }
    
        $optionField = $selectField->find('xpath', "//option[@selected='selected']");
        if (NULL === $optionField) {
            throw new \Exception(sprintf('No option is selected in the %s select in the page %s', $select, $this->getSession()->getCurrentUrl()));
        }
    
        if ($optionField->getValue() !== $optionValue) {
            throw new \Exception(sprintf('The option "%s" was not selected in the page %s, %s was selected', $optionValue, $this->getSession()->getCurrentUrl(), $optionField->getValue()));
        }
    }
    
        2
  •  8
  •   Community T.Woody    2 年前

    如果您不想在功能上下文中添加自定义步骤,可以尝试直接在Gherkin中使用CSS选择器:

    Then the "select[name='day'] option[selected='selected']" element should contain "Thursday"

    And the "select[name='month'] option[selected='selected']" element should contain "April"

        3
  •  2
  •   Mauricio Sánchez    9 年前

    我用这个步骤定义了一个上下文:

    /**
     * Checks, that option from select with specified id|name|label|value is selected.
     *
     * @Then /^the "(?P<option>(?:[^"]|\\")*)" option from "(?P<select>(?:[^"]|\\")*)" (?:is|should be) selected/
     * @Then /^the option "(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)" (?:is|should be) selected$/
     * @Then /^"(?P<option>(?:[^"]|\\")*)" from "(?P<select>(?:[^"]|\\")*)" (?:is|should be) selected$/
     */
    public function theOptionFromShouldBeSelected($option, $select)
    {
        $selectField = $this->getSession()->getPage()->findField($select);
        if (null === $selectField) {
            throw new ElementNotFoundException($this->getSession(), 'select field', 'id|name|label|value', $select);
        }
    
        $optionField = $selectField->find('named', array(
            'option',
            $option,
        ));
    
        if (null === $optionField) {
            throw new ElementNotFoundException($this->getSession(), 'select option field', 'id|name|label|value', $option);
        }
    
        if (!$optionField->isSelected()) {
            throw new ExpectationException('Select option field with value|text "'.$option.'" is not selected in the select "'.$select.'"', $this->getSession()); 
        }
    }
    

    因此,您可以在Behat功能中添加以下步骤:

    Then the "Option 1" option from "Select options" should be selected
    

    您可以在这里看到完整的上下文代码: https://github.com/Aplyca/BehatContexts/blob/master/src/Aplyca/BehatContext/FormContext.php#L64

        4
  •  1
  •   Beto Aveiga    5 年前

    如果要声明如下所示的选择值:

    And "4" from "Number of Columns" is selected
    And "red" from "Background Color" is selected
    And "plaid" from "Background Image" is selected
    

    然后使用@Dallas的这个修改/改进版本

      /**
       * @Then /^"([^"]*)" from "([^"]*)" is selected$/
       *
       * To assert a select value.
       * Shamelessly inspired by: https://stackoverflow.com/a/33223002/1038565
       */
      public function theOptionFromSelectIsSelected($optionValue, $select) {
        $selectField = $this->getSession()->getPage()->findField($select);
    
        if (NULL === $selectField) {
          throw new \Exception(sprintf('The select "%s" was not found in the page %s', $select, $this->getSession()->getCurrentUrl()));
        }
    
        $optionField = $selectField->find('xpath', "//option[@selected]");
        if (NULL === $optionField) {
          throw new \Exception(sprintf('No option is selected in the %s select in the page %s', $select, $this->getSession()->getCurrentUrl()));
      }
    
      if ($optionField->getValue() != $optionValue) {
        throw new \Exception(sprintf('The option "%s" was not selected in the page %s, %s was selected', $optionValue, $this->getSession()->getCurrentUrl(), $optionField->getValue()));
      }
    }
    
        5
  •  0
  •   navitronic    9 年前

    我使用以下代码获取所选选项的值

    $elementByName = $this->getSession ()->getPage ()->find ( 'css', "#" . $name );
    
    if (! $elementByName) {
        throw new FailureException ( 'select with name ' . $name . ' is not found' );
    } else {
        $v = $elementByName->getValue ();
    }
    
        6
  •  -1
  •   vijay pujar    10 年前

    以下选择器适用于我:

    $defaultFilterDay = $page->find("css", "#day option[selected='selected']")->getText();
        $defaultFilterMonth = $page->find("css", "#month option[selected='selected']")->getText();
    
        date_default_timezone_set('UTC');
        $currentTime = new \DateTime('now');
        $currentDay = $currentTime->format('l');
        $currentMonth = $currentTime->format('F');
    
        assertEquals($currentDay, $defaultFilterDay);
        assertEquals($currentMonth, $defaultFilterMonth);