1

I tried to have a one time authentication using session and use the same for all the tests in the spec file. While trying to run my test , sometimes i get this below error which im unable to underdstand or fix. Any help on this would be appreciated.

browser.newContext: Cookie should have a valid expires, only -1 or a positive number for the unix timestamp in seconds is allowed

at C:\Users\v.shivarama.krishnan\source\repos\PlaywrightDemo\node_modules\@playwright\test\lib\index.js:595:23
at Object.context [as fn] (C:\Users\v.shivarama.krishnan\source\repos\PlaywrightDemo\node_modules\@playwright\test\lib\index.js:642:15)

Spec.ts

import { chromium, test, expect } from "@playwright/test";

test.describe('Launch Browser', () => {

        await context.storageState({ path: 'storage/admin.json' });
        await page.goto('abc.com');
        await expect(page.locator('#ebiLink')).toBeVisible();

        const texts = await page.locator('#ebiLink').textContent();
        console.log("text of ebi is " + texts);

        await page.goto('abc.com');

        await expect(page.locator('text= Detailed Interfaces ')).toBeVisible();

        await page.waitForSelector('#searchTab');

        await page.waitForSelector('#InterfaceCard');

        await page.locator('#searchTab').type('VISHW-I7939');

        await page.locator("button[type='button']").click();

        await page.locator('#InterfaceCard').first().click();

        await expect(page.locator('#ngb-nav-0')).toBeVisible();

        const interfaceID = await page.locator("//span[@class='value-text']").first().allInnerTexts();

        console.log('interface id is' + interfaceID);

        const dp = await page.waitForSelector('role=listbox');

        await page.locator('role=listbox').click();

        const listcount = await page.locator('role=option').count();
        await page.locator('role=option').nth(1).click();

        await expect(page.locator('#ngb-nav-0')).toBeVisible();

    });

    test('Move to shells screen', async ({ page, context }) => {
      
        await context.storageState({ path: 'storage/admin.json' });
        await page.goto('abc.com');
        
        await expect(page.locator('#ListHeader')).toBeVisible();

        const shells = await page.locator('#ListHeader').textContent();

        console.log('Text of shells header is '+shells);

    });


});

global-setup.ts (for one time login and getting the session)

import { Browser, chromium, FullConfig } from '@playwright/test'

async function globalSetup(config: FullConfig) {
  const browser = await chromium.launch({
    headless: false
  });
  await saveStorage(browser, 'Admin', 'User', 'storage/admin.json')
  await browser.close()
}

async function saveStorage(browser: Browser, firstName: string, lastName: string, saveStoragePath: string) {
  const page = await browser.newPage()
  await page.goto('abc.com');
  await page.waitForSelector("//input[@type='email']", { state: 'visible' });
  await page.locator("//input[@type='email']").type('ABC@com');
  await page.locator("//input[@type='submit']").click();
  await page.locator("//input[@type='password']").type('(&^%');
  await page.locator("//input[@type='submit']").click();
  await page.locator('#idSIButton9').click();
  await page.context().storageState({ path: saveStoragePath })
}

export default globalSetup

Vishwa nathan
  • 11
  • 2
  • 4

2 Answers2

0
  1. Have you registered global-setup.ts script in the Playwright configuration file: like below?
// playwright.config.ts
import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
  globalSetup: require.resolve('./global-setup'),
};
export default config; 

again you don't have to write code to use the session-storage at each test level, you can use - use attribute of Playwright configuration as below:

// playwright.config.ts
import { PlaywrightTestConfig } from '@playwright/test';

const config: PlaywrightTestConfig = {
 globalSetup: require.resolve('./global-setup'),
 use: {
   // Tell all tests to load signed-in state from 'storageState.json'.
   storageState: 'storageState.json'
 }
};
export default config;

Seems like you are trying to use the same context in both tests, could that be a problem?

can you please try with isolated context and page for each tests? Also please check if it make sense to use session storage at test level instead of context-

test.use({ storageState: './storage/admin.json' })

Update about the tests-

General structure of tests would be -

test.describe('New Todo', () => {
     test('Test 1', async ({context, page }) => {});
     test('Test 2', async ({context, page }) => {});
});

Amol Chavan
  • 3,835
  • 1
  • 21
  • 32
  • 1
    Hi Amol . Thank you for responding . All the suggestions you had mentioned were added before itself, but still im getting the issue :( – Vishwa nathan Jun 19 '22 at 06:22
  • The tests run once successfully, if i re run then it fails with the error as given in the original question – Vishwa nathan Jun 19 '22 at 06:29
  • browser.newContext: Cookie should have a valid expires, only -1 or a positive number for the unix timestamp in seconds is allowed at C:\Users\v.shivarama.krishnan\source\repos\PlaywrightDemo\node_modules\@playwright\test\lib\index.js:595:23 at Object.context [as fn] (C:\Users\v.shivarama.krishnan\source\repos\PlaywrightDemo\node_modules\@playwright\test\lib\index.js:642:15) – Vishwa nathan Jun 19 '22 at 08:11
  • Thanks for clearing that up, are you sure your describe block is in correct format? I'll update the answer as comment wont let me do that – Amol Chavan Jun 21 '22 at 05:23
  • Thanks for the inputs again . I have made all the changes as suggested, but still same error :( Im really stuck here and not able to move forward :( – Vishwa nathan Jun 21 '22 at 09:21
  • if you really using public domain, can you share the github repo? – Amol Chavan Jun 21 '22 at 12:50
  • Actually its not a public domain. I tried the sol you had provided and it works for other public site (in my case browserstack.com).But when i try to use it for our site, which is not public btw , throws the error as in the question :( – Vishwa nathan Jun 22 '22 at 07:04
  • It's likely that issue is with cookies that you are creating/storing, can you provide url cookies here- make sure to strip sensitive information – Amol Chavan Jun 22 '22 at 17:01
  • Thank you .. Sure i will .. can i send you a DM with the details – Vishwa nathan Jun 24 '22 at 08:36
  • Yes, please - https://www.linkedin.com/in/4m01/ – Amol Chavan Jun 24 '22 at 12:38
0

I looked into the source code of playwright and found these two lines which show the error message you see.

assert(!(c.expires && c.expires < 0 && c.expires !== -1), 'Cookie should have a valid expires, only -1 or a positive number for the unix timestamp in seconds is allowed');
assert(!(c.expires && c.expires > 0 && c.expires > kMaxCookieExpiresDateInSeconds), 'Cookie should have a valid expires, only -1 or a positive number for the unix timestamp in seconds is allowed');

The kMaxCookieExpiresDateInSeconds is defined as 253402300799. So basically the cookie that you captured could breach one of above rules. In my case it's that the expiry of a cookie is greater than this figure :).

refer to source code - https://github.com/microsoft/playwright/blob/5fd6ce4de0ece202690875595aa8ea18e91d2326/packages/playwright-core/src/server/network.ts#L53

Ming
  • 1