0

I'm having trouble with safariDriver running tests with Protractor. everything works fine on chrome and phantom.

so the problem is lets say i have a page i click on a link and it reroutes me to another page where it does something and then routes me back to where i was going.

so in chrome i do click on link and then get the url. url === expected url.

in safari though

click on link get url url === rerouted url and hasn't reached where i was going yet. i've tried doing browser.wait(function(){ return browser.getCurrentUrl}) doesn't work.

heres a snippet.

waitAndGetUrl: function(){
  return  browser.wait(function(){
        return browser.getCurrentUrl().then(function(url){
                 return url 
               });
    }, 1000)
}

describe("should click on link and add cookie", function(){
    it('click the fragment link', function(){
        testPageActions.clickOnFragmentLink();
        expect(testPageActions.waitAndGetUrl()).toContain(fragmentPath);
    });

    it('should grab have the cookie set', function(){
        expect(testPageActions.getCookie()).toBe(Cookie);
    });

});

ps i tried doing browser.sleep() as well

Chris Kim
  • 1
  • 1

3 Answers3

1

I think your own answer isn't correct.

  1. Sleep doesn't have a function, see the docs. I think it will now work because you are just waiting 1 second.
  2. Secondly you are using an expect in the comparison. When an expect fails it will reject a promise and will kill you test for the wrong reason.

With you initial question and code you almost had the answer. You only need to implement it like this

waitAndGetUrl: function(expectedUrl){    
    return browser.wait(function(){
        return browser.getCurrentUrl().then(function(currentUrl){
            return expectedUrl === currentUrl;
        });
    }, 3000);
}

The wait waits for a condition to become true. In this case you want to wait until the currentUrl is equal to the expectedUrl. If not it will executed the getCurrentUrl() for the max timeout you have given to become true. If the max timeout you gave is over it will fail. See the docs for more information and adding a custom timeout message.

wswebcreation
  • 2,365
  • 2
  • 10
  • 18
  • After reading your comment i went back and ran the test with a failing test and still passed so you are right. i went back and used the .wait and everything works great – Chris Kim May 11 '17 at 15:36
0

We need to know the output/error you are getting to help better, but try resolving the promise:

it('click the fragment link', function(){
    testPageActions.clickOnFragmentLink();
    testPageActions.waitAndGetUrl().then(function(url) {
        expect(url).toContain(fragmentPath);
    };
});

And if clickOnFragmentLink() returns a promise, you must do the same thing there also.

nugenjs
  • 155
  • 2
  • 10
  • Thanks I'll try that so when we click on a link we have a script that takes a user to our site and then brings them back to where they we're going. when i do get the url its still stuck in our address and not the where they we're going. so google.com/ ---> oursite.com/ ----> google.com/search?whatever in chrome get url gives me google.com/search?whatever safari gives me oursite.com/ i'll try your solution though – Chris Kim May 05 '17 at 16:22
  • Alternatively, you can use `expect(testPageActions.waitAndGetUrl()).to.eventually.be(fragmentPath);` – nugenjs May 05 '17 at 16:23
  • I've tried this. looks like my version of jasmine doesn't know what .to.eventually.be means. i guess i will test a bunch of things and see if i can come up with more information. – Chris Kim May 05 '17 at 19:31
-1

Hey guys thanks you guys gave me good ideas. Basically used the first answer but used .sleep instead of wait

describe("should click on link and add cookie", function(){
    it('click the fragment link', function(){
        var link = element(by.css(fragmentLinkCss));
        link.click().then(function(){
            browser.sleep(function(){
                browser.getCurrentUrl().then(function(url){
                    expect(url).toContain(fragmentPath);
                })
            }, 1000)
        });
    });

    it('should grab have the cookie set', function(){
        expect(testPageActions.getCookie()).toBe(Cookie);
    });
});
Chris Kim
  • 1
  • 1