【问题标题】:Behat/Mink and Select2 comboBehat/Mink 和 Select2 组合
【发布时间】:2015-02-02 06:34:01
【问题描述】:

我尝试使用 Select2 使用 Web UI 创建 Behat 场景。

当我尝试更改选择值时,由于基本选择被 Select2 隐藏,我遇到了 Behat 错误。

但是我的 select2 组件已经出错了,因为 mink 无法与之交互。

您是否已经将 Behat/Mink 与 Select2 一起使用?你有什么建议可以帮助我吗?

【问题讨论】:

    标签: php jquery-select2 behat mink


    【解决方案1】:

    我终于写了一个 Behat 来像用户一样与 Select2 字段进行交互。

    这里是最完整步骤的摘录:

    /**
     * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
     */
    public function iFillInSelectInputWithAndSelect($field, $value, $entry)
    {
        $page = $this->getSession()->getPage();
    
        $inputField = $page->find('css', $field);
        if (!$inputField) {
            throw new \Exception('No field found');
        }
    
        $choice = $inputField->getParent()->find('css', '.select2-selection');
        if (!$choice) {
            throw new \Exception('No select2 choice found');
        }
        $choice->press();
    
        $select2Input = $page->find('css', '.select2-search__field');
        if (!$select2Input) {
            throw new \Exception('No input found');
        }
        $select2Input->setValue($value);
    
        $this->getSession()->wait(1000);
    
        $chosenResults = $page->findAll('css', '.select2-results li');
        foreach ($chosenResults as $result) {
            if ($result->getText() == $entry) {
                $result->click();
                break;
            }
        }
    }
    

    几天后我将在 Github 上开源 Select2 上下文。

    【讨论】:

    • 我在 Github 上开源了 select2 上下文。分支 1.x 与 Behat2 兼容,2.x 与 Behat3 兼容。
    【解决方案2】:

    我被那个问题困住了,但我已经用我在Filling in hidden inputs with Behat 找到的信息完成了它。

    我的解决方案只是略有不同:

    /** 
     * @Given /^I fill hidden field "([^"]*)" with "([^"]*)"$/ 
    */
    public function iFillHiddenFieldWith($class, $value){
      $this->getSession()->getPage()->find('css', $class)->setValue($value);
    }
    

    然后我以如下方式使用它:

    And I fill hidden field "#s2id_edit-my-field input" with "random value".
    

    【讨论】:

      猜你喜欢
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 2017-01-25
      • 1970-01-01
      • 2015-07-07
      • 2014-09-22
      相关资源
      最近更新 更多