AngularJs ui-router.量角器测试失败

and*_*s.k 5 angularjs angular-ui protractor

我正在使用AngularJs和Angular-UI的ui-router创建SPA.现在我正在尝试创建身份验证逻辑.

$rootScope.$on("$stateChangeStart", function (event, toState) {
    if(toState.authenticate && !MainService.isAuthenticated()) {
        if($cookieStore.get('authToken')) {
            MainService.loginWithToken($cookieStore.get('authToken'))
            .then(function() {
                $state.go(toState.name);
                event.preventDefault();
            });
        }

        $rootScope.requestPath = toState.name;
        $state.go('public.login');
        event.preventDefault();
    } 

    if(toState.url == '/login' && MainService.isAuthenticated()) {
        $state.go('private.main');
        event.preventDefault();
    }
});
Run Code Online (Sandbox Code Playgroud)

在状态更改时,这将检查状态是否需要身份验证并在必要时转移到登录状态.此外,如果用户登录,则会阻止其进入登录状态.身份验证由存储在cookie中的令牌完成.

这是我的量角器测试场景:

describe('Routes', function() {
it('Should go to the selected path if user logged in', function() {
    browser.get('/');
    expect(browser.getLocationAbsUrl()).toMatch("/login");

    browser.manage().addCookie("authToken", "aaa");

    browser.manage().getCookie("authToken").then(function(cookie) {
        expect(cookie.name).toBe('authToken');
        expect(cookie.value).toBe('aaa');
    });

    browser.get('/');
    expect(browser.getLocationAbsUrl()).toMatch("/main");

    browser.get('/#/main');
    expect(browser.getLocationAbsUrl()).toMatch("/main");

    /* This part fails, because, when the user is logged in, 
    he should be transfered to main state, if he is trying to reach the 
    login page. In this failing case, the user is able to reach the 
    /login even if he is  logged in. */

    browser.get('/#/login');
    expect(browser.getLocationAbsUrl()).toMatch("/main");

    browser.manage().deleteCookie("authToken");

    browser.get('/#/login');
    expect(browser.getLocationAbsUrl()).toMatch("/login");

    browser.get('/#/main');
    expect(browser.getLocationAbsUrl()).toMatch("/login");
});
Run Code Online (Sandbox Code Playgroud)

});

当我尝试自己模拟测试行为时,一切都很好,但是当我运行量角器时:

Message:
 Expected 'http://localhost/#/login' to match '/main'.
Run Code Online (Sandbox Code Playgroud)

Stacktrace:错误:期望失败

Cal*_*Cal 0

您可能需要等待页面加载:

browser.get('/#/main');
var ptor = protractor.getInstance();
ptor.waitForAngular();
expect(browser.getLocationAbsUrl()).toMatch("/main");
Run Code Online (Sandbox Code Playgroud)