TypeError:无法读取属性'然后'量角器黄瓜中未定义的

时间:2018-02-02 13:16:49

标签: protractor

我正在使用带有量角器5.2.2的黄瓜3。我已将配置文件中的网址作为baseUrl:' https://www.linkedin.com'我必须检查点击登录后是否导航到主页。而不是传递URL" https://www.linkedin.com/feed" ,我已经过了#34; feed"并在我的step.js文件中给出了split方法。 我的功能文件在下面给出

When I enter "qwerty" in ".username"
And I enter "Passw0rd" in ".password"
And I click on ".login"
Then I should be at the "feed"

和我的step.js

Then(/^I should be at the "(.*)"$/, function (url) { 
var geturl=browser.getCurrentUrl() + '';
var res=geturl.split("/");
var result=res[1];
return expect(result).to.eventually.equal(url);
}); 

我的第四步失败并收到错误" TypeError:无法读取属性'然后'未定义"。我在我的step.js文件中出错了。或者当我在内页面上时,如何检查基本网址的剩余部分,其中网址格式为" base.com/feed /profile/profile-edit"。提前谢谢。

1 个答案:

答案 0 :(得分:1)

在下面内联解释您的代码问题。

Then(/^I should be at the "(.*)"$/, function (url) { 
    var geturl=browser.getCurrentUrl() + '';
    // browser.getCurrentUrl() return a pomise which it's also a javascript object
    // in javascript claculate `an object + ''`, will convert the object 
    // to string firstly by toString() function, 
    // if the object not overwrite the supper toString() function, 
    // it will return an string "[object Object]"
    // So geturl = "[object Object]"

    var res=geturl.split("/");
    // there is no "/" in geturl, so the array length of res is 1

    var result=res[1];
    // res[1] exceed array length, so result = undefined

    return expect(result).to.eventually.equal(url);
    // because you used eventually at here, so chai will regard result is a promise,
    // chai will check the argument of expect(xxx) is a promise or not 
    // by detect xxx has property: then via calling xxx.then in chai's inside code, 
    // but result is undefined, of course has no property: 'then'.
}); 

你必须在then()

中使用promise最终值
Then(/^I should be at the "(.*)"$/, function (url) { 
    return browser.getCurrentUrl().then(function(cururl){
        var parts = cururl.split("/");
        return expect(parts[parts.length-1]).to.equal(url);
        // dont't use eventually at here, because parts[parts.length-1] is not promise
    });
});