Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

You can also write a custom expected condition to wait for current url to being equal a desired one. Besides, use browser and element notations:

browser.get("#/current_page"); 

it("change page and current url", function() {
    element(by.css(".button")).click();

    browser.wait(urlChanged("#/new_page")), 5000);
});

where urlChanged is:

var urlChanged = function(url) {
  return function () {
    return browser.getCurrentUrl().then(function(actualUrl) {
      return actualUrl.indexOf(url) >= 0;
    });
  };
};

Or, a Protractor>=4.0.0 solution and the urlContains expected condition:

element(by.css(".button")).click();

var EC = protractor.ExpectedConditions;
browser.wait(EC.urlContains("#/new_page"), 5000);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top