Stop ng2 downtime for protractor tests

I am using ng2-idle to automatically log out after a while. I initialize it in my appComponent constructor:

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

After a successful login, I ran it in my LoginComponent using this.idle.watch()(Idle is injected into the constructor).

All this works fine, but when I go to run my protractor tests, they time out, I think, because the protractor has to wait for the timeout to take, which will take some time, since I set ng2-idle for 6 hours!

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

If I do not initialize the use Idle.watch(), then the tests are executed.

, Idle.stop(); onPrepare protractor reset Idle.watch(); onComplete.

var idle = require('@ng-idle/core'); confractor conf, :

ReferenceError:

, ng2-idle ?

+4
1

. . Protractor - ng-idle. , , , API- Angular. appcomponent . . runOutsideAngular .

, run. , , , , .

, 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), beforeEach afterEach jasmine. .

: ng2-idle issue tracker ,

+3

Source: https://habr.com/ru/post/1666596/


All Articles