最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: Cannot find module '@playwrighttest' - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Dec 2, 2021 at 21:38 Antonio contrerasAntonio contreras 1371 gold badge1 silver badge5 bronze badges 4
  • in case it helps, you may already know, you are using named imports and so the name your are specifying, i.e expect, must exactly match the name of a value that is being exported from the module @playwright/test. so 1) is the file really named @playwright/test? and 2) is it exporting the value expect? – user1063287 Commented Dec 2, 2021 at 21:44
  • Hi, I went into the node modules, playwright -> test -> expect.js and found this const expect = _expect.default; exports.expect = expect; So I think they are correct? I also forgot to mention that I use an M1 Pro and there was a time where I had an issue with importing as well but Idk if the M1 Pro has anything to do – Antonio contreras Commented Dec 2, 2021 at 21:53
  • yes, sorry, I wasn’t familiar with this package, it looks like it should work, if you followed all instructions here: playwright.dev/docs/intro – user1063287 Commented Dec 2, 2021 at 21:54
  • 1 No need to apologize, I am doing this a bit different and not using the npx command just npm run test and just doing everything on the main.js so that's why I was curious about why this error is happening. – Antonio contreras Commented Dec 2, 2021 at 22:01
Add a comment  | 

4 Answers 4

Reset to default 14
  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.

Now, in after test validations, preferred way to use is auto-retry assertions compared to plain assertions.

The reason is unlike plain assertions ,these will retry until either the assertion passes, or the assertion timeout is reached.

Note that retrying assertions are async, so you must await them unlike plain assertions.

Few Usage Examples:

// A specific element is visible.
await expect(page.getByText('Welcome')).toBeVisible();

// At least one item in the list is visible.
await expect(page.getByTestId('todo-item').first()).toBeVisible();

// At least one of the two elements is visible, possibly both.
await expect(
    page.getByRole('button', { name: 'Sign in' })
        .or(page.getByRole('button', { name: 'Sign up' }))
        .first()
).toBeVisible();

While trying to use playwright as a library i did run into the same error

Error: Cannot find module '@playwright/test'

To fix this error in my case the following needed to be done. The pre-condition is that playwright is already installed.

Create a node js project

  1. Create a folder: mkdir HelloLibrary
  2. Inside this folder create a file from the command prompt: echo var msg = 'Hello World'; console.log(msg); > app.js
  3. Open a command prompt and run node app.js
  4. Add playwright npm i -D playwright
  5. Change content of app.js like code sample below
  6. Run it again node app.js
  7. Voila done

App.js sample

This code launches a browser and takes a screenshot

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

(async () => {
 
 const browser = await chromium.launch({ headless: false, slowMo: 50 });
 const page = await browser.newPage();
 await page.goto('http://whatsmyuseragent.org/');
 await page.screenshot({ path: `example.png` });
 await browser.close();
})();

See also

  1. Tutorial: Node.js for Beginners
  2. Use playwright as a library
发布评论

评论列表(0)

  1. 暂无评论