【问题标题】:TypeError while using Behat/Mink使用 Behat/Mink 时出现类型错误
【发布时间】:2016-12-19 21:24:49
【问题描述】:

以下代码的执行:

<?php

class InheritedFeatureContext extends Behat\MinkExtension\Context\MinkContext
{
    /**
     *Simulates hovering over a link
     *
     * @When /^I mouse over "([^"]*)"$/
     */
        public function iMouseOver($link)
        {
        $this->getSession()->getPage()->findLink($link)->mouseOver();
        }

    /**
     *Waits for the appearence of a drop down
     *
     * @Then /^I wait for the suggestion box to appear$/
     */
    public function iWaitForTheSuggestionBoxToAppear()
    {
        $this->getSession()->wait(5000, "$('.suggestions-results').children().length > 0");
    }
}

产生错误消息:

  TypeError: $ is not a function

参考:

$this->getSession()->wait(5000, "$('.suggestions-results').children().length > 0");

此代码以前运行良好。任何想法

上下文:这是 Behat/Mink 功能的一部分

【问题讨论】:

  • 这应该不需要 jQuery 它是从 Mink 上下文继承的。

标签: php behat mink


【解决方案1】:

您可能没有包含 jQuery,但您尝试在这里使用它:

$this->getSession()->wait(5000, "$('.suggestions-results').children().length > 0");

wait() 方法接受 javascript 作为其第二个参数。它等待脚本评估为真(作为第一个参数传递的最大毫秒数)。

【讨论】:

    【解决方案2】:

    如果我没记错的话,这段代码来自现场的 behat mink 示例。

    该示例使用http://en.wikipedia.org/wiki 作为基本网址,维基百科确实附有jquery 脚本。在您的情况下,您是否检查了您的 url 位置是否包含 jquery

    【讨论】:

      【解决方案3】:

      很可能您没有包含 jQuery。

      如果您不想为了支持这个 Mink 测试而将 jQuery 添加到您的项目中,请尝试将您的行更改为以下内容,这将完成同样的事情,但不使用 jQuery:

      $this-&gt;getSession()-&gt;wait(5000, "document.querySelector('.suggestions-results').children.length &gt; 0");

      【讨论】:

        【解决方案4】:

        这适用于我使用 jQuery 而不是 $ 快捷方式:

        $this->getSession()->wait(5000, "jQuery('.suggestions-results').children().length > 0");
        

        【讨论】:

        • 这不是和 brianrhea 完全一样的答案吗?
        • 没有。 brianrea 的答案不使用 jQuery。这个可以。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-11
        • 1970-01-01
        • 1970-01-01
        • 2012-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多