简体   繁体   English

Playwright Javascript 测试定位器功能

[英]Playwright Javascript Testing Locator function

I'm trying to use Playwright with Javascript and struggling to get the page.locator ( https://playwright.dev/docs/locators ) function to work.我正在尝试将 Playwright 与 Javascript 一起使用,并努力让 page.locator ( https://playwright.dev/docs/locators ) 功能正常工作。

I've created the file testLocator.js that I run with node .\testLocator.js in the command terminal of visual studio code.我已经在 Visual Studio 代码的命令终端中创建了使用 node .\testLocator.js 运行的文件 testLocator.js。

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?当我在此处附加的剧作家网站声称它是时,为什么page.locator不被识别为功能?

Found a way to create a package.json file and it was built with these contents:找到了一种创建 package.json 文件的方法,它是用以下内容构建的:

{
  "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"
}

更新:我卸载并重新安装了 playwright 测试,它现在似乎可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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