cypress-documentation icon indicating copy to clipboard operation
cypress-documentation copied to clipboard

[sessions] More complete documentation for cy.session() and Cypress.session API

Open alexniculae opened this issue 3 years ago • 2 comments

What would you like?

I need to access all the IDs of the sessions created using cy.session() in my tests.

As this is already being done in the "time travel" logs, I assume this is somehow accessible, but I could not find a way to do it.

Could you please share any input on this and update the documentation surrounding this feature?

Why is this needed?

To be able to have a complete log of the sessions created for each spec (when triggered via cypress,run or for the entire run when triggered via cypress.open) and gain access to these sessions in order to be able to manage/manipulate them.

Thank you

alexniculae avatar Jan 26 '22 16:01 alexniculae

Some quick digging in the code and this was easy enough to find.

For reference to anyone looking for this, you can get all active sessions by using the function

cy.state('activeSessions')

Afterwards you can easily play with it to retrieve the data that you want:

// get the ids of your sessions using the activeSession keys
Object.keys(cy.state('activeSessions'))

or better

// get the ids of your sessions using the activeSession values - id key
    Object.values(cy.state('activeSessions')).map((sessions) => sessions.id)

or anything else you might be needing.

Note that if you are using Typescript, you will need to @ts-ignore the cy.state() function, as this is not exposed.

alexniculae avatar Jan 27 '22 08:01 alexniculae

Could we please have this available for use in the code and not have it hidden? Afterwards it would be great to also have it documented, which I guess is done for anything that is exposed.

Thank you.

alexniculae avatar Jan 27 '22 08:01 alexniculae