Connection Refused if ran the appium with mvn test

Hello, I was able to run the appium test execution with testNG however when I executed it with mvn test it return the

Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

Caused by: org.openqa.selenium.WebDriverException:
Connection refused

this is how I initialize the driver


    private static final ThreadLocal<AppiumDriver> driver = new ThreadLocal<>();
    public static AppiumDriver getDriver() {
        return driver.get();
    }

    public static void setDriver(AppiumDriver driver1) {
        driver.set(driver1);
    }

    public static void initializeDriver() throws MalformedURLException, FileNotFoundException {

        AppiumDriver driver = null;
 

        MutableCapabilities capabilities = new UiAutomator2Options();
        capabilities.setCapability("unicodeKeyboard", "true");
        capabilities.setCapability("resetKeyboard", "true");

        if (str[0].contains("platformName=ios")) {
            capabilities = new XCUITestOptions();
        }


        URL url = new URL("http://127.0.0.1:4723/wd/hub");

        String platformName = capabilities.getCapability("platformName").toString();
        setPlatform(platformName);

        System.out.println(platformName.equals("IOS"));
        if (platformName.equals("IOS")) {
            driver = new IOSDriver(url, capabilities);
        }
        if (platformName.equals("ANDROID")) {
            driver = new AndroidDriver(url, capabilities);
        }

        setDriver(driver);
    }

I cant share the details about the appium as im using the browserstack but when I ran it using the testNG or the run button in intellij my script is working fine. just the issue with the maven test with profile

I double the check the testng file path and they are pointed to correct path.

I did ran the sample project from browserstack with maven test -P sample-test and they are working fine.

here is my pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>ProjectName</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ProjectName</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <surefire.version>2.19.1</surefire.version>
        <surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
        <config.file>default</config.file>
        <java.version>11</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>

    </properties>

    <build>
        <pluginManagement>
            <plugins>

                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>3.3.0</version>
                    <executions>
                        <execution>
                            <id>getClasspathFilenames</id>
                            <goals>
                                <goal>properties</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.0.0-M5</version>
                    <configuration>
                        <skipTests>false</skipTests>
                        <suiteXmlFiles>
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
<!--                        <argLine>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argLine>-->
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>sample-test</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>3.0.0-M5</version>
                        <configuration>
                            <suiteXmlFiles>
                                <suiteXmlFile>testng.xml</suiteXmlFile>
                            </suiteXmlFiles>
                        </configuration>
                    </plugin>
                </plugins>

            </build>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.5</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
        <!--dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>8.3.0</version>
        </dependency-->
        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>8.6.0</version>
        </dependency>


        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.13.0</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.browserstack</groupId>
            <artifactId>browserstack-java-sdk</artifactId>
            <version>LATEST</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>5.0.9</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.1.0.jre8</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire</artifactId>
            <version>3.2.5</version>
            <type>pom</type>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.netty/netty-codec -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-codec</artifactId>
            <version>4.1.106.Final</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.28</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.0.1-jre</version>
        </dependency>


    </dependencies>


</project>

I’m also using the browserstack SDK.
So why im getting the connection refused pointed to http://127.0.0.1:4723/wd/hub?

Thanks

possibly cause with Appium 2.X url changes and wd/url not needed any more → Migrating from Appium 1.x to Appium 2.x - Appium Documentation

1 Like

Changed it to http://127.0.0.1:4723/ but still getting the connection refused.

  1. What is your appium version and how you start it?
  2. You can open in browser to check → http://127.0.0.1:4723/status →
{"value":{"ready":true,"message":"The server is ready to accept new connections","build":{"version":"2.5.1"}}}

accessing http://127.0.0.1:4723/status give me a unable to connect, im not sure about the appium version that i have i rely on browserstack executing things. It working fine as long i ran it via testNG xml file or the run button in intellij which is I believe still in testNG.

Then I ran the appium via terminal Then retried to access the https://127.0.0.1:4723/status and I got “{“value”:{“build”:{“version”:“2.0.0”}}}”
with error message

Encountered internal error running command: Error: Could not find a driver for automationName ‘UIAutomator2’ and platformName ‘ANDROID’. Have you installed a driver that supports those capabilities? Run ‘appium driver list --installed’ to see. (Lower-level error: Could not find installed driver to support given caps)

Then i execute command:

appium driver install [email protected]

after that command i got the installed message

:heavy_check_mark: Installing ‘[email protected]’ using NPM install spec ‘[email protected]’
:warning: Driver “uiautomator2” has 1 potential problem:

  • Driver “uiautomator2” (package appium-uiautomator2-driver) may be incompatible with the current version of Appium (v2.0.0) due to an invalid or missing peer dependency on Appium. Please ask the developer of appium-uiautomator2-driver to add a peer dependency on ^[email protected].
    :information_source: Driver [email protected] successfully installed
  • automationName: UiAutomator2
  • platformNames: [“Android”]

Reinstall the appium to get the latest version appium 2.5.4v now getting the

{“status”:9,“value”:{“error”:“unknown command”,“message”:“The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource”,“stacktrace”:“”}}

Try start Appium server with DEBUG logs to see more in console and start tests again.

here what i got

