使用Behat/Mink时的类型错误


TypeError while using Behat/Mink

执行

以下代码:

<?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,但你试图在这里使用它:

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

wait() 方法接受 JavaScript 作为其第二个参数。它等待脚本计算为 true(作为第一个参数传递的最大毫秒数)。

如果我

没记错的话,这段代码来自网站上的 behat mink 示例。

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

很可能你没有包括jQuery。

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

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

对我来说是使用 jQuery 而不是$快捷方式:

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