protractor expect currenturl fails

Ian Richards picture Ian Richards · Aug 27, 2013 · Viewed 14.7k times · Source

I'm trying to get an e2e test running against my local server and test that the resulting url (after a navigational button has been clicked) is the correct result. However the resulting url is always false.

My code is shown below:

HTML:

//http://localhost/#/current_Page
<html>
    <head><title></title></head>
    <body>
    //should change the current url to
    //http://localhost/#/new_page
    <button class="button" ng-click="change_page()">Change Page</button>
</html>

TEST CODE:

var protractor = require('protractor');
require('protractor/jasminewd');

describe('Tests', function() {      
    var ptor;

    describe('Test 1', function() {
        var ptor = protractor.getInstance();
        ptor.get('#/current_page'); 
        it('change page and current url', function() {
            ptor.findElement(protractor.By.className('.button').click().then(function() {
                expect(ptor.currentUrl()).toContain('#/new_page');
            });
        });
    }, 30000);
});

The issue is the current url after clicking the button remains #/current_url and does not change to the expected result #/new_page.

Does anyone know where I have gone wrong?

Answer

Ian Richards picture Ian Richards · Aug 29, 2013

After search for the answer to this question I figured it out myself

The current url does not fail, I was not waiting for the promise to return to angular. The ammended code below shows where I had gone wrong

var protractor = require('protractor');
require('protractor/jasminewd');

describe('Tests', function() {      
var ptor;

describe('Test 1', function() {
    var ptor = protractor.getInstance();
    ptor.get('#/current_page'); 
        it('change page and current url', function() {
            ptor.findElement(protractor.By.className('.button').click().then(function() {
                ptor.waitForAngular();
                expect(ptor.currentUrl()).toContain('#/new_page');
            });
        });
    }, 30000);
});

This then waits for angular to route to the new page and update any bindings and then proceeds to check the expected result which is now what I would expect it to be.

Please be advised that this does not solve all issues relating to unexpected getCurrentUrl() results. if using driver.findElement() you may need to refer to JulieMR's answer to this question

I hope this helps someone stuck on this issue.