2
votes

I am running a simple Calculator test for Android Emulator using Appium + WebDriver in Android Studio. I have manually started Appium Server and Android Emulator and then running test using TestNG.xml.

Here is my sample WebDriver code -

package com.example.user.mysampleapp2;

/**
 * Created by CParmar on 14-09-2017.
 */

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;

public class SampleTests {
    public WebDriver driver;

    @BeforeClass
    public void setUp() throws MalformedURLException {
        // Created object of DesiredCapabilities class.
        DesiredCapabilities capabilities = new DesiredCapabilities();

        // Set android deviceName desired capability. Set your device name.
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus_6_API_25");

        // Set BROWSER_NAME desired capability. It's Android in our case here.
        capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Android");

        // Set android VERSION desired capability. Set your mobile device's OS version.
        capabilities.setCapability(MobileCapabilityType.VERSION, "7.1.1");

        // Set android platformName desired capability. It's Android in our case here.
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);

        // Set android appPackage desired capability. It is
        // com.android.calculator2 for calculator application.
        // Set your application's appPackage if you are using any other app.
        capabilities.setCapability("appPackage", "com.android.calculator2");

        // Set android appActivity desired capability. It is
        // com.android.calculator2.Calculator for calculator application.
        // Set your application's appPackage if you are using any other app.
        capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");

        // Created object of RemoteWebDriver will all set capabilities.
        // Set appium server address and port number in URL string.
        // It will launch calculator app in android device.
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test
    public void testFirstCalculator() {

        // Click on DELETE/CLR button to clear result text box before running test.
        driver.findElements(By.xpath("//android.widget.Button")).get(0).click();

        // Click on number 2 button.
        driver.findElement(By.name("7")).click();

        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
    }

    @AfterClass
    public void End() {
        driver.quit();
    }
}

When I run this from Android Studio I get below Appium Error.

Launching Appium server with command: C:\Program Files (x86)\Appium\node.exe lib\server\main.js --address 127.0.0.1 --port 4723 --platform-name Android --platform-version 23 --automation-name Appium --log-no-color info: Welcome to Appium v1.4.16 (REV ae6877eff263066b26328d457bd285c0cc62430d) info: Appium REST http interface listener started on 127.0.0.1:4723 info: [debug] Non-default server args: {"address":"127.0.0.1","logNoColors":true,"platformName":"Android","platformVersion":"23","automationName":"Appium"} info: Console LogLevel: debug info: --> POST /wd/hub/session {"desiredCapabilities":{"appPackage":"com.android.calculator2","appActivity":"com.android.calculator2.Calculator","browserName":"Android","platformName":"Android","deviceName":"Nexus_6_API_25","version":"7.1.1"},"requiredCapabilities":{},"capabilities":{"desiredCapabilities":{"appPackage":"com.android.calculator2","appActivity":"com.android.calculator2.Calculator","browserName":"Android","platformName":"Android","deviceName":"Nexus_6_API_25","version":"7.1.1"},"requiredCapabilities":{},"alwaysMatch":{"browserName":"Android","platformName":"Android"},"firstMatch":[]}} info: Client User-Agent string: Apache-HttpClient/4.5.3 (Java/1.8.0_131) info: [debug] The following desired capabilities were provided, but not recognized by appium. They will be passed on to any other services running on this server. : version info: [debug] Didn't get app but did get Android package, will attempt to launch it on the device info: [debug] Creating new appium session 42818f6f-0677-486a-bc53-efa31009bbe7 info: Starting android appium info: [debug] Getting Java version info: Java version is: 1.8.0_131 info: [debug] Checking whether adb is present warn: The ANDROID_HOME environment variable is not set to the Android SDK root directory path. ANDROID_HOME is required for compatibility with SDK 23+. Checking along PATH for adb. info: [debug] executing cmd: where adb info: [debug] Using adb from C:\Users\CParmar\AppData\Local\Android\sdk\platform-tools\adb.exe

warn: No app capability, can't parse package/activity info: [debug] Using fast reset? true info: [debug] Preparing device for session info: [debug] Not checking whether app is present since we are assuming it's already on the device info: Retrieving device info: [debug] Trying to find a connected android device info: [debug] Getting connected devices... info: [debug] executing cmd: "C:\Users\CParmar\AppData\Local\Android\sdk\platform-tools\adb.exe" devices info: [debug] 1 device(s) connected info: Found device emulator-5554 info: [debug] Setting device id to emulator-5554 info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5) info: [debug] executing cmd: "C:\Users\CParmar\AppData\Local\Android\sdk\platform-tools\adb.exe" -s emulator-5554 wait-for-device info: [debug] executing cmd: "C:\Users\CParmar\AppData\Local\Android\sdk\platform-tools\adb.exe" -s emulator-5554 shell "echo 'ready'" info: [debug] Starting logcat capture error: Logcat capture failed: spawn "C:\Users\CParmar\AppData\Local\Android\sdk\platform-tools\adb.exe" ENOENT info: [debug] Stopping logcat capture

I have gone through all the available posts on forum and also Set ANDROID_HOME variable with SDK path in Android Studio Run configurations (Android_Home). Still I am receiving this error.

Any help?

Thanks, Chandresh Parmar

2

2 Answers

1
votes

You can follow these answers to fix ANDROID_HOME env variable being set. In the meantime, I suggest to install appium-doctor and check what is missing in your environment setup prior appium tests execution.

In your case:

npm install appium-doctor -g
appium-doctor --android

And response you expect to get is:

### Diagnostic starting ###
✔ The Node.js binary was found at: /usr/local/bin/node
✔ Node version is 7.9.0
✔ ANDROID_HOME is set to: /Users/<user>/Library/Android/sdk
✔ JAVA_HOME is set to: /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
✔ adb exists at: /Users/<user>/Library/Android/sdk/platform-tools/adb
✔ android exists at: /Users/<user>/Library/Android/sdk/tools/android
✔ emulator exists at: /Users/<user>/Library/Android/sdk/tools/emulator
✔ Bin directory of $JAVA_HOME is set
### Diagnostic completed, no fix needed. ###

Fix all the x marks and good luck

0
votes

From the error logs, you can clearly see that, ANDROID_HOME environment variable is not set.

For example : You have installed the Android SDK in "C:\Users\YourUserName\AndroidSDK" then the value of ANDROID_HOME path will be the same path.

Just add new environment variable with name as ANDROID_HOME and value as your path.

And also your appium version very old(1.4.16). I suggest you to upgrade it to 1.6.3 which is very stable version of appium as of now