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

时间:2017-01-12 11:17:03

标签: angular protractor

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

this.idle.watch()

成功登录后,我使用Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. 在我的LoginComponent中启动它(Idle被注入到构造函数中)。

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

Idle.watch()

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

我想要做的是在我的量角器配置文件中的onPrepare块中设置Idle.watch();,并在我的onComplete中完成测试后将其重置为var idle = require('@ng-idle/core');块。

我在量角器配置文件中尝试了 Dim connectionString As String = String.Format("Data Source=.\SQLEXPRESS;AttachDbFilename={0}\Word.mdf;Integrated Security=False;Connect Timeout=30;User Instance=True", My.Application.Info.DirectoryPath) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim sql As String = "SELECT * FROM test" Dim connection As New SqlConnection(connectionString) Dim dataadapter As New SqlDataAdapter(sql, connection) Dim ds As New DataSet() connection.Open() dataadapter.Fill(ds, "test_table") connection.Close() DataGridView1.DataSource = ds DataGridView1.DataMember = "test_table" MakePivot() connection.Close() DataGridView2.ClearSelection() End Sub ,但它让我回过头来看:

  

ReferenceError:未定义文档

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

1 个答案:

答案 0 :(得分:4)

我无法为您提出的问题提供解决方案。但我会为您提供一种解决此问题的不同方法。在为我的团队设置Protractor时,在使用ng-idle实现会话超时策略后遇到了同样的问题。经过一番讨论如何解决这个问题,我意识到可以使用Angular zone API来解决这个问题。这是我的appcomponent与解决方案。 请参阅区域中的 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)取得了部分成功,并在beforeEachafterEach jasmine例程中禁用并启用了它们。但是它们很脆弱,并不能用于所有测试。

编辑:我检查了ng2-idle问题跟踪器,发现此here

存在漏洞