0

I'm trying to use Playwright with Javascript and struggling to get the page.locator (https://playwright.dev/docs/locators) function to work.

I've created the file testLocator.js that I run with node .\testLocator.js in the command terminal of visual studio code.

My code:

const { test, expect } = require('@playwright/test');
const playwright = require('playwright');

const delay = ms => new Promise(res => setTimeout(res, ms));

(async () => {
  // Code execution happens within here
  const browser = await playwright["chromium"].launch({
    headless: false
  });

  //context
  const context = await browser.newContext();

  //page
  const page = await context.newPage();

  //naviagate to the page
  await page.goto("https://stackoverflow.com/");
  const locator = page.locator('text=About');
  delay(5000);
})();

When I run the code I get the following error:

C:\Users\todd.baker\PlaywrightTest\my-playwright-intro\testLocator.js:22
  const locator = page.locator('text=About');
                       ^

TypeError: page.locator is not a function
    at C:\Users\todd.baker\PlaywrightTest\my-playwright-intro\testLocator.js:22:24
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Why is page.locator not recognized as a function when the playwright website that I've attached here claims that it is?

Found a way to create a package.json file and it was built with these contents:

{
  "name": "my-playwright-intro",
  "version": "1.0.0",
  "description": "",
  "main": "testLocator.js",
  "dependencies": {
    "agent-base": "^4.3.0",
    "brace-expansion": "^1.1.11",
    "async-limiter": "^1.0.1",
    "balanced-match": "^1.0.2",
    "buffer-crc32": "^0.2.13",
    "buffer-from": "^1.1.2",
    "core-util-is": "^1.0.3",
    "concat-map": "^0.0.1",
    "concat-stream": "^1.6.2",
    "fd-slicer": "^1.1.0",
    "extract-zip": "^1.7.0",
    "debug": "^4.3.4",
    "es6-promisify": "^5.0.0",
    "glob": "^7.2.3",
    "fs.realpath": "^1.0.0",
    "https-proxy-agent": "^3.0.1",
    "inflight": "^1.0.6",
    "inherits": "^2.0.4",
    "isarray": "^1.0.0",
    "mkdirp": "^0.5.6",
    "mime": "^2.6.0",
    "jpeg-js": "^0.3.7",
    "minimatch": "^3.1.2",
    "ms": "^2.1.2",
    "once": "^1.4.0",
    "path-is-absolute": "^1.0.1",
    "pend": "^1.2.0",
    "playwright": "^0.10.0",
    "process-nextick-args": "^2.0.1",
    "playwright-core": "^0.10.0",
    "proxy-from-env": "^1.1.0",
    "pngjs": "^3.4.0",
    "readable-stream": "^2.3.7",
    "safe-buffer": "^5.1.2",
    "es6-promise": "^4.2.8",
    "uuid": "^3.4.0",
    "util-deprecate": "^1.0.2",
    "minimist": "^1.2.6",
    "rimraf": "^2.7.1",
    "ws": "^6.2.2",
    "yauzl": "^2.10.0",
    "wrappy": "^1.0.2",
    "string_decoder": "^1.1.1",
    "progress": "^2.0.3",
    "typedarray": "^0.0.6"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
Todd Baker
  • 41
  • 1
  • 9
  • 2
    Your code is working on my machine – hardkoded Jul 14 '22 at 17:23
  • Did you run it in Visual Studio Code? Could there be some setting or installation that I'm missing? – Todd Baker Jul 14 '22 at 17:37
  • This works for me as well. I did run using `node .\testLocator.js` which version of nodejs are you on? Do you have playwright installed? I am on windows 10 and node 16 – Raju Jul 14 '22 at 18:18
  • Ran node -v in the terminal in vs code to check my version of node.js and it shows v16.15.1. I do have the extension Playwright Test for VSCode installed and have been able to run other playwright commands. – Todd Baker Jul 14 '22 at 18:52
  • Please share your package.json – ggorlen Jul 15 '22 at 22:17
  • I see a file under node_modules named .package-lock.json. Is this what you're referring to? It has 488 lines. – Todd Baker Jul 18 '22 at 19:34
  • Found a way to create a package.json file and added it to my original post – Todd Baker Jul 18 '22 at 20:06

1 Answers1

0

Update: I uninstalled and reinstalled playwright test and it appears to be working now.

Todd Baker
  • 41
  • 1
  • 9