Search code examples
androidappiumappium-android

Selenium / Appium 1.7.2 test runs on Android 5.1 but not Android 8.0


I have an Selenium/Appium test against an Android Hybrid Web app that runs if I use Pixel 2 emulator running Android OS 5.1 but does not run on Pixel 2 emulator running Android OS 8.0. I've tried other emulator/os combos and it seems to be a 5.1 vs anything later issue. This also appears to be an issue on physical devices.

This on a login screen under native context.

Android Studio has the latest SDK 27 with latest adv images. Selenium Driver is 3.11.2. Appium Server is 1.7.2 (Desktop 1.5.0).

Appium logs show the uiautomator failed to locate element. However I can manipulate the element using ADB commands regardless of Android OS level I'm running. Here is the driver setup for 5.1:

 DesiredCapabilities capability = new DesiredCapabilities();
 capability.SetCapability("deviceName", "emulator-5554");
 capability.SetCapability("fullReset", "True");
 capability.SetCapability("autoWebView", "true");
 capability.SetCapability("platformName", "Android");
 capability.SetCapability("platformVersion", "5.1"); //ONLY DIFFERENCE
 capability.SetCapability("appiumVersion", "1.7.2");
 capability.SetCapability("app", "c:/users/user/desktop/myapp.apk");
 capability.SetCapability("appWaitActivity", "*");
 return new AndroidDriver<AppiumWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), capability);

Here is the driver setup for 8.0:

 DesiredCapabilities capability = new DesiredCapabilities();
 capability.SetCapability("deviceName", "emulator-5554");
 capability.SetCapability("fullReset", "True");
 capability.SetCapability("autoWebView", "true");
 capability.SetCapability("platformName", "Android");
 capability.SetCapability("platformVersion", "8.0"); // ONLY DIFFERENCE
 capability.SetCapability("appiumVersion", "1.7.2");
 capability.SetCapability("app", "c:/users/user/desktop/myapp.apk"); 
 capability.SetCapability("appWaitActivity", "*");
 return new AndroidDriver<AppiumWebElement>(new Uri("http://127.0.0.1:4723/wd/hub"), capability);

After setup and driver instantiation this is the line that works against Android 5.1 but not against 8.0

Username = _driver.FindElement(By.Id("myapp:id/username"));

I've tried multiple locator strategies, but I'm not confident I implemented them all correctly.

Oddly with either OS version the test can click an accept button on the EULA screen that precedes the login screen.

Accept = _driver.FindElement(By.Id("android:id/button1"));

Any guidance would be greatly appreciated.


Solution

  • Hi Update your code to following one :

    DesiredCapabilities capability = new DesiredCapabilities();
     capability.SetCapability("deviceName", "emulator-5554");
     capability.SetCapability("fullReset", "True");
     capability.SetCapability("autoWebView", "true");
     capability.SetCapability("platformName", "Android");
     capability.SetCapability("platformVersion", "8.0"); // ONLY DIFFERENCE
     capability.SetCapability("appiumVersion", "1.7.2");
     capability.SetCapability("app", "c:/users/user/desktop/myapp.apk"); 
     capability.SetCapability("appWaitActivity", "*");
     capability.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
     return new AndroidDriver<MobileElement>(new Uri("http://127.0.0.1:4723/wd/hub"), capability);
    
    1. There could be couple of reasons your code is failing to locate the element: first one is the the driver type you gave is AppiumWebElement, so it will work for web elements only, hence change it to MobileElement.
    2. Second one is you did not specify the AUTOMATION_NAME capability.

    Hope this works for you.