简体   繁体   中英

Cypress Testing Angular App Login Session Timeouts

I'm trying to set up Cypress to test a session timeout in an Angular app, using the Node Package Manager to install modules. Right now the session timeout is set to wait an hour. It would be easy to get a Cypress test to wait an hour, but I've been asked to help find a way to get a test to run faster. The session timeout is set as an hour currently, and is defined in a typescript file, specifically an app.module.ts. The Angular app is setup to use the 'angular-user-idle' module to count idle time before a timeout. This idle time is defined in an imports statement, using the UserIdleModule.forRoot({idle: X, timout: Y}) function, defined in the following structure. I have removed unrelated modules to hopefully make it more readable

@NgModule({
declarations: [
    Component Declarations 
  ],
  imports: [
    Other Modules
    UserIdleModule.forRoot({ idle: 3600, timeout: 5 }) // for session-exipred function, entries are in seconds
  ],
  providers: [
    AuthService,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptorService,
      multi: true
    },
    DatePipe
  ],
  bootstrap: [AppComponent] 

Really the summary of the question is, is there anyway to modify the UserIdleModule.forRoot({idle: X, timeout: Y}) function when running a Cypress test?

Not sure it's possible, probably is a bad idea, but I figured I'd get a second opinion before scraping the idea entirely. Sorry if my question is unclear.

Thanks!

Cypress provides cy.clock() and cy.tick() commands to allow timeframes to be compressed for testing.

It works based on controlling native browser setTimeout() and setInterval() functions.

Looking at UserIdleModule it uses ReactiveX (rxjs) which at it's core has setInterval() to do scheduling, so there's a reasonable chance this will work

cy.clock()
cy.visit('/')

// perform login

cy.tick(3600 * 1000)  // move clock by 1 hour (defined in ms)

// verify logged out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM