0
votes

I am using cucumber 3 with protractor 5.2.2. and i have given the url in my config file as baseUrl: 'https://www.linkedin.com' and i have to check whether navigated to the home page after clicking login.Instead of passing URL "https://www.linkedin.com/feed" ,i have pass "feed" and given split method in my step.js file. my feature file is given below

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

and in my 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);
}); 

My fourth step failed and getting error "TypeError: Cannot read property 'then' of undefined".where i am going wrong in my step.js file.Or how can i check the remaining part of base url when i am on inner page which having a url format of "base.com/feed/profile/profile-edit".Thanks in advance.

1
The method browser.getCurrentUrl() returns a promise, not a string. Thus you need to resolve it first with browser.getCurrentUrl().then(...) or await browser.getCurrentUrl() depending on the control flow you are using. You could also directly call expect(browser.getCurrentUrl()).to.eventually.equal(url); which will resolve the promise for you. - Florent B.
@FlorentB. this should be an answer - nilesh

1 Answers

1
votes

Explain your code issue inline at below.

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'.
}); 

You have to consume promise eventual value in then()

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
    });
});