appium / java-client

Java language binding for writing Appium Tests, conforms to W3C WebDriver Protocol
Apache License 2.0
1.2k stars 753 forks source link

AppiumServiceBuilder return appiumJS is null #2007

Closed AleksandraZelik closed 11 months ago

AleksandraZelik commented 12 months ago

Description

Hey folks. I have an issue with starting appium server through the IDE. In some reason, it was worked on Monday, but now not. I didn't update java, Appium, Selenium, and other things that can have some dependence on it. I tried to debug it and found that in the AppiumServiceBuilder appiumJS variable is null, even when I override it through withAppiumJS. In the withAppiumJS appium got correct file, but on the builder part I see null exception.

image

Here are my properties:

<properties>
    <java.version>15</java.version>
    <junit.jupiter.version>5.9.0</junit.jupiter.version>
    <junit.platform.version>1.9.0</junit.platform.version>
    <rest-assured.version>5.2.0</rest-assured.version>
    <allure.version>2.19.0</allure.version>
    <maven-surefire.version>2.22.2</maven-surefire.version>
    <aspectj.version>1.9.6</aspectj.version>
    <maven-compiler.version>3.10.1</maven-compiler.version>
    <allure-maven.version>2.10.0</allure-maven.version>
    <gson.version>2.9.0</gson.version>
    <lombok.version>1.18.24</lombok.version>
    <selenium.version>4.9.1</selenium.version>
    <appium.version>8.5.0</appium.version>
    <postgresql.vervion>42.4.1</postgresql.vervion>
    <hibernate.version>5.6.9.Final</hibernate.version>
    <parallelTests>1</parallelTests>
</properties>

Laptop details:

OS: macOS Ventura 1.13 Java: 15.0.2 appium: 2.1.3 node: 18.17.1

Code To Reproduce Issue:

package base;

import io.appium.java_client.service.local.AppiumDriverLocalService; import io.appium.java_client.service.local.AppiumServiceBuilder; import io.appium.java_client.service.local.flags.GeneralServerFlag; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import utils.ReadProperties;

import java.io.File; import java.io.IOException; import java.net.ServerSocket;

import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE; import static java.lang.String.format; import static utils.Constants.KILL_NODE_COMMAND;

public class AppiumStarter { private static AppiumDriverLocalService appiumDriver; private static final Logger logger = LogManager.getLogger(AppiumStarter.class);

public static AppiumDriverLocalService getAppiumDriver(int port) {
    if (appiumDriver == null) startService(port);

    return appiumDriver;
}

public static void startService(int port){
   makePortAvailableIfOccupied(port);
    appiumDriver = new AppiumServiceBuilder()
            .withAppiumJS(new File("/usr/local/lib/node_modules/appium/build/lib/main.js"))
            .withIPAddress(ReadProperties.getAppiumAddress())
            .usingPort(ReadProperties.getAppiumPort())
            .usingDriverExecutable(new File("/usr/local/bin/node"))
            .withArgument(GeneralServerFlag.BASEPATH, ReadProperties.getAppiumBasePath())
            .withArgument(SESSION_OVERRIDE)
            .build();
    logger.info("Create Appium Server with next settings: AppiumAddress {}, Port {}, BasePath {}, Argument {}", ReadProperties.getAppiumAddress(), ReadProperties.getAppiumPort(), ReadProperties.getAppiumBasePath(), SESSION_OVERRIDE);
    appiumDriver.start();
    logger.info("Start Appium Server");
}

public static void closeAppium() {
    appiumDriver.stop();
}

private static void makePortAvailableIfOccupied(int port) {
    if (!isPortFree(port)) {
        try {
            logger.info("If port {} not free, we kill this process", port);
            Runtime.getRuntime().exec(KILL_NODE_COMMAND);
        } catch (IOException e) {
            logger.debug(e.getStackTrace());
        }
    }
}

private static boolean isPortFree(int port) {

    boolean isFree = true;
    try (ServerSocket ignored = new ServerSocket(port)) {
        logger.info("Checking, that port {} is free", port);
    } catch (Exception e) {
        isFree = false;
        logger.debug(e.getStackTrace());
    }
    return isFree;
}

public static void closeEmulator() {
    logger.info("Close Emulator {} after tests", ReadProperties.getAndroidUdid());
    try {
        Runtime.getRuntime().exec(format("adb -s %s emu kill", ReadProperties.getAndroidUdid()));
    } catch (IOException e) {
        logger.debug(e.getStackTrace());
    }
}

}

mykola-mokhnach commented 12 months ago

Maybe a duplicate of https://github.com/appium/java-client/issues/2004

Try to downgrade the selenium version as a workaround

AleksandraZelik commented 12 months ago

@mykola-mokhnach, i'm not sure that the issue is the same. And unfortunately downgrading the selenium version didn't help.

mykola-mokhnach commented 12 months ago

@mykola-mokhnach, i'm not sure that the issue is the same. And unfortunately downgrading the selenium version didn't help.

The stack trace looks similarly. Have you also tried to provide the log output explicitly?

AleksandraZelik commented 11 months ago

@mykola-mokhnach thank you, added withLogOutput(ByteStreams.nullOutputStream()). And it is work

valfirst commented 11 months ago

Duplicate of #2004

madhura-75 commented 11 months ago

Hi, What is the solution for the above issue?

madhura-75 commented 11 months ago

@AleksandraZelik Did you find solution for above issue? Even I am facing same