Search code examples
mavenselenium-webdriverappium-java

java.lang.NullPointerException at io.appium.java_client.service.local.AppiumDriverLocalService.destroyProcess(AppiumDriverLocalService.java:220)


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.


Solution

  • 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");