Playwright JS

npm install @tracera/playwright

Node.js 22.19–26.12 और Playwright 1.50–1.64 पर परीक्षित (देखें ऑटोटेस्ट एडेप्टर)। @tracera/playwright npm पर.

playwright.config.ts में:

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

export default defineConfig({
  reporter: [['@tracera/playwright/reporter']],
  use: {
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
    trace: 'retain-on-failure', // or "on-first-retry"
  },
});

import "@tracera/playwright/register" हर worker में test.step और hooks पैच करता है ताकि tracera.comment और tracera.error मौजूदा स्टेप पर जाएँ।

कॉन्फ़िग में एक बार screenshot, video और trace चालू करें — Playwright आर्टिफ़ैक्ट अपने आप Tracera रिजल्ट में जुड़ जाते हैं।

एनवायरनमेंट, रन मोड और रीट्राई: ऑटोटेस्ट एडेप्टर।

रीट्राई Env कुंजी: PLAYWRIGHT_RETRY।

testCaseId और autotestName बाँधना

test और expect @playwright/test से इम्पोर्ट करें, tracera और annotations @tracera/playwright से। annotations नाम शाब्दिक होते हैं ({param} नहीं)।

घोषणा पर (पसंदीदा):

import { test, expect } from '@playwright/test';
import { annotations } from '@tracera/playwright';

test('login', { annotation: annotations(42, 'e2e/login') }, async ({ page }) => {
  await page.goto('/login');
  await expect(page.getByRole('button', { name: 'Sign in' })).toBeVisible();
});

टेस्ट के अंदर (अगर annotations पहले से केस बाँध चुके हों तो वैकल्पिक):

import { test, expect } from '@playwright/test';
import { tracera } from '@tracera/playwright';

test('login', async ({ page }) => {
  tracera.testCase(42, 'e2e/login');
  await page.goto('/login');
  await expect(page.getByRole('button', { name: 'Sign in' })).toBeVisible();
});

Skip

स्टॉक Playwright skip इस्तेमाल करें (test.skip, testInfo.skip(…))। Skip reason परिणाम में comment के रूप में जुड़ता है।

import { test } from '@playwright/test';
import { annotations } from '@tracera/playwright';

test('login', { annotation: annotations(42, 'e2e/login') }, async ({}, testInfo) => {
  testInfo.skip(!process.env.READY, 'env not ready');
});

स्टेप्स

Playwright का test.step इस्तेमाल करें — यह टेस्ट में रिकॉर्ड होता है। वही रिपोर्टिंग tracera.step से भी: कॉल tracera.step(title, fn), क्लास मेथड पर डेकोरेटर @tracera.step("…"), या tracera.step("…")(fn)। डेकोरेटर शीर्षक में {name} / {0} मेथड आर्गुमेंट से भरते हैं; शीर्षक लौटाने वाला फ़ंक्शन भी दे सकते हैं। कॉल-फ़ॉर्म में {name} / {0} नहीं भरते — तैयार शीर्षक स्ट्रिंग दें (या खुद बनाएँ)। Async मेथड और Promise पूरे होने तक प्रतीक्षा होती है। test.beforeEach के अंदर स्टेप्स परिणाम के सेटअप में जाते हैं; test.afterEach के अंदर स्टेप्स परिणाम के टीयरडाउन में। सूट-स्तरीय हुक्स (test.beforeAll / test.afterAll) रिपोर्ट नहीं होते।

import { test } from '@playwright/test';
import { annotations } from '@tracera/playwright';

test.beforeEach(async ({ page }) => {
  await test.step('open shop', async () => {
    await page.goto('/shop');
  });
});

test('checkout', { annotation: annotations(10, 'e2e/checkout') }, async ({ page }) => {
  await test.step('add to cart', async () => {
    await page.getByRole('button', { name: 'Add' }).click();
  });
});

test.afterEach(async ({ page }) => {
  await test.step('cleanup', async () => {
    await page.goto('/cart/clear');
  });
});

मेथड पर डेकोरेटर:

import { tracera } from '@tracera/playwright';

class Cart {
  @tracera.step('add {item}')
  async addToCart(item: string) {
    // …
  }
}

टिप्पणियाँ, त्रुटियाँ और अटैचमेंट

टिप्पणी, त्रुटियाँ और अटैचमेंट: ऑटोटेस्ट एडेप्टर।

import { test } from '@playwright/test';
import { tracera, annotations } from '@tracera/playwright';

test('login', { annotation: annotations(42, 'e2e/login') }, async ({ page }, testInfo) => {
  tracera.comment('result-level note');
  tracera.error('result-level error text');

  await test.step('open login', async () => {
    await page.goto('/login');
    tracera.comment('landed on login');
  });

  await testInfo.attach('suite-log.txt', {
    body: 'finished login flow',
    contentType: 'text/plain',
  });
});

फ़ाइल जोड़ने के लिए testInfo.attach इस्तेमाल करें। test.step के अंदर फ़ाइल उस स्टेप पर जुड़ती है। स्टेप के बाहर वह परिणाम पर जुड़ती है। Playwright स्क्रीनशॉट, वीडियो और Playwright trace खुद अटैच करता है — Tracera में इमेज लाइटबॉक्स में खुलती हैं, वीडियो इनलाइन चलता है, और trace Trace Viewer में खुलता है।

पैरामीटराइज़्ड टेस्ट

import { test, expect } from '@playwright/test';
import { annotations } from '@tracera/playwright';

const rows = [
  { id: 42, name: 'e2e/login-ok', path: '/login' },
  { id: 43, name: 'e2e/login-sso', path: '/login/sso' },
];

for (const row of rows) {
  test(row.name, { annotation: annotations(row.id, row.name) }, async ({ page }) => {
    await test.step('open', async () => {
      await page.goto(row.path);
    });
    await expect(page.getByRole('button', { name: 'Sign in' })).toBeVisible();
  });
}

या टेस्ट में annotations की जगह tracera.testCase(row.id, row.name) कॉल करें।

कस्टम fixtures

स्टॉक Playwright test को हमेशा की तरह extend करें:

import { test as base, expect } from '@playwright/test';
import { tracera } from '@tracera/playwright';

export const test = base.extend<{ asUser: void }>({
  asUser: [
    async ({ page }, use) => {
      await page.goto('/login');
      await page.getByLabel('Email').fill('user@example.com');
      await page.getByRole('button', { name: 'Sign in' }).click();
      await use();
    },
    { auto: true },
  ],
});

test('dashboard', async ({ page }) => {
  tracera.testCase(99, 'e2e/dashboard');
  await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
});