5 java -appium 找toast找不到

各位大侠,java -appium 找toast找不到,代码如下,环境应该都配对了,把text改成很容易找到的元素,就能找到,换成toast的text就找不到,为什么?谢谢,代码好下:

package com.iteach.zed.a03.main;


import java.io.File;

import java.net.URL;

import java.text.SimpleDateFormat;

import java.util.Date;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.remote.DesiredCapabilities;


import io.appium.java_client.android.AndroidDriver;

import io.appium.java_client.android.AndroidElement;


public class A03InitDriver2 {

public static SimpleDateFormat df = new SimpleDateFormat("MM-dd HH-mm-ss");

public static String time=df.format(new Date());

public static File logFile=new File( "E:\\A03Log\\"+time+" A03ATLog.txt");

public static WebDriver initDriver() throws Exception {

DesiredCapabilities caps=new DesiredCapabilities();

caps.setCapability("deviceName", "huawei");

caps.setCapability("appPackage", "com.iteach.zed.a03");

caps.setCapability("appActivity", "com.iteach.zed.a03.activity.LoginActivity");

caps.setCapability("noReset", true);//不对app重置,这个比较重要,根据实际需要进行设置

caps.setCapability("unicodeKeyboard", true);//用appium自带的输入法,无UI

caps.setCapability("resetKeyboard", true);

caps.setCapability("newCommandTimeout", 10000);

//caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);

caps.setCapability("automationName", "UiAutomator2");

System.out.println("121123123");

AndroidDriver<AndroidElement> driver=new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),caps);

Thread.sleep(2000);

//WriteToTxt.logWriteToTxt(df.format(new Date())+"   "+"app启动成功");

return driver;

}

public static void main(String[] args) {

try {

A03InitDriver2.initDriver();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

package com.iteach.zed.a03.main;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

import com.iteach.zed.a03.utils.AndroidDriverWait;
import com.iteach.zed.a03.utils.ExpectedCondition;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

public class Toast {
public static AndroidDriver<AndroidElement> driver;
    public  Toast(AndroidDriver<AndroidElement> driver) {
this.driver=driver;
    }
    public static void getToast() throws Exception {
AndroidElement ele = driver.findElementById("com.iteach.zed.a03:id/login_phone");
ele.click();
ele.sendKeys("ZedchaorenA1029");
Thread.sleep(1000);
driver.findElementById("com.iteach.zed.a03:id/login_password").sendKeys("2");
driver.findElementById("com.iteach.zed.a03:id/login_phone").click();
driver.findElementById("com.iteach.zed.a03:id/login_btn").click();
String toast="账户与密码不匹配,请重新输入账号密码!";
//        String toast="全制式便携侦码仪";
//        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//        driver.findElement(By.xpath(".//*[contains(@text,'"+ toast + "')]"));
try {
WebDriverWait wait=new WebDriverWait(driver, 10);
AndroidElement element=(AndroidElement) wait.until(
ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[contains(@text,'"+ toast + "')]")));
System.out.println("找到toast");
} catch (Exception e) {  
throw new AssertionError("找不到"+toast);  
}
请先 登录 后评论

1 个回答

桃之妖妖 - 从2.5小白开始

参考下 http://ask.testfan.cn/article/960

请先 登录 后评论