Search code examples
androidseleniumtestingappium

Why AndroidDriver can not test app on real device after connected?


I'm trying to use Appium to test app on real Android device. The AndroidDriver actually can connect to the app, when I run the code, the app on device will be invoked. But then when I try to use functions such as driver.findElement nothing happened and after waiting a long time, there is an Error communicating with the remote browser.

Here is the code:

public class sampleTest {
    private AndroidDriver driver;

    @Before
    public void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("deviceName","Zachary");
        desiredCapabilities.setCapability("udid","88Y5T19B1793");
        desiredCapabilities.setCapability("platformName","Android");
        desiredCapabilities.setCapability("platformVersion","3.0.0");
        desiredCapabilities.setCapability("appPackage","com.test.test");
desiredCapabilities.setCapability("appActivity","host.exp.exponent.MainActivity");

        URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
//Here the app on device will be opened.
    }

    @Test
    public void SampleTest() {
//Nothing happens here.
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.findElement(By.id("SignIn")).click();

    }

    @After
    public void tearDown() {
        driver.quit();
    }
}

The error message after waiting a long time:

org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.

Solution

    1. Use automationName capability and set it to UiAutomator2

    2. The value of platformVersion capability seems wrong. version 3.0.0 is too old and probably not supported by Appium. The latest Android OS version is 12.0 I believe. Please set it to the correct value. You can login to your device, go to Settings and get the OS version. Also, since you are using udid capability to identify the device, you don't really have to use platformVersion capability. So, you can remove it if you want.

    If still the issue persists, please add a comment with the new information and we will try to solve it.