protractor expect currenturl fails
Asked Answered
E

3

13

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?

Encumber answered 27/8, 2013 at 13:41 Comment(0)
E
10

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.

Encumber answered 29/8, 2013 at 15:34 Comment(1)
In current versions it needs to be ptor.getCurrentUrl(). Anything else is fine.Dulcy
B
2

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);
});
Bibliography answered 3/1, 2015 at 14:13 Comment(0)
D
0

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);
Dyewood answered 24/4, 2015 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.