加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 06:47:09 所属栏目:安全 来源:网络整理
导读:我正在使用AngularJs和Angular-UI的ui-router创建SPA.现在我正在尝试创建身份验证逻辑. $rootScope.$on("$stateChangeStart",function (event,toState) { if(toState.authenticate !MainService.isAuthenticated()) { if($cookieStore.get('authToken')) { M
我正在使用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();
    }
});

在状态更改时,这将检查状态是否需要身份验证并在必要时转移到登录状态.此外,如果用户登录,则会阻止其进入登录状态.身份验证由存储在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");
});

});

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

Message:
 Expected 'http://localhost/#/login' to match '/main'.

堆栈跟踪:
?????错误:期望失败

解决方法

我碰到了另一个解决这个问题的问题.

基本上,您等待新页面中的元素出现而不是依赖量角器等待状态/页面完成加载.在撰写此答案时,量角器在ui-router满载的等待页面上仍然不可靠.量角器等待$timeout和$http完成.

official doc

因此,如果您使用websocket,它可能不会被覆盖(至少根据我的观察).

你需要使用的api是browser.wait

browser.wait(function() {
       return $('#test321').isPresent(); // keeps waiting until this statement resolves to true
      },timeToWaitInMilliseconds,'message to log to console if element is not present after that time'
    );

    expect($('#test321').isPresent()).toBe(true);

您可以在以下链接中找到详细信息
Protractor,AngularJS,Parse — Protractor does not wait for Angular to resolve

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读