4

When I am trying to instantiate AndroidDriver class it is giving an error. Please help.

Code

import io.appium.java_client.android.AndroidDriver;

public class Testing {
    @Test
    public void testMethod() {
        AndroidDriver driver;
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("deviceName", "samsung-sm_g530h-5554c610");
        cap.setCapability("platformVersion", "4.4.4");
        cap.setCapability("platformName", "Android");
        cap.setCapability(CapabilityType.BROWSER_NAME, "");
        cap.setCapability("appPackage", "com.whatsapp");
        cap.setCapability("appActivity", "com.whatsapp.HomeActivity");
        driver = new AndroidDriver(new URL("127.0.0.1:4723"), cap);
    }
}

// Here is the error enter image description here

RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
Santosh Kumar
  • 81
  • 1
  • 1
  • 11
  • The error has covered part of the code.. Here is it. driver= new AndroidDriver(new URL("http://127.0.0.1:4723"), cap); – Santosh Kumar Nov 26 '15 at 17:45
  • Multiple markers indicates that there are more than one issue.Post the code instead of image. – RockAndRoll Nov 26 '15 at 17:47
  • import io.appium.java_client.android.AndroidDriver; public class Testing { @Test public void testMethod() { AndroidDriver driver; DesiredCapabilities cap = new DesiredCapabilities(); cap.setCapability("deviceName", "samsung-sm_g"); cap.setCapability("platformVersion", "4.4.4"); cap.setCapability("platformName", "Android"); cap.setCapability(CapabilityType.BROWSER_NAME, ""); cap.setCapability("appPackage", "com.whatsapp"); cap.setCapability("appActivity","com.whatsapp.HomeActivity"); driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), cap); } } – Santosh Kumar Nov 26 '15 at 17:49
  • Unable to format the code here. Please see if you can paste it in your eclipse or give me a min .. i m trying to format her. – Santosh Kumar Nov 26 '15 at 17:51
  • updated the code .. please check in the main question body – Santosh Kumar Nov 26 '15 at 17:53

5 Answers5

1

It is giving an error: AndroidDriver is Raw type. You can initialize driver as below:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
...
public class Testing()
{
  public  AppiumDriver driver;
  ...
  @BeforeTest
  public void testMethod()
   {
     driver = new AndroidDriver(new URL(Node), capabilities);
     ...
   }
}
MKay
  • 818
  • 9
  • 32
  • I am new to java and have to know lot of basics. Could you please tell me what is Raw Type? – Santosh Kumar Nov 27 '15 at 07:16
  • 1
    :) I am also not a Java Developer, but googling with relevant keyword will help you understand better, instead I comment it here. e.g. [This SO question](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – MKay Nov 27 '15 at 09:04
1

I also met this problem before, but I now have solved, and the reasons for this problem is because Java - client-(version number). jar is not compatible,So I will Java - client-(version number). jar replacement into Java - the client - 3.1.0. Jar.Hope to be able to help you!

Rye
  • 21
  • 1
1

Try replacing:

driver = new AndroidDriver(new URL("127.0.0.1:4723"), cap);

With:

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
Artemis
  • 2,553
  • 7
  • 21
  • 36
0

You are getting this error as AppiumDriver is now Generic, so it can be set to return elements of class MobileElement or IOSElement or AndroidElement without casting.

This change is introduced in Java client version 3.0 and above. Details can be found here

Also, app package, app activity and device name is sufficient enough to run tests. So, you can modify your code as:

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class Testing {
  AndroidDriver<MobileElement> driver;

 @Test
 public void testMethod() { 
    DesiredCapabilities caps = new DesiredCapabilities() ;
    caps.setCapability(MobileCapabilityType.DEVICE_NAME,"samsung-sm_g530h-5554c610");
    caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.whatsapp");
    caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.whatsapp.HomeActivity");
    driver = new AndroidDriver<MobileElement>(new URL ("http://127.0.0.1:4723/wd/hub"),caps);
 }
}
Smriti
  • 1,552
  • 3
  • 15
  • 29
  • The above code is working with APPIUM JAVA CLIENT 3.1.0 but not with the latest version which is 3.2.0. When I use the above code the below error is being displayed on the same line. The type org.openqa.selenium.remote.service.DriverSrivice$Builder cannot be resolved. It is indirectly referenced from .class files. How to resolve this? – Santosh Kumar Nov 27 '15 at 16:16
  • Try adding 'selenium-remote-driver' and 'selenium-server' dependencies to the pom.xml file followed by mvn eclipse:eclipse. You can read more about "eclipse:eclipse" here - https://maven.apache.org/plugins/maven-eclipse-plugin/eclipse-mojo.html – Smriti Nov 27 '15 at 17:04
0

Following is the correct way to initialize Androidriver:

public class AppiumController{
    public static void main(String[] args) throws MalformedURLException{
        AppiumDriver<?> driver;
        final String URL_STRING = "http://127.0.0.1:4723/wd/hub";
        URL url = new URL(URL_STRING);

        File appDirAndroid = new File("src/main/resources/app/");
        File appAndroid = new File(appDirAndroid, "in.amazon.mShop.android.shopping_2018-02-22.apk");

        DesiredCapabilities androidCapabilities = new DesiredCapabilities();
        androidCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        androidCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
        androidCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
        androidCapabilities.setCapability("appPackage", "in.amazon.mShop.android.shopping");
        androidCapabilities.setCapability("appActivity", "com.amazon.mShop.home.HomeActivity");
        androidCapabilities.setCapability(MobileCapabilityType.APP, appAndroid.getAbsolutePath());

        driver = new AndroidDriver<MobileElement>(url, androidCapabilities);
        driver.closeApp();
    }
}

The above piece of code will successfully launch the amazon app on emulator.

Suraj Jogdand
  • 308
  • 2
  • 17