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?

4

3 回答 3

10

在搜索了这个问题的答案后,我自己想通了

当前的 url 没有失败,我没有等待返回 angular 的承诺。下面的修改后的代码显示了我出错的地方

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

然后等待 angular 路由到新页面并更新任何绑定,然后继续检查预期的结果,这就是我现在期望的结果。

请注意,这并不能解决与意外 getCurrentUrl() 结果相关的所有问题。如果使用 driver.findElement() 您可能需要参考 JulieMR 对这个问题的回答

我希望这有助于有人坚持这个问题。

于 2013-08-29T15:34:25.710 回答
2

在 Protractor 1.5.0protractor.getInstance();中不再工作了,所以你必须browser改用。

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);
});
于 2015-01-03T14:13:45.023 回答
0

您还可以编写自定义预期条件等待当前 url 等于所需的。此外,使用browserelement符号:

browser.get("#/current_page"); 

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

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

哪里urlChanged是:

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

或者,Protractor>= 4.0.0解决方案和urlContains预期条件:

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

var EC = protractor.ExpectedConditions;
browser.wait(EC.urlContains("#/new_page"), 5000);
于 2015-04-24T22:02:55.073 回答