Search code examples
javanode.jsappium

AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started


Im trying to start Appium server programmatically from Java (OS: Windows7 x64)

The code that I use for starting Appium sever is:


  public void startServer() {
    //Set Capabilities
    cap = new DesiredCapabilities();
    cap.setCapability("noReset", "false");

    //Build the Appium service
    builder = new AppiumServiceBuilder();
    builder.withIPAddress("127.0.0.1");
    builder.usingPort(4723);
    builder.withCapabilities(cap);
    builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
    builder.withArgument(GeneralServerFlag.LOG_LEVEL, "error");

    //added by myself:
    builder.usingDriverExecutable(new File("C:/node/node.exe"));
    builder.withAppiumJS(new File("C:/Users/[user]/AppData/Roaming/npm/node_modules/appium/lib/appium.js"));


    //Start the server with the builder
    service = AppiumDriverLocalService.buildService(builder);
    service.start();
}

I'm getting an exception:

Exception in thread "main" io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started. The given Node.js executable: C:\node\node.exe Arguments: [C:\Users\Dima\AppData\Roaming\npm\node_modules\appium\lib\appium.js, --port, 4723, --address, 127.0.0.1, --log-level, error, --session-override, --default-capabilities, {\"noReset\": \"false\"}] 

Process output: C:\Users[user]\AppData\Roaming\npm\node_modules\appium\lib\appium.js:1 (function (exports, require, module, __filename, __dirname) { import _ from 'lodash'; ^^^^^^

SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3

I tried every way to start Appium server from the source, but second one causes to the same, but third causes to error enter image description here

Any ideas? Thanks to all in advance!


Solution

  • It did mistake in code. Fixed method is:

         public static String runAppiumService(int appiumPort) {
    
            //Build parameters for appium server:
            AppiumServiceBuilder appiumServiceBuilder = new AppiumServiceBuilder();
            appiumServiceBuilder.usingPort(appiumPort)
                    .withIPAddress(APPIUM_IP)
                    .withAppiumJS(new File(getAppiumJsPath()))
                    .withArgument(GeneralServerFlag.SESSION_OVERRIDE)
                    .withLogFile(new File(System.getProperty("user.dir") + "/target/resources/appium_server_logs" + Thread.currentThread().getId()));
            AppiumDriverLocalService service = AppiumDriverLocalService.buildService(appiumServiceBuilder);
            service.start();
    }