javascript - Appium代码生成 "SyntaxError: await is only valid in async function"

标签 javascript node.js npm async-await appium

我一直在关注 Appium tutorial在官方网站上。按照这些步骤调整代码模板以适应我机器上的资源名称/路径后,我使用 node.js 运行了 test.js 文件并收到以下错误:

/home/samaraassis/Appium_tutorial/test.js:18
const elementId = await client.findElement("accessibility id","TextField1");    client.elementSendKeys(elementId.ELEMENT, "Hello World!");
                  ^^^^^

SyntaxError: await is only valid in async function
    at new Script (vm.js:84:7)
    at createScript (vm.js:264:10)
    at Object.runInThisContext (vm.js:312:10)
    at Module._compile (internal/modules/cjs/loader.js:684:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
    at executeUserCode (internal/bootstrap/node.js:342:17)

这是 test.js 文件的内容:

  1 // javascript
  2 
  3 const wdio = require("webdriverio");
  4 
  5 const opts = {
  6   port: 4723,
  7   capabilities: {
  8     platformName: "Android",
  9     platformVersion: "8.0",
 10     deviceName: "Nexus 5X API 28",
 11     app: "/home/samaraassis/ApiDemos-debug.apk",
 12     automationName: "UiAutomator2"
 13   }
 14 };
 15 
 16 const client = wdio.remote(opts);
 17 
 18 const elementId = await client.findElement("accessibility id","TextField1");    clie    nt.elementSendKeys(elementId.ELEMENT, "Hello World!");
 19 const elementValue = await client.findElement("accessibility id","TextField1");
 20 await client.getElementAttribute(elementValue.ELEMENT,"value").then((attr) => {
 21 assert.equal(attr,"Hello World!");
 22 });

问题真的出在 client.findElement() 函数的定义上,还是问题不太明显,我只是看不到它?是否有解决方法可以使教程正常工作,比如使用另一个 node.js 版本?我的版本是v11.6.0。

最佳答案

你应该像下面这样写。 (请注意,教程中的代码与演示应用程序完全不匹配。似乎没有人维护教程...)。测试将单击“文本”按钮,因此您将看到下一个屏幕。

const wdio = require("webdriverio");

const opts = {
    port: 4723,
    capabilities: {
        platformName: "Android",
        platformVersion: "9",
        deviceName: "Pixel 2 API 28 Pie",
        //app: "ApiDemos-debug.apk",
        app: "https://github.com/appium/appium/raw/master/sample-code/apps/ApiDemos-debug.apk",
        automationName: "UiAutomator2"
    }
};

(async () => {
    const client = await wdio.remote(opts);

    console.log(client.findElement);

    const field =   await client.$("~Text");
    field.click();
})();

截图 enter image description here

enter image description here

关于javascript - Appium代码生成 "SyntaxError: await is only valid in async function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54044555/

相关文章:

javascript - 用D3绘制三个相互交织的圆圈

Javascript 在知道字符串是什么时返回 "undefined"

javascript - AngularJs $watch 为什么这不起作用?

javascript - 如何拆分单体 node.js javascript

node.js 和 redis : is a redis store accessible across node connections?

angularjs - 如何使用nodejs/angular删除Amazon s3文件

node.js - 与 MongoDB 的单个连接还是多个连接更好?

git - 自定义 Bower 安装组件的正确方法

php - 如何将 node_modules 文件夹部署到生产服务器?

javascript - Electron - 单击按钮执行程序