Search code examples
playwright

can we use the toHaveScreenshot() and toMatchSnaphot() out side the test


Can we use the toHaveScreenshot() and toMatchSnaphot() outside the test without using config file only simple install NPM i playwright in package.json

I have already one snapshot I want to compare snapshot using toHaveScreenshot() method but I am confused we can use outside the test context?

const { chromium } =require( "playwright");

const example = async () => {
  const browser = await chromium.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto("https://zversal.com/");
 
  await page.toHaveScreenshot("zeversal.png", {
    fullPage: false,
    maxDiffPixelRatio: 0.24,
  });
};
example();

Console reports error:

toHaveScreenshot() must be called during the test


Solution

  • I don't think this is possible. Afaik, toHaveScreenshot() is part of the @playwright/test package.

    If I'm looking at the Page API docs there's no toHaveScreenshot() listed. I'd say it's only available in combination with Playwright Test and it's provided expect method.

    await expect(page).toHaveScreenshot();