Appium代码生成“ SyntaxError:等待仅在异步函数中有效”

时间:2019-01-04 18:56:18

标签: 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。

1 个答案:

答案 0 :(得分:1)

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

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