webdriverio / appium-boilerplate

Boilerplate project to run WebdriverIO tests with Appium to test native applications on iOS and Android
MIT License
453 stars 261 forks source link

Getting "Cannot use import statement outside a module" when running Android sample specs #36

Closed ggiunta closed 4 years ago

ggiunta commented 4 years ago

Hey Guys, I'm getting the following when trying to run the tests. I suspect it might have to do with the Node version and ES6 support but just guessing

Environment (please complete the following information):

Describe the bug Getting the following for all the specs when running npm run android.app

[0-0] RUNNING in Android - /tests/specs/app.forms.spec.js
[0-0] Error: Cannot use import statement outside a module
[0-0] FAILED in Android - /tests/specs/app.forms.spec.js

Expected behavior Tests are executed

wswebcreation commented 4 years ago

Hi @ggiunta

Yes, this is a ES6 support thing, when are you having this? With the current project, or when you do some modifications? The project should work out if the box because it uses Babel to solve this.

ggiunta commented 4 years ago

I just cloned the repo, followed instructions and run the tests with the Android cmd. Did not change a thing

wswebcreation commented 4 years ago

@ggiunta

Sorry for my late response, the diff might be in the supported NodeJS env, I need to dig into it, haven't got the time today. It works with Node 10 and 12 on my machine so for as far I can see it not an issue with the boilerplate itself but more an environmental issue. Can you also test it with an older version of Node?

I'll get back to you with my results when I have some more time

erwinheitzman commented 4 years ago

If the import isn't recognized it will complain about the syntax, in this specific case however it looks like an import is used on a file for which it cannot find any modules to import @wswebcreation

wswebcreation commented 4 years ago

@ggiunta

I've been running this with Node v13.8.0 and with v10.18.1 and I get this result

➜  appium-boilerplate git:(master) npm run android.app -- --spec=/tests/specs/app.forms.spec.js

> appium-boilerplate@5.3.1 android.app /Users/wimselles/Sauce/Git/appium-boilerplate
> wdio ./config/wdio.android.app.conf.js "--spec=/tests/specs/app.forms.spec.js"

Execution of 1 spec files started at 2020-02-09T07:21:42.737Z

[0-0] RUNNING in Android - /tests/specs/app.forms.spec.js
[0-0] PASSED in Android - /tests/specs/app.forms.spec.js

 "spec" Reporter:
------------------------------------------------------------------
[emulator-5554 LINUX 8.1.0 #0-0] Spec: /Users/wimselles/Sauce/Git/appium-boilerplate/tests/specs/app.forms.spec.js
[emulator-5554 LINUX 8.1.0 #0-0] Running: emulator-5554 on LINUX 8.1.0 executing /Users/wimselles/Sauce/Git/appium-boilerplate/apps/Android-NativeDemoApp-0.2.1.apk
[emulator-5554 LINUX 8.1.0 #0-0] Session ID: 266d8280-cc89-41af-aff4-85f8bd0fc41e
[emulator-5554 LINUX 8.1.0 #0-0]
[emulator-5554 LINUX 8.1.0 #0-0] WebdriverIO and Appium, interacting with form elements,
[emulator-5554 LINUX 8.1.0 #0-0]    ✓ should be able type in the input and validate the text
[emulator-5554 LINUX 8.1.0 #0-0]    ✓ should be able turn on and off the switch
[emulator-5554 LINUX 8.1.0 #0-0]    ✓ should be able select a value from the select element
[emulator-5554 LINUX 8.1.0 #0-0]    ✓ should be able to open the alert and close it with all 3 buttons
[emulator-5554 LINUX 8.1.0 #0-0]    ✓ should be able to determine that the inactive button is inactive
[emulator-5554 LINUX 8.1.0 #0-0]
[emulator-5554 LINUX 8.1.0 #0-0] 5 passing (17.4s)

Spec Files:      1 passed, 1 total (100% completed) in 00:00:24 

To be honest, I don't know where this error is coming from, what happens if you run this with logLevel=debug, can you post the logs here? Secondly you mention that you didn't change the cloned project but by default it takes Android 8.1, are you sure you didn't make any changes?

wswebcreation commented 4 years ago

Closing due to inactivity

peterpham commented 4 years ago

I got the same error running on Node v13.12.0

cd ./examples/pageobject
npm test

throw error

Execution of 1 spec files started at 2020-04-13T23:54:42.128Z

[0-0] RUNNING in chrome - /specs/dynamic.spec.js
[0-0]  Error:  Cannot use import statement outside a module
[0-0] FAILED in chrome - /specs/dynamic.spec.js

Spec Files:  0 passed, 1 failed, 1 total (100% completed) in 00:00:03 

npm ERR! Test failed.  See above for more details.
wswebcreation commented 4 years ago

@peterpham

Can you create a new issue and provide an example, as mentioned, I can't reproduce it, this might even be a coding issue in your own framework

peterpham commented 4 years ago

Thanks. I added as new issue #72

Ricardinh0 commented 3 years ago

Having a babel.config.js setup helped me get over this. See WebdriverIO for details.

mittalri commented 2 years ago

I am getting error while trying to start Appium in java code. AppiumDriverLocalService service; AppiumServiceBuilder builder = new AppiumServiceBuilder(); builder.withIPAddress("127.0.0.1"); builder.usingPort(Integer.parseInt("4723")); builder.withAppiumJS(new File("C:\Users\Ritesh Mittal\AppData\Roaming\npm\node_modules\appium\lib\appium.js"));

    service = AppiumDriverLocalService.buildService(builder);
    service.start();
    System.out.println("Appium Service Started ");

Error C:\Users\Ritesh Mittal\AppData\Roaming\npm\nodemodules\appium\lib\appium.js:1 import from 'lodash'; ^^^^^^

SyntaxError: Cannot use import statement outside a module

vadimperkin commented 2 years ago

Does someone found the solution? Nothing helps: type:module, renaming to cjs or mjs, adding babel and adding to wdio,conf.js require: [babel/register]. Always the same problem with "import" statement, but when I hover on "import" I can see a pop-up hint, so it means Node can read some imported file/function, but not able to run. In the whole internet so far I don't find any answer. Please, if you somehow resolve this issue, reply to this! Thanks in advance.

YungTosti commented 2 years ago

Having the same issue...

mph6083 commented 2 years ago

having the same issue