Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protractor expect currenturl fails

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?

like image 931
Ian Richards Avatar asked Aug 27 '13 13:08

Ian Richards


2 Answers

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.

like image 130
Ian Richards Avatar answered Nov 08 '22 12:11

Ian Richards


In Protractor 1.5.0 protractor.getInstance(); isn't working anymore, so you have to use browser instead.

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

describe('Tests', function() {      

describe('Test 1', function() {
    browser.get('#/current_page'); 
        it('change page and current url', function() {
            ptor.findElement(protractor.By.className('.button').click().then(function() {
                browser.waitForAngular();
                expect(browser.getCurrentUrl()).toContain('#/new_page');
            });
        });
    }, 30000);
});
like image 43
Michael K. Avatar answered Nov 08 '22 13:11

Michael K.