简体   繁体   English

Appium 代码生成“SyntaxError: await is only valid in async function”

[英]Appium code generates "SyntaxError: await is only valid in async function"

I have been following the Appium tutorial on the official website.我一直在关注官方网站上的Appium 教程 After following the steps and adapting their code template to the resource names/paths on my machine, I ran the test.js file using node.js and got the following error:按照步骤操作并将其代码模板调整为我机器上的资源名称/路径后,我使用 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)

This is the content of the test.js file:这是 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 });

Is the issue really the definition of the client.findElement() function, or is the problem less obvious and I just can't see it?问题真的是 client.findElement() 函数的定义,还是问题不那么明显而我看不到它? Is there a work-around to make the tutorial work, like using another node.js version?是否有解决方法可以使教程正常工作,例如使用另一个 node.js 版本? My version is v11.6.0.我的版本是 v11.6.0。

You should write your as below.你应该写你的如下。 (Note the the code in Tutorial does not match the Demo app at all. Seems nobody is maintaining the tutorial...). (注意教程中的代码根本不匹配演示应用程序。似乎没有人维护教程......)。 The test will click on button "Text" so you will see the next screen.测试将单击“文本”按钮,因此您将看到下一个屏幕。

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();
})();

Screenshots截图在此处输入图片说明

在此处输入图片说明

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

相关问题 SyntaxError: await 仅在我的代码中的异步 function 中有效 - SyntaxError: await is only valid in async function in my code Javascript:SyntaxError:await仅在异步函数中有效 - Javascript: SyntaxError: await is only valid in async function 未捕获的SyntaxError:await仅在异步函数的异步函数中有效 - Uncaught SyntaxError: await is only valid in async function in async function SyntaxError: await 仅在异步中有效 function **而且我有异步** - SyntaxError: await is only valid in async function **and I have async** SyntaxError: await 仅在 async 函数中有效。 无法纠正 - SyntaxError: await is only valid in async function. Unable to Correct it Node Js - SyntaxError: await 仅在异步中有效 function - Node Js - SyntaxError: await is only valid in async function SyntaxError:“await”仅在“async”中有效 function - 需要帮助 - SyntaxError: “await” is only valid in an “async” function - Help Needed 未捕获的 SyntaxError:await 仅在异步函数中有效(FireBase 文档) - Uncaught SyntaxError: await is only valid in async function (FireBase documentation) 如何解决:SyntaxError: await is only valid in async function - How to resolve: SyntaxError: await is only valid in async function 语法错误:await 仅在异步函数 discord.js 中有效 - SyntaxError: await is only valid in async function discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM