简体   繁体   中英

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

I have been following the Appium tutorial on the official website. 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:

 /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:

 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? Is there a work-around to make the tutorial work, like using another node.js version? My version is 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在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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