Jagadeesh
Jagadeesh

Reputation: 398

MobileElement was not found in appium android automation

I am going to automate the android app using appium

Configs:
appium version: 1.8.1
selenium server standalone: 2.53.0
java client: 4.1.2

Actually when i run my script, some error is coming like

org.openqa.selenium.remote.RemoteWebElement cannot be cast to io.appium.java_client.MobileElement

can anyone please give a solution for this?

My code:

public class Sample {

//public static WebDriver driver= null; 



    AndroidDriver driver;

  @Before
    public void setup() throws MalformedURLException {

        DesiredCapabilities capabilities= new DesiredCapabilities();
        capabilities.setCapability("deviceName", "HKL3LA2M");
        capabilities.setCapability(CapabilityType.PLATFORM, "Android");
        capabilities.setCapability("platformVersion","8");
        capabilities.setCapability("appPackage", "com.manash.purplle");
        capabilities.setCapability("appActivity", "com.manash.purplle.activity.SplashActivity");
        capabilities.setCapability("noReset", "true");


        // File file=new File("/home/chinna/Downloads", "purplleAndroid-1.8.2.test3.apk");
        //  capabilities.setCapability("app", file.getAbsolutePath());

        driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    }

  @After
  public void tearDown() {

      driver.quit();

  }
  @Test
  public void testMethod() throws InterruptedException {
     /* WebDriverWait wait = new WebDriverWait(driver, 30);
      wait.until(ExpectedConditions.elementToBeClickable(By
              .id("com.android.packageinstaller:id/permission_allow_button")));
      */


        MobileElement smilyIcon=(MobileElement) (new WebDriverWait(driver,60)).until(ExpectedConditions.presenceOfElementLocated(By.id("com.manash.purplle:id/profile_overflow")));
        smilyIcon.click();
        //driver.findElementById("com.manash.purplle:id/profile_overflow").click();

        /*MobileElement smilyIcon= (MobileElement) driver.findElement(By.id("com.manash.purplle:id/profile_overflow"));
        smilyIcon.click();*/
        driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);

        WebElement parentElement=driver.findElement(By.className("android.widget.ListView"));


        List<WebElement> childElements = parentElement.findElements(By.id("com.manash.purplle:id/title"));
        System.out.println("|__________________________|");
        System.out.println("  Smily popup has " + childElements.size() + " links");
        System.out.println("|__________________________|");

        String expected= "Logout";

        String actual=childElements.get(9).getText();

        if(expected.equals(actual)){

            childElements.get(9).click();
            // Logout.logoutButton(driver).click();

            Logout.logoutButtonAlertYes(driver).click();

        }else{

            childElements.get(9).click();

            Login.username(driver).sendKeys("[email protected]");

            Login.password(driver).sendKeys("1234567890");

            Login.logiButton(driver).click();

            System.out.println("Successfully logged in");

        }


  }

}

Failure trace:

java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to io.appium.java_client.MobileElement
at app_automation.Sample.testMethod(Sample.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

Upvotes: 0

Views: 3200

Answers (1)

dmle
dmle

Reputation: 3658

You are most likely using incompatible version of appium server (1.8.1 is the latest) and client (4.1.2 is too old).

Do not set selenium library explicitly (unless you have strong reasons for it), appium already have it as dependency => you may cause issue with that action

Update appium-java-client version to 6.0.0

Try to cleanup your code:

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //set implicit wait first
WebDriverWait wait = new WebDriverWait(driver, 60); // now proceed with explicit wait 
WebElement smilyIcon = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("profile_overflow"))); // no need to put package, will be handled automatically; no need in casting as well, but try with latest client library - I had no issues on my side
smilyIcon.click();

Upvotes: 1

Related Questions