[HTTP] Request idempotency key: 79ae0207-3c03-4381-bcbf-04ea011e5b03
[HTTP] → POST /session
[HTTP] {“capabilities”:{“firstMatch”:[{}],“alwaysMatch”:{“appium:automationName”:“UIAutomator2”,“appium:resetKeyboard”:“true”,“appium:unicodeKeyboard”:“true”,“platformName”:“ANDROID”}}}
[AppiumDriver@b4c5] Calling AppiumDriver.createSession() with args: [null,null,{“firstMatch”:[{}],“alwaysMatch”:{“appium:automationName”:“UIAutomator2”,“appium:resetKeyboard”:“true”,“appium:unicodeKeyboard”:“true”,“platformName”:“ANDROID”}}]
[AppiumDriver@b4c5] Event ‘newSessionRequested’ logged at 1716366873523 ((Standard Time))
[Appium] Attempting to find matching driver for automationName ‘UIAutomator2’ and platformName ‘ANDROID’
[Appium] The ‘uiautomator2’ driver was installed and matched caps.
[Appium] Will require it at /Users/user_path/.appium/node_modules/appium-uiautomator2-driver
[Appium] Requiring driver at /Users/user_path/.appium/node_modules/appium-uiautomator2-driver/build/index.js
[AppiumDriver@b4c5] Appium v2.5.4 creating new AndroidUiautomator2Driver (v2.0.1) session
[AppiumDriver@b4c5] Checking BaseDriver versions for Appium and AndroidUiautomator2Driver
[AppiumDriver@b4c5] Appium’s BaseDriver version is 9.6.0
[AppiumDriver@b4c5] AndroidUiautomator2Driver’s BaseDriver version is 8.7.3
[AndroidUiautomator2Driver@b7ba] Creating session with W3C capabilities: {
[AndroidUiautomator2Driver@b7ba] “alwaysMatch”: {
[AndroidUiautomator2Driver@b7ba] “platformName”: “ANDROID”,
[AndroidUiautomator2Driver@b7ba] “appium:automationName”: “UIAutomator2”,
[AndroidUiautomator2Driver@b7ba] “appium:resetKeyboard”: “true”,
[AndroidUiautomator2Driver@b7ba] “appium:unicodeKeyboard”: “true”
[AndroidUiautomator2Driver@b7ba] },
[AndroidUiautomator2Driver@b7ba] “firstMatch”: [
[AndroidUiautomator2Driver@b7ba] {}
[AndroidUiautomator2Driver@b7ba] ]
[AndroidUiautomator2Driver@b7ba] }
[AndroidUiautomator2Driver@b7ba] Capability ‘unicodeKeyboard’ changed from string to boolean. This may cause unexpected behavior
[AndroidUiautomator2Driver@b7ba] Capability ‘resetKeyboard’ changed from string to boolean. This may cause unexpected behavior
[AndroidUiautomator2Driver@b7ba (4cde4f1f)] Session created with session id: 4cde4f1f-cae9-448a-a8e3-af31c29d1d42
[UiAutomator2] Neither ‘app’ nor ‘appPackage’ was set. Starting UiAutomator2 without the target application
[UiAutomator2] Deleting UiAutomator2 session
[AppiumDriver@b4c5] Event ‘newSessionStarted’ logged at 1716366873537 (Standard Time))
[AppiumDriver@b4c5] Encountered internal error running command: Error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported. Read Environment variables  |  Android Studio  |  Android Developers for more details
[AppiumDriver@b4c5] at requireSdkRoot (/Users/user_path/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-adb/lib/helpers.js:82:11)
[AppiumDriver@b4c5] at Function.createADB (/Users/user_path/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-adb/lib/adb.ts:71:39)
[AppiumDriver@b4c5] at Object.createBaseADB (/Users/user_path/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/android-helpers.js:133:20)
[AppiumDriver@b4c5] at Object.getDeviceInfoFromCaps (/Users/user_path/.appium/node_modules/appium-uiautomator2-driver/node_modules/appium-android-driver/lib/android-helpers.js:224:29)
[AppiumDriver@b4c5] at AndroidUiautomator2Driver.startUiAutomator2Session (/Users/user_path/.appium/node_modules/appium-uiautomator2-driver/lib/driver.js:349:40)
[AppiumDriver@b4c5] at AndroidUiautomator2Driver.createSession (/Users/user_path/.appium/node_modules/appium-uiautomator2-driver/lib/driver.js:240:18)
[AppiumDriver@b4c5] at AppiumDriver.createSession (/usr/local/lib/node_modules/appium/lib/appium.js:717:35)
[HTTP] ← POST /session 500 39 ms - 816
[HTTP]

opa! run

// for macOS
echo $ANDROID_HOME
/Users/YOUR_USER/Library/Android/sdk
  1. Check or reinstall Android Studio
  2. add into your environment ANDROID_HOME
// for macOS !
export ANDROID_HOME=/Users/YOUR_USER!/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH

Okay, But I should be getting connected to Browserstack sdk right? Why I need to define my sdk and im using intellij.

We talking about only your computer configuration. When you run tests on your machine.

I get it, but there will be another problem arise i believe when i switched to IOS.
I want to ran it on browserstack only. since on my organization machine i dont have a capable to install things so the only way that i can execute the script is via browserstack and I was hoping to get the solution for maven test