停止ng2-idle进行量角器测试

Mar*_*nny 5 protractor angular

我在一段时间后使用ng2-idle自动注销用户.我在我的appComponent构造函数中初始化它:

import {Idle, DEFAULT_INTERRUPTSOURCES} from '@ng-idle/core';

export class AppComponent {

    constructor(
      private idle:Idle) {

      idle.setIdle(21600);
      idle.setTimeout(1);
      idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);

      idle.onTimeout.subscribe(() => { this.logout(); });
    };
Run Code Online (Sandbox Code Playgroud)

成功登录后,我在LoginComponent中启动它this.idle.watch()(Idle被注入到构造函数中).

这一切都运行正常但是当我去运行我的量角器测试他们超时时,我认为因为量角器等待超时结束,这需要一段时间,因为我将ng2-idle设置为6小时!

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Run Code Online (Sandbox Code Playgroud)

如果我没有初始化使用Idle.watch()那么测试运行.

我想要做的是设置Idle.stop();onPrepare我的量角器配置文件块,并将其重置Idle.watch();后的测试中我的完整onComplete块.

我已经var idle = require('@ng-idle/core');在量角器conf文件中尝试过,但它会让我回想起以下内容:

ReferenceError:未定义文档

那么如何在protractors配置文件中要求ng2-idle模块呢?

san*_*ton 4

我无法为您提供您提出的问题的解决方案。但我将为您提供一种不同的方法来解决这个问题。在为我的团队设置 Protractor 时,我在使用 ng-idle 实施会话超时策略后遇到了同样的问题。在对如何解决这个问题感到心痛之后,我意识到 Angular zone API 可以用来解决这个问题。这是我的应用程序组件和解决方案。查看runOutsideAngular运行区域中的

请注意,所有状态更改都包含在 run 方法中。否则,更改检测将不起作用,并且包括倒计时器在内的所有状态字段都不会更新 UI。

该解决方案涉及修改使用 ng-idle 的应用程序,因为根据我目前的理解,这无法在 Protractor 中完成。

this.ngZone.runOutsideAngular

@Component( {
selector   : 'app-root',
templateUrl: './app.component.html',
styleUrls  : [ './app.component.scss' ]
} )

export class AppComponent implements OnInit, OnDestroy{

idleState = 'Not started.';
idleStateTitle = '';
idleStateBtnText = '';
timedOut = false;

@ViewChild( DialogComponent ) dialog: DialogComponent;

constructor( private idle: Idle,
             private locationStrategy: LocationStrategy,
             private authUtils: AuthUtils,
             private ngZone: NgZone ){

    this.ngZone.runOutsideAngular(() => {
        //Idle timer is set to 28 minutes and timeout count down timer will run for 2 minutes.
        idle.setIdle( 28 * 60 );
        idle.setTimeout( 120 );
    });


    //When idling starts display the dialog with count down timer
    idle.onIdleStart.subscribe( () =>{
        this.idleState = '';
        this.idleStateTitle = 'Your session is about to expire.';
        this.idleStateBtnText = 'Stay Logged In';
        this.ngZone.run(() => this.dialog.show());
    } );

    //User stopped idling
    idle.onIdleEnd.subscribe( () =>{
        this.idleState = 'No longer idle.';
    } );

    //Show timeout warning two minutes before expiry
    idle.onTimeoutWarning.subscribe( ( countdown ) =>{
        this.ngZone.run(() => { this.idleState = 'Your session will time out in ' + countdown + ' seconds! '});
    } );

    //Session Timed out
    idle.onTimeout.subscribe( () =>{
        this.ngZone.run( () =>{
            this.idleStateTitle = "Your session has expired.";
            this.idleState = 'To Continue, log back in';
            this.timedOut = true;
            this.idleStateBtnText = 'Log in Again';
        } );
    });
}

reset(){
    this.ngZone.runOutsideAngular(() => {
        this.idle.watch();
    });
    this.ngZone.run( () =>{
        this.idleState = 'Started.';
        this.idleStateTitle = "";
        this.idleStateBtnText = '';
        this.timedOut = false;
    });
}

title = 'Projects';

/**
 * <p>Refresh Token by getting user token from the user service. Also reset the idle state timer here.</p>
 */
refreshToken(){
    this.authUtils.refreshToken();
    this.reset();
}

/**
 * Handle timeout
 */
processTimeout(){
    this.dialog.hide( null );
    if( this.timedOut ){
        AuthUtils.invalidateSession();
        let origin = window.location.origin;
        window.location.href = origin + this.locationStrategy.getBaseHref() + "?_="+ (Math.floor(Math.random() * 10000));
    }
    else{
        this.refreshToken();
    }
}

ngOnInit(): void{
    this.reset();
}

ngOnDestroy(): void{
    this.ngZone.runOutsideAngular(() =>{
        this.idle.stop();
    });
}
}
Run Code Online (Sandbox Code Playgroud)

我在茉莉花例程browser.waitForAngularEnabled(false)中使用、禁用和启用它们取得了部分成功。但它们很不稳定,并且不适用于所有测试。beforeEachafterEach

编辑:我检查了 ng2-idle 问题跟踪器,发现这里存在一个未解决的缺陷