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 |4 Answers
Reset to default 14- You have to install
@playwright/test
library:
npm i -D @playwright/test
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.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
- Create a folder:
mkdir HelloLibrary
- Inside this folder create a file from the command prompt:
echo var msg = 'Hello World'; console.log(msg); > app.js
- Open a command prompt and run
node app.js
- Add playwright
npm i -D playwright
- Change content of
app.js
like code sample below - Run it again
node app.js
- 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
- Tutorial: Node.js for Beginners
- Use playwright as a library
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 valueexpect
? – user1063287 Commented Dec 2, 2021 at 21:44npm 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