I'm trying to follow this article here to add cy.session() to my login function. However when I do this and then run my test, I get the error cy.session is not a function
I've set "experimentalSessionSupport": true in my cypress.json config file (and confirmed that my test is seeing this value as true too). From my understanding, that should be all I need to do, but obviously I'm missing something.
I'm using Cypress version 9.4.1. I've tried creating a custom login command instead (exactly as in the article) but just get the same error when running it.
My test works perfectly without the cy.session() command.
login.js:
class LoginPage {
...
login(username, password) {
cy.session([username, password], () => {
cy.visit('/login');
this.emailField.type(username);
this.passwordField.type(password);
this.loginButton.click();
});
}
}
export default new LoginPage();
cypress.json:
{
...
"experimentalSessionSupport": true
}
I'm using Cypress version 9.4.1.
Welp, this wasn't actually true. I was still on a version that didn't support cy.session(). Silly mistake on my end. It's now all working fine since upgrading Cypress.
Is it a Typescript error - TypeError: cy.[custom command] is not a function?
If so, the type definition is probably not added yet due to experimental flavour.
Add same definition as you would use for a custom command to the top of the test.
This is taken from the latest release.
declare namespace Cypress {
interface SessionOptions {
validate?: () => false | void
}
interface Chainable<Subject = any> {
session(
id: string | object,
setup?: SessionOptions['validate'],
options?: SessionOptions
): Chainable<null>
}
}