javascript - How to share an object in playwright tests - Stack Overflow

admin2025-04-17  3

I've a question on sharing the object between tests when a particular test fails. From the below code, when the "Check Bank account status" fails, PW is unable to successfully run other tests beneath it. I am assuming this is because bankAcctDetails object is not being properly shared between tests. My requirement is, even if the test for checking the status fails, I should be successfully run other tests down the line.

import { test as baseTest, expect } from '@playwright/test';
import { fileURLToPath } from 'url';
import fs from 'fs/promises';
import path from 'path';

import { BankAccountPage } from '../../pages/bankAccount';
import { CommonActions } from '../../pages/commonMethods';
import { LoginPage } from '../../pages/login';
import { BASE_URL } from '../../../config';

class BankAcctDetails {
  constructor(bankAcctId, 
    acctNumber, acctName, acctType, cashMgmtFlag, agentBank, currency, entity, txnType,
    cutOffTime, cutOffTimezone, cutOffOffset, aliases, secondAliases, status) {
    this.bankAcctId = bankAcctId;
   }
}
test.describe('Bank Account Test Set', () => {
  test.use({ 
    timeout: 90000,
    actionTimeout: 20000,
    navigationTimeout: 120000,
    ignoreHTTPSErrors: true });
  let page;
  let browser;
  let bankAcctPage;
  let commonActions;
  let loginPage;
  let context;

  test.beforeAll(async ({ browser }) => {
     const startTime = Date.now();
    console.log("Running Bank account test suite");
    const authFilePath = process.env.AUTH_FILE_PATH || path.resolve(__dirname, '..', '..', '..', 'LoginAuth.json');
    console.log(`authFilePath from spec file: ${authFilePath}`);

    try {
      await fs.access(authFilePath);
      console.log('Auth file exists');
      const contents = await fs.readFile(authFilePath, 'utf8');
    } catch (error) {
      console.log('Auth file does not exist or is not accessible');
  }
context = await browser.newContext({ storageState: authFilePath });
page = await context.newPage();
    bankAcctPage = new BankAccountPage(page);
    commonActions = new CommonActions(page);
    loginPage = new LoginPage(page);
    await page.goto(BASE_URL, { timeout: 120000, waitUntil: 'domcontentloaded'});
    console.log("Bank Account:Navigation to UAT successful");
    console.log('Page loaded in: ' + (Date.now() - startTime) + 'ms');
    await loginPage.loginForStaticDataClient();
    await commonActions.clickAdminLink();
    await bankAcctPage.clickBankAccountsLink();
    await bankAcctPage.clickBankAccountsLink();
  });
test('Enter Account Number', {
  tag: '@FillBADetails',
  }, async() => {
  await commonActions.clickAddNewButton();
  const todayDtTime = new Date().toISOString().replace(/[:T.]/g, '-').slice(0, -5);
  bankAcctDetails.acctNumber = "PW-Bank-Acct-Num-" + todayDtTime
  await bankAcctPage.enterAccountNumber(bankAcctDetails.acctNumber);
});
test('More tests...')
...
test('Capture Bank Account ID', {
  tag: '@BankAcctID',
  }, async () => {
  await expect(bankAcctPage.getBankIdLocator()).toBeVisible();
  const capturedbankAcctId = await bankAcctPage.getBankAcctId();
  bankAcctDetails.bankAcctId = capturedbankAcctId;
});
test('Check Bank account status', {
    tag: '@ActivateBA',
    }, async() => {
    console.log(`Bank account here: ${bankAcctDetails.bankAcctId}`);
    try {
      const statusText = await bankAcctPage.statusFromTheGrid();
      expect(statusText).toBe('Inactive');
      testState.bankAccountStatus = statusText;
    } catch(error) {
      console.error('Error in Check Bank account status test:', error);
      test.fail(true, `Expected status to be 'Inactive', but got '${statusText}`);
    }
  });
test('Enter Bank Account in Search Window to Activate', {
  tag: '@ActivateBA',
  }, async() => {
  console.log(`Bank account passed: ${bankAcctDetails.bankAcctId}`);
  await bankAcctPage.enterAgentBankIdInFilterSearchBox(bankAcctDetails.bankAcctId);
});

Any help to achieve this is much appreciated.

转载请注明原文地址:http://anycun.com/QandA/1744848555a88467.html