Playwright – storageState and globalSetup not working properly with POM

I’m using Playwright to write my e2e tests. I have been using beforeEach hooks but I have decided that it would be better to use a global setup. It seems however that the storage state is not properly passed to the tests themselves or maybe my whole implementation is just wrong.

This is my playwright.config.ts:

import { defineConfig } from '@playwright/test';

export default defineConfig({
  globalSetup: "./global-setup.ts",
  testDir: './tests',
  retries: 2, // Set the number of retries here
  reporter: [['html'], ['list']],
  use: {
    baseURL: 'http://localhost:8080/velocity_builder',
    headless: true,
    screenshot: 'only-on-failure',
    storageState: './storageState.json'
  },
  workers: 4,
  timeout: 30000,
});

Here is my global-setup.ts:


import { expect, Browser, chromium, Page } from '@playwright/test';
import { BuilderPage } from './tests/BuilderPage';

async function globalSetup() {
const browser: Browser = await chromium.launch({ headless: false })
const context = await browser.newContext()
const page: Page = await context.newPage()
const builderPage = new BuilderPage(page); // initialize a new builder page object
await page.goto('http://localhost:8080'); // go to the builder
await builderPage.initializeBuilder(); // initialize the builder by clicking a blank applet

    // Ensure panels are visible
    await expect(builderPage.panels.document).toBeVisible();
    await expect(builderPage.panels.quickView).toBeVisible();
    await expect(builderPage.panels.properties).toBeVisible();
    
    // Ensure buttons are visible
    await expect(builderPage.saveButton).toBeVisible();
    await expect(builderPage.previewButton).toBeVisible();
    await expect(builderPage.automationButton).toBeVisible();
    await expect(builderPage.globalizationButton).toBeVisible();
    await expect(builderPage.addButton).toBeVisible();
    
    // Click on the add button and ensure toolbox panel is visible
    await builderPage.addButton.click();
    await expect(builderPage.panels.toolbox).toBeVisible();
    
    // Store session state (e.g., cookies) for further use in tests
    const storageState = await page.context().storageState({ path: 'storageState.json' });
    console.log(storageState)
    
    await browser.close()

}

export default globalSetup

And lastly this is the test I have been trying to run unsuccsessfully:


import { test, expect, Page } from '@playwright/test';
import { BuilderPage } from './BuilderPage';

test.describe.configure({ mode: 'parallel', retries: 4 }); // Runs the test in the file in parallel mode and retries them 4 times if they fail
test.describe('Element Style Tests', () => {
// let page: Page;
let builderPage: BuilderPage;
test('Check Header Style', async () => {
await builderPage.addElementToDocument('Header');
const isElementInQuickView = await builderPage.verifyElementInQuickView();
expect(isElementInQuickView).toBe(true);

        const types = [
            'Header (Branded)',
            'Header (Decorative)',
            'Section',
            'Header'
        ];
    
        for (let i = 0; i < types.length; i++) {
            await new Promise((resolve) => setTimeout(resolve, 500)); // You might want to replace this with a better synchronization method
            await builderPage.checkHeaderStyleChange(types[i]);
        }
    });

});

The global setup seems to be working fine as it successfully writes in the storageState.json:


{
"cookies": [],
"origins": [
{
"origin": "http://localhost:8080",
"localStorage": [
{
"name": "flowplayerTestStorage",
"value": "test"
}
]
}
]
}

And also when i run it in a headless mode i can see that the commands are working just fine. However when it gets to the test itself, this is what i get in the test results terminal:

Running global setup if any…
{
cookies: [],
origins: [ { origin: 'http://localhost:8080', localStorage: [Array] } ]
}

Running 1 test using 1 worker

x  1 testStyles.spec.ts:9:9 › Element Style Tests › Check Header Style (11ms)
x  2 testStyles.spec.ts:9:9 › Element Style Tests › Check Header Style (retry #1) (11ms)
x  3 testStyles.spec.ts:9:9 › Element Style Tests › Check Header Style (retry #2) (12ms)
x  4 testStyles.spec.ts:9:9 › Element Style Tests › Check Header Style (retry #3) (11ms)
x  5 testStyles.spec.ts:9:9 › Element Style Tests › Check Header Style (retry #4) (11ms)

1) testStyles.spec.ts:9:9 › Element Style Tests › Check Header Style ─────────────────────────────

    TypeError: Cannot read properties of undefined (reading 'addElementToDocument')
    
       8 |     let builderPage: BuilderPage;
       9 |     test('Check Header Style', async () => {
    > 10 |         await builderPage.addElementToDocument('Header');
         |                           ^
      11 |         const isElementInQuickView = await builderPage.verifyElementInQuickView();
      12 |         expect(isElementInQuickView).toBe(true);
      13 |
    
      ```

I would be happy if someone could help me understand what I am doing wrong.

3

You need to make an instance of builderPage first.

let builderPage: BuilderPage;

test('Check Header Style', async ({ page }) => {
    builderPage = new BuilderPage(page)
    await builderPage.addElementToDocument('Header');

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật