【问题标题】:Appium code generates "SyntaxError: await is only valid in async function"Appium 代码生成“SyntaxError: await is only valid in async function”
【发布时间】:2019-05-31 08:33:58
【问题描述】:

我一直在关注官网上的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。

【问题讨论】:

  • 这个错误告诉你问题到底出在哪里:你不能 awaitasync 函数,除非在 另一个 async 函数中。既然你有一个.then() 子句,就去掉await 关键字。
  • github.com/appium/appium/blob/master/sample-code/… 与您完成的简单教程的代码几乎相同,但显示了正确的调用模式。
  • 不得不说,Appium 的教程太烂了。它很容易将潜在用户拒之门外。

标签: javascript node.js npm async-await appium


【解决方案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();
})();

截图

【讨论】:

    猜你喜欢
    • 2016-10-15
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 2020-03-29
    • 2022-11-15
    • 2021-12-19
    相关资源
    最近更新 更多