2

我目前正在尝试使用 expect 来做断言,const { expect } = require('@playwright/test');但每次我得到错误:找不到模块'@playwright/test'。这是一个非常短的脚本,但其中有问题。

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]");
})();

包.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"
  }
}

没有这个测试工作正常:

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

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

它做我要求它做的事情,但是现在我想做断言并且我补充说,现在它不起作用。

4

1 回答 1

1
  1. 您必须安装@playwright/test库:
   npm i -D @playwright/test
  1. 不要使用playwright-expect库。Playwright 已经包含了网络优先的断言。因此,没有理由使用额外的库来扩展期望。

  2. 删除未使用的代码:

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

// add custom matchers
expect.extend(matchers);
于 2021-12-03T08:30:21.307 回答