Codeceptjs: How to store the cookie from logged in session?

Created on 31 Oct 2018  路  4Comments  路  Source: codeceptjs/CodeceptJS

Hello, I am using codeceptjs with webdriverio.

I am trying to achieve the same thing which codeception(php) provides with saveSession method.

this is my code:

async quicklyLogin() {
            const I = this;

            I.amOnPage('http://somepage.com');
            if (this.cookie) {
                I.setCookie(this.cookie);
                I.refreshPage();
            }
            else {
                I.fillField(loginUsername, 'name');
                I.fillField(loginPassword, 'pass');
                I.click(login.clickLogin);
                this.cookie = await I.grabCookie('PHPSESSID');
            }
        }

The goal is to be able to use this quicklyLogin() method inside all other tests, so I skip the initial login step due to performance.
But the cookie is automatically deleted when the process ends.

Most helpful comment

Hi, I had a similar issue with puppeteer, when another Scenario starts it was cleaning the session, there is a setting to keep the session values: keepBrowserState => change to true

https://codecept.io/helpers/Puppeteer/

"helpers": {
    "Puppeteer": {
      "url": "http://url",
      "show": true,
      "restart": false,
      "keepBrowserState": true,
      "chrome": {
        "args": [
          "--no-sandbox"
        ]
      }
    }
  },

Hope it helps you!

All 4 comments

Did you set the keepCookies to true?

keepCookies: (optional, default: false) - keep cookies between tests when restart set to false.

Yes. I set it in codecept.json, and also directly in the WebDriverIO defaults.. It keeps failing the same. Also I see that if in codecept.json I set "restart": false - should take care of the cookies, but still nothing is happening

Hi, I had a similar issue with puppeteer, when another Scenario starts it was cleaning the session, there is a setting to keep the session values: keepBrowserState => change to true

https://codecept.io/helpers/Puppeteer/

"helpers": {
    "Puppeteer": {
      "url": "http://url",
      "show": true,
      "restart": false,
      "keepBrowserState": true,
      "chrome": {
        "args": [
          "--no-sandbox"
        ]
      }
    }
  },

Hope it helps you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

prashantsail picture prashantsail  路  4Comments

ecampii picture ecampii  路  3Comments

DavertMik picture DavertMik  路  3Comments

lets-open-source picture lets-open-source  路  4Comments

bionicles picture bionicles  路  3Comments