【问题标题】:'no method expect' when using Protractor as a library将量角器用作库时“没有方法期望”
【发布时间】:2014-01-25 14:35:06
【问题描述】:

使用Protractor as a library

无法要求引用 Jasmine。引用expect 方法返回输出Cannot call method 'expect' of null

更新代码以反映 cmets:

var protractor = require('protractor');
require('protractor/node_modules/minijasminenode');
require('protractor/jasminewd'); // output: jasmine is undefined (this error can only be seen if the above line is commented out)
//expect(true).toBe(true); // output: Cannot call method 'expect' of null

var driver = new protractor.Builder()
    .usingServer('http://localhost:4444/wd/hub')
    .withCapabilities(protractor.Capabilities
    .chrome()).build();

var ptor = protractor.wrapDriver(driver);

ptor.get('http://www.angularjs.org').then(function(){
    ptor.element(protractor.By.model('yourName')).sendKeys('test')
        .then(console.log('success')); // output: success
        ptor.getCurrentUrl().then(function(url){
            console.log(url); // output: http://www.angularjs.org
            expect(url).toContain('angular'); // output: Cannot call method 'expect' of null
        });
});

相关信息请参见https://github.com/angular/protractor/issues/21

【问题讨论】:

    标签: node.js angularjs jasmine protractor jasmine-node


    【解决方案1】:

    引用朱莉的this post

    要让 Jasmine 自动理解异步测试,您需要使用 jasmine-wd 适配器:

    require('protractor/jasminewd');
    

    (只需在... = require('protractor');之后添加上述行。)

    【讨论】:

    • 感谢您的回复。这为我指明了正确的方向。但是,我现在收到以下错误: jasmine is not defined at this line at protractor/jasminewd/index.js: jasmine.getEnv().addReporter(new OnTimeoutReporter(function() { flow.reset(); }));
    • 你添加了require('protractor/jasminewd'); 之后 self.protractor = require('protractor'); 吗?
    • expect 是否定义在全局范围内? (例如,在 specs 文件的顶部添加 expect(true).toBe(true); 是否会引发错误?)
    • 这正是我的问题,expect 没有在全局范围内定义(也没有任何其他 jasmine 方法)。如代码示例所示,“未定义预期”。 expect(true).toBe(true);expect(true).toBe(true); 也是如此。再次感谢您的反馈。
    • 您的示例确实 not 表明 expect() 未在全局范围内定义(您调用的是其他对象的方法,而不是全局对象(例如 @987654331 @))。我的问题是它是否被定义为全局对象的方法。
    【解决方案2】:

    每次进入 jasmine 的 it 函数上下文时都会新生成全局 jasmine expect 函数。

    这对您的代码意味着什么? 不能在 it 函数之外调用 expect。

    示例:

    ...
      // somewhere in your code
    ...
      // function expect doesn’t exist here
    describe( 'your case', function() {
        // expect doesn’t exist here either
      it( 'should work', function() {
        // horray - the global expect is available !!
        // note : the expect function is generated before running your callback 
        // function to collect the expect'ed results for exactly this 'it' case
          expect( true).toBe( true); 
      });
    })
    

    【讨论】:

    • 感谢您的回复。我不再使用 Jasmine,而是使用 CucumberJS + Protractor + Chai。
    • 这是我的问题。将expect 放入it 就可以了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多