停止ng2怠速进行量角器测试



我使用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(); });
};

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

这一切都很好,但当我去运行量角器测试时,它们会超时,我想是因为量角器一直在等待超时结束,这需要一段时间,因为我将ng2空闲设置为6小时!

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

如果我不使用Idle.watch()初始化,则运行测试。

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

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

ReferenceError:文档未定义

那么我如何才能在量角器配置文件中要求ng2空闲模块?

我无法为您提出的问题提供解决方案。但我将为您提供一种不同的方法来解决这个问题。在为我的团队设置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();
});
}
}

我使用browser.waitForAngularEnabled(false)并在beforeEachafterEachjasmine例程中禁用和启用它们取得了部分成功。但它们很脆弱,并不能适用于所有的测试。

编辑:我检查了ng2空闲问题跟踪器,发现这里有一个开放的缺陷

最新更新