简体   繁体   中英

Error: Cannot find module '@playwright/test'

I am currently trying to use expect to do assertions by using const { expect } = require('@playwright/test'); but every time I get Error: Cannot find module '@playwright/test'. It is a very short script but something is wrong with that.

const { chromium } = require("playwright");
const { expect } = require('@playwright/test');
const { matchers } = require('playwright-expect');
console.log("##########", expect)

// add custom matchers
expect.extend(matchers);

(async () => {
  const browser = await chromium.launch({
    headless: false,
  });
  const page = await browser.newPage();
  await page.goto("someurl");
  await page.fill("input[name='userLoginId']", 'nnn');
  await page.fill("input[name='password']", 'nnn');
  await page.click("button[type=submit]");
})();

package.json

{
  "name": "playwright",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "node ./index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "playwright": "^1.15.1",
    "playwright-expect": "^0.1.2"
  }
}

The test works fine without this:

const { expect } = require('@playwright/test');
const { matchers } = require('playwright-expect');
console.log("##########", expect)

// add custom matchers
expect.extend(matchers);

And it does what I ask it to do, but now that I want to do assertions and I add that, now it does not work.

  1. You have to install @playwright/test library:
   npm i -D @playwright/test
  1. Do not use playwright-expect library. Playwright already includes web-first assertions . Hence, there is no reason to use an additional library to extend expect.

  2. Remove unused code:

const { matchers } = require('playwright-expect');
console.log("##########", expect)

// add custom matchers
expect.extend(matchers);

I've created an issue about the same question here https://github.com/microsoft/playwright/issues/14971 and I'll update the result when it's answered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM