Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

we have Playwright Typescript fixtures like below: export const adminTest =…

  1. User

    we have Playwright Typescript fixtures like below: export const adminTest = baseTest.extend<{}, { workerStorageState: string }>({ storageState: ({ workerStorageState }, use) => use(workerStorageState), workerStorageState: [async ({ browser }, use) => { if (!checkEnvVars()) { adminTest.skip(); } const id = adminTest.info().parallelIndex; const adminFileName = path.resolve(adminTest.info().project.outputDir, ../state/state.admin.${id}.json); if (fs.existsSync(adminFileName)) { await use(adminFileName); return; } let page = await browser.newPage({ storageState: undefined }); let adminlogin = new LoginPage(page); await adminlogin.loginWithAdminCredentials(); await page.context().storageState({ path: adminFileName }); await use(adminFileName); await page.context().close(); await page.close(); fs.unlinkSync(adminFileName); }, { scope: 'worker' }], }); export const singleProjectUserTest = baseTest.extend<{}, { workerStorageState: string }>({ storageState: ({ workerStorageState }, use) => use(workerStorageState), workerStorageState: [async ({ browser }, use) => { if (!checkEnvVars()) { singleProjectUserTest.skip(); } const id = singleProjectUserTest.info().parallelIndex; const singleProjectUserFileName = path.resolve(singleProjectUserTest.info().project.outputDir, ../state/state.single.project.user.${id}.json); if (fs.existsSync(singleProjectUserFileName)) { await use(singleProjectUserFileName); return; } let page = await browser.newPage({ storageState: undefined }); let singleProjectUserlogin = new LoginPage(page); await singleProjectUserlogin.loginWithSingleProjectUserCredentials(); await page.context().storageState({ path: singleProjectUserFileName }); await use(singleProjectUserFileName); await page.context().close(); await page.close(); fs.unlinkSync(singleProjectUserFileName); }, { scope: 'worker' }], }); and test file like below: import { adminTest, singleProjectUserTest, adminWorkerStorageState, singleProjectUserWorkerStorageState } from '../fixtures/fixtures'; import { test } from '@playwright/test'; import { AttachmentsPage } from '../pages/attachments.page'; test.describe.configure({ mode: 'serial' }); test('Attachments test for Admin and Single Project User', async ({ browser }) => { // adminContext and all pages inside, including adminPage, are signed in as "admin". const adminContext = await browser.newContext({ storageState: String(adminWorkerStorageState) }); const adminPage = await adminContext.newPage(); // userContext and all pages inside, including userPage, are signed in as "user". const singleProjectUserContext = await browser.newContext({ storageState: String(singleProjectUserWorkerStorageState) }); const singleProjectUserPage = await singleProjectUserContext.newPage(); // ... interact with both adminPage and userPage ... let attachmentsPage = new AttachmentsPage(adminPage); await attachmentsPage.selectProjectB(); await attachmentsPage.nevigateNewRecordPage(); await attachmentsPage.createNewRecord(); await attachmentsPage.navigateToAttachmentPage(); await attachmentsPage.assignAttachmentToMe(); await adminPage.close(); let singleProjectUserAttachmentsPage = new AttachmentsPage(singleProjectUserPage); await singleProjectUserAttachmentsPage.navigateToRecordsPageAsSingleProjectUser(); await singleProjectUserAttachmentsPage.findRecordInRecordsList(); await singleProjectUserAttachmentsPage.navigateToAttachmentPage(); await singleProjectUserAttachmentsPage.verifyAttachmentIsReadOnly(); await singleProjectUserPage.close(); singleProjectUserAttachmentsPage = new AttachmentsPage(singleProjectUserPage); await singleProjectUserAttachmentsPage.navigateToRecordsPageAsSingleProjectUser(); await singleProjectUserAttachmentsPage.findRecordInRecordsList(); await singleProjectUserAttachmentsPage.navigateToAttachmentPage(); await singleProjectUserAttachmentsPage.assignAttachmentToMe(); await singleProjectUserAttachmentsPage.verifyAttachmentIsEditable(); await singleProjectUserAttachmentsPage.verifyAttachmentHistory(); await singleProjectUserPage.close(); await adminContext.close(); await singleProjectUserContext.close(); }); how to inherit test in the scope of test file from both adminTest and singleProjectUserTest fixtures?

  2. Assistant

    To inherit test cases from both adminTest and singleProjectUserTest fixtures in the scope of the test file, you can use the toHaveFixtures function provided by Playwright Test. Here's an example of how you can do it:

    // Import the fixtures and functions
    import {
      adminTest,
      singleProjectUserTest,
      adminWorkerStorageState,
      singleProjectUserWorkerStorageState,
      test,
      expect,
    } from '@playwright/test';
    
    // Import the required pages
    import { AttachmentsPage } from '../pages/attachments.page';
    
    test.describe.configure({ mode: 'serial' });
    
    /**
     * Test case from adminTest
     */
    adminTest('Attachments test for Admin', async ({ browser }) => {
      const adminContext = await browser.newContext({ storageState: String(adminWorkerStorageState) });
      const adminPage = await adminContext.newPage();
    
      // ... Interact with adminPage ...
    });
    
    /**
     * Test case from singleProjectUserTest
     */
    singleProjectUserTest('Attachments test for Single Project User', async ({ browser }) => {
      const singleProjectUserContext = await browser.newContext({ storageState: String(singleProjectUserWorkerStorageState) });
      const singleProjectUserPage = await singleProjectUserContext.newPage();
    
      // ... Interact with singleProjectUserPage ...
    });
    
    // Inheriting test cases from fixtures in the current test file
    test('Attachments test for Admin and Single Project User', async ({ browser }) => {
      // Inheriting test cases from adminTest fixture
      adminTest.sampleTests({ browser });
    
      // Inheriting test cases from singleProjectUserTest fixture
      singleProjectUserTest.sampleTests({ browser });
    
      // Your additional test code
    });
    

    In the example above, we import the necessary fixtures and functions from Playwright Test. Then, we define separate test cases for the adminTest and singleProjectUserTest fixtures. Finally, we use the adminTest.sampleTests() and singleProjectUserTest.sampleTests() functions to inherit the test cases defined in the fixtures.

  3. User

    we have fixtures.ts file with such a content which contains fixtures:

    import { test as baseTest, type Page, PlaywrightTestArgs, PlaywrightTestOptions } from '@playwright/test'; import { LoginPage } from '../pages/login.page'; import fs from 'fs'; import path from 'path';

    const requiredEnvVars = [ process.env.TEST_ADMIN_USERNAME, process.env.TEST_ADMIN_PASSWORD, process.env.TEST_SINGLE_PROJECT_USERNAME, process.env.TEST_SINGLE_PROJECT_PASSWORD, process.env.TEST_PROJECT_1, process.env.TEST_PROJECT_2 ];

    const checkEnvVars = () => { for (const envVar of requiredEnvVars) { if (envVar == undefined) { return false; } } return true; };

    export * from '@playwright/test'; export const adminTest = baseTest.extend<{}, { workerStorageState: string }>({ // Use the same storage state for all tests in this worker. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker storageState: ({ workerStorageState }, use) => use(workerStorageState), // Authenticate once per worker with a worker-scoped fixture. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
    workerStorageState: [async ({ browser }, use) => { if (!checkEnvVars()) { adminTest.skip(); } // Use parallelIndex as a unique identifier for each worker. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker const id = adminTest.info().parallelIndex; const adminFileName = path.resolve(adminTest.info().project.outputDir, ../state/state.admin.${id}.json);

    if (fs.existsSync(adminFileName)) {
        // Reuse existing authentication state if any. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
        await use(adminFileName);
        return;
    }
    
    // Important: make sure we authenticate in a clean environment by unsetting storage state. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
    let page = await browser.newPage({ storageState: undefined });
    // Perform authentication steps. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
    let adminlogin = new LoginPage(page);
    await adminlogin.loginWithAdminCredentials();
    // End of authentication steps. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
    await page.context().storageState({ path: adminFileName });
    await use(adminFileName);
    await page.context().close();
    await page.close();
    fs.unlinkSync(adminFileName); 
    

    }, { scope: 'worker' }], });

    export const singleProjectUserTest = baseTest.extend<{}, { workerStorageState: string }>({ // Use the same storage state for all tests in this worker. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker storageState: ({ workerStorageState }, use) => use(workerStorageState), // Authenticate once per worker with a worker-scoped fixture. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
    workerStorageState: [async ({ browser }, use) => { if (!checkEnvVars()) { singleProjectUserTest.skip(); } // Use parallelIndex as a unique identifier for each worker. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker const id = singleProjectUserTest.info().parallelIndex; const singleProjectUserFileName = path.resolve(singleProjectUserTest.info().project.outputDir, ../state/state.single.project.user.${id}.json);

    if (fs.existsSync(singleProjectUserFileName)) {
        // Reuse existing authentication state if any. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
        await use(singleProjectUserFileName);
        return;
    }
    // Important: make sure we authenticate in a clean environment by unsetting storage state. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
    let page = await browser.newPage({ storageState: undefined });
    // Perform authentication steps. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
    let singleProjectUserlogin = new LoginPage(page);
    await singleProjectUserlogin.loginWithSingleProjectUserCredentials();
    // End of authentication steps. https://playwright.dev/docs/next/auth#moderate-one-account-per-parallel-worker
    await page.context().storageState({ path: singleProjectUserFileName });
    await use(singleProjectUserFileName);
    await page.context().close();
    await page.close();
    fs.unlinkSync(singleProjectUserFileName);
    

    }, { scope: 'worker' }], });

    we also have the attachments.test.ts file with a single test function:

    import { adminTest, singleProjectUserTest, adminWorkerStorageState, singleProjectUserWorkerStorageState } from '../fixtures/fixtures'; import { test } from '@playwright/test'; import { AttachmentsPage } from '../pages/attachments.page';

    test.describe.configure({ mode: 'serial' });

    test('Attachments test for Admin and Single Project User', async ({ browser }) => { const adminContext = await browser.newContext({ storageState: String(adminWorkerStorageState) }); const adminPage = await adminContext.newPage();

    let attachmentsPage = new AttachmentsPage(adminPage); await attachmentsPage.selectProjectB(); await attachmentsPage.nevigateNewRecordPage(); await attachmentsPage.createNewRecord(); await attachmentsPage.navigateToAttachmentPage(); await attachmentsPage.assignAttachmentToMe(); await adminPage.close(); await adminContext.close();

    const singleProjectUserContext = await browser.newContext({ storageState: String(singleProjectUserWorkerStorageState) }); const singleProjectUserPage = await singleProjectUserContext.newPage();

    let singleProjectUserAttachmentsPage = new AttachmentsPage(singleProjectUserPage); await singleProjectUserAttachmentsPage.navigateToRecordsPageAsSingleProjectUser(); await singleProjectUserAttachmentsPage.findRecordInRecordsList(); await singleProjectUserAttachmentsPage.navigateToAttachmentPage(); await singleProjectUserAttachmentsPage.verifyAttachmentIsReadOnly(); await singleProjectUserPage.close(); await singleProjectUserContext.close();

    singleProjectUserAttachmentsPage = new AttachmentsPage(singleProjectUserPage); await singleProjectUserAttachmentsPage.navigateToRecordsPageAsSingleProjectUser(); await singleProjectUserAttachmentsPage.findRecordInRecordsList(); await singleProjectUserAttachmentsPage.navigateToAttachmentPage(); await singleProjectUserAttachmentsPage.assignAttachmentToMe(); await singleProjectUserAttachmentsPage.verifyAttachmentIsEditable(); await singleProjectUserAttachmentsPage.verifyAttachmentHistory(); await singleProjectUserPage.close(); await singleProjectUserContext.close(); });

    we need modify test function in the scope of the attachments.test.ts file in a way where we inherits adminTest and singleProjectUserTest fixtures form fixture.ts file to manage both admin and singleProjectUser roles and switch between ther storage state. According to the test steps the test should first sign in as admin user then performs steps to create new record with uploaded attachment, assign an attachment to himself. Then the test should switch and sign in as a singleProjectUser, find the record created by admin user before, navigate to the attachment of this record and check that he cannot update the attachment. And the last the test should again sign in as a singleProjectUser, find the record created by admin user before, navigate to the attachment of this record, assign it to himself and check that the attachments is editable according to the test steps described in a list of calling functions from the AttachmentsPage

  4. Assistant

    To modify the test function in attachments.test.ts to inherit adminTest and singleProjectUserTest fixtures and manage both admin and singleProjectUser roles, you can follow the steps below:

    1. Import the necessary modules and fixtures:
    import { adminTest, singleProjectUserTest, test, expect } from '../fixtures/fixtures';
    import { AttachmentsPage } from '../pages/attachments.page';
    
    1. Update the test function to use the inherited fixtures and manage the roles and storage state switching:
    test('Attachments test for Admin and Single Project User', async ({ browser }) => {
      // Sign in as admin user
      await adminTest(async ({ browser }) => {
        const adminContext = await browser.newContext({ storageState: String(adminWorkerStorageState) });
        const adminPage = await adminContext.newPage();
    
        let attachmentsPage = new AttachmentsPage(adminPage);
        await attachmentsPage.selectProjectB();
        await attachmentsPage.nevigateNewRecordPage();
        await attachmentsPage.createNewRecord();
        await attachmentsPage.navigateToAttachmentPage();
        await attachmentsPage.assignAttachmentToMe();
        await adminPage.close();
        await adminContext.close();
      });
    
      // Sign in as singleProjectUser and check attachment read-only
      await singleProjectUserTest(async ({ browser }) => {
        const singleProjectUserContext = await browser.newContext({ storageState: String(singleProjectUserWorkerStorageState) });
        const singleProjectUserPage = await singleProjectUserContext.newPage();
    
        let singleProjectUserAttachmentsPage = new AttachmentsPage(singleProjectUserPage);
        await singleProjectUserAttachmentsPage.navigateToRecordsPageAsSingleProjectUser();
        await singleProjectUserAttachmentsPage.findRecordInRecordsList();
        await singleProjectUserAttachmentsPage.navigateToAttachmentPage();
        await singleProjectUserAttachmentsPage.verifyAttachmentIsReadOnly();
        await singleProjectUserPage.close();
        await singleProjectUserContext.close();
      });
    
      // Sign in as singleProjectUser, assign attachment, and check attachment history
      await singleProjectUserTest(async ({ browser }) => {
        const singleProjectUserContext = await browser.newContext({ storageState: String(singleProjectUserWorkerStorageState) });
        const singleProjectUserPage = await singleProjectUserContext.newPage();
    
        let singleProjectUserAttachmentsPage = new AttachmentsPage(singleProjectUserPage);
        await singleProjectUserAttachmentsPage.navigateToRecordsPageAsSingleProjectUser();
        await singleProjectUserAttachmentsPage.findRecordInRecordsList();
        await singleProjectUserAttachmentsPage.navigateToAttachmentPage();
        await singleProjectUserAttachmentsPage.assignAttachmentToMe();
        await singleProjectUserAttachmentsPage.verifyAttachmentIsEditable();
        await singleProjectUserAttachmentsPage.verifyAttachmentHistory();
        await singleProjectUserPage.close();
        await singleProjectUserContext.close();
      });
    });
    

    With these changes, the test function will first sign in as the admin user and perform the necessary steps. Then, it will switch to the singleProjectUser role and check the attachment's read-only state. Finally, it will switch to the singleProjectUser role again and perform the steps to assign the attachment and check its editability and history.

    Note: Make sure to import the required modules and fixtures correctly, and adjust any paths and function names according to your project structure and requirements.