[英]MobileElement was not found in appium android automation
I am going to automate the android app using appium
我将使用
appium
自动化android应用
Configs:
CONFIGS:
appium version: 1.8.1 Appium版本:1.8.1
selenium server standalone: 2.53.0 硒服务器独立版:2.53.0
java client: 4.1.2 Java客户端: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("tjagadeesh37@gmail.com");
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)
You are most likely using incompatible version of appium server (1.8.1 is the latest ) and client (4.1.2 is too old ). 您最有可能使用了不兼容版本的appium服务器( 最新版本为1.8.1)和客户端(4.1.2版本太旧 )。
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 不要显式设置硒库 (除非您有很强的理由),appium已经将其作为依赖项=>可能导致该操作出现问题
Update appium-java-client version to 6.0.0 将appium-java-client版本更新为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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.