From 67f7a71c44312b6d37635f057c3f0f362c50bf64 Mon Sep 17 00:00:00 2001 From: Andrew Sipple Date: Wed, 6 Nov 2024 08:20:03 -1000 Subject: [PATCH 1/2] Use session cookie to check if the user is logged in --- login.js | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/login.js b/login.js index d40380a..eeb5c0a 100644 --- a/login.js +++ b/login.js @@ -3,32 +3,33 @@ // cy.login('username', 'password'); // login as a specific user. Cypress.Commands.add('login', (usernameParam = '', password = '') => { - cy.logout() - cy.visit('/user/login') - let username = ''; - let default_user = false; - if (usernameParam === '' && password === '') { - username = 'cypress'; - default_user = true; - } - else { - username = usernameParam; - } + cy.logout(); + cy.visit("/user/login"); + let username = ""; + let default_user = false; + if (usernameParam === "" && password === "") { + username = "cypress"; + default_user = true; + } else { + username = usernameParam; + } - if (default_user) { - cy.get('form.user-login-form #edit-name').type(username); - cy.get('form.user-login-form #edit-pass').type('cypress'); - } - else { - cy.get('form.user-login-form #edit-name').type(username); - cy.get('form.user-login-form #edit-pass').type(password); - } + if (default_user) { + cy.get("form.user-login-form #edit-name").type(username); + cy.get("form.user-login-form #edit-pass").type("cypress"); + } else { + cy.get("form.user-login-form #edit-name").type(username); + cy.get("form.user-login-form #edit-pass").type(password); + } - // Login. - cy.get('form.user-login-form .form-submit').click(); - cy.wait(500) // Wait for the UI to catch up. + // Login. + cy.get("form.user-login-form .form-submit").click(); + cy.wait(500); // Wait for the UI to catch up. - // Check user page loads. - cy.get('main').contains(username) - cy.get('main').contains('Member for') + // Check user session cookie is set after logging in. + cy.getCookies() + .should("have.length", 1) + .then((cookies) => { + expect(cookies[0].name).to.include("SESS"); + }); }) From 961cce5356821e44be2cd9d5b513720cfbdefd5d Mon Sep 17 00:00:00 2001 From: Andrew Sipple Date: Wed, 6 Nov 2024 09:09:11 -1000 Subject: [PATCH 2/2] Don't assume the session cookie is always the first first cookie. --- login.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/login.js b/login.js index eeb5c0a..a000b07 100644 --- a/login.js +++ b/login.js @@ -27,9 +27,9 @@ Cypress.Commands.add('login', (usernameParam = '', password = '') => { cy.wait(500); // Wait for the UI to catch up. // Check user session cookie is set after logging in. - cy.getCookies() - .should("have.length", 1) - .then((cookies) => { - expect(cookies[0].name).to.include("SESS"); - }); + cy.getCookies().then((cookies) => { + // Search for cookie.name and value contains SESS. + const sessionCookie = cookies.find((cookie) => cookie.name.includes("SESS")); + expect(sessionCookie).to.exist; + }); })