0

I encountered this error when i run appium server programmatically, but the test code i wrote was executed successful with the results and connected to the emulator(android studio).

enter image description here

package com.codenbox;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;

public class Base {

    public AndroidDriver driver;
    public AppiumDriverLocalService service;

    @BeforeClass
    public void configureAppium() throws MalformedURLException, InterruptedException {

        AppiumServiceBuilder builder = new AppiumServiceBuilder ();
        builder = new AppiumServiceBuilder();
        builder.withAppiumJS(new File("C:\\Users\\Admin\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"));
        builder.withIPAddress("127.0.0.1");
        builder.usingPort(4723);
        
        try {
            service = AppiumDriverLocalService.buildService(builder);
            service.start();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
        // create capabilities
        UiAutomator2Options options = new UiAutomator2Options();
        options.setDeviceName("Demo 1");
        // ApiDemos-debug apk
        options.setApp(System.getProperty("user.dir")+"\\src\\test\\java\\com\\codenbox\\resources\\ApiDemos-debug.apk");
        options.setChromedriverExecutable(System.getProperty("user.dir")+"\\src\\test\\java\\com\\codenbox\\resources\\chromedriver.exe");
        //create object for AndroidDriver/IOSDriver
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), options);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
        service.stop();
    }
    
}

I wanted to let you know that the code above was working fined in my early stage of testing. I dont know what triggers this issue and I already updated/downgraded vice versa my dependency but still not working.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
Poploy
  • 60
  • 1
  • 6

2 Answers2

2

For some reason, it is not detecting the nodejs folder.

Try adding the next argument to your builder.

builder.usingDriverExecutable(new File("/path/to/your/nodejs");

In mac, it would be something like:

builder.usingDriverExecutable(new File("/usr/local/bin/node");

  • Thanks mate ! just added this argument and fixed my issue. So this code means need to execute my node.exe everytime i run AppiumService ? hmmm ... i thought this is the job of withAppiumJS("url of main.js")) hehe lol – Poploy Mar 30 '23 at 20:39
1

Try below code. It worked for me

builder = new AppiumServiceBuilder ();
    builder.withIPAddress ("127.0.0.1")
            .usingPort (4723)
            .withAppiumJS (
                    new File ("C:\\Users\\{yourname}\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))
            .usingDriverExecutable (new File ("C:\\Program Files\\nodejs\\node.exe"))
            .withArgument (GeneralServerFlag.BASEPATH, "/wd/hub")
            .withArgument (GeneralServerFlag.SESSION_OVERRIDE)
            .withArgument (GeneralServerFlag.LOG_LEVEL, "debug");

    appiumDriverLocalService = AppiumDriverLocalService.buildService (builder);
    appiumDriverLocalService.start ();