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:

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 source file:

const wdio = require("webdriverio");
const opts = {
  port: 4723,
  capabilities: {
    platformName: "Android",
    platformVersion: "8.1.0",
    deviceName: "Android Emulator",
    app: "/Users/guest/Downloads/ApiDemos-debug.apk",
    automationName: "UiAutomator2"
  }
};

const client = wdio.remote(opts);

const elementId = await client.findElement("accessibility id","TextField1");    
client.elementSendKeys(elementId.ELEMENT, "Hello World!");
const elementValue = await client.findElement("accessibility id","TextField1");
await client.getElementAttribute(elementValue.ELEMENT,"value").then((attr) => {
	assert.equal(attr,"Hello World!");
});

1 Like

can any one please ping the solution for above issue

1 Like

update NodeJS to v. 8+

Currently am using latest one only

Have you tried this short example?

When running this i have the same issue.
SyntaxError: await is only valid in async function

This is a normal interpreter error if one tries to use await in non-async function

The issue here is online getting started.
Yes, Appium is not for someone who does not have a clue what Javascript is.
But beleive me, there are some who call themselves seniors and get stuck with this getting started.

If you try to follow that getting started using Appium-Desktop you’ll see what I mean.
The guide simply does not work. And is missleading. I still did not figure how to use Appium desktop and do a simple tap button on a real android device.

I am not sure what is the “tone” of my reply, but it was call for help of a noob. = )