在appium android自动化中找不到MobileElement

时间:2018-06-02 10:37:18

标签: java selenium selenium-webdriver appium

我将使用appium

自动化Android应用

CONFIGS:
appium版本:1.8.1
selenium服务器独立:2.53.0
java客户端:4.1.2

实际上当我运行我的脚本时,会出现一些错误,如

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

任何人都可以为此提供解决方案吗?

我的代码:

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("tjagadeesh37@gmail.com");

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

            Login.logiButton(driver).click();

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

        }


  }

}

失败追踪

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)

1 个答案:

答案 0 :(得分:1)

您最有可能使用不兼容的appium服务器版本(1.8.1 最新)和客户端(4.1.2 太旧)。

不要明确设置selenium库(除非你有充分的理由),appium已将它作为dependency =&gt;你可能会引起该行动的问题

appium-java-client 版本更新为 6.0.0

尝试清理代码:

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();