1.toast弹窗,普通方式不能获取

例如使用getPageSource是无法找到toast的信息,uiautomatorViewer加载页面时间较长,也很难采集到toast信息

2.通过curl命令探测toast

for i in `seq 1 1000`;do
date
curl -X POST http://localhost:4723/wd/hub/session/d3608ba7-08fd-4f2c-8e24-d0e58cf8f05c/elements --data-binary '{"using":"xpath","value":"//*[@class=\"android.widget.Toast\"]"}' -H "Content-Type:application/json;charset=UTF-8"
sleep 0.5
done

3.Uiautomator2对toast的支持

//Uiautomator2对toast的处理,尝试从当前界面去找toast,如果发现toast,就将toast生成一个节点(只给了text, className="android.widget.Toast", packageName="com.android.settings"3个属性)加到appium的元素树里,
private void addToastMsgRoot(CharSequence tokenMSG){
AccessibilityNodeInfo node = AccessibilityNodeInfo.obtain();
node.setText(tokenMSG);
node.setClassName(Toast.class.getName());
node.setPackageName("com.android.settings");
this.children.add(new UiAutomationElement(node));
}

3.1根据以上代码,可以使用xpath查找

  • 通过//*[@class='android.widget.Toast']
  • 通过//*[contains(@text,"xxxx")]
  • 通过//*[@package="com.android.settings"] 使用包名去匹配toast时,注意不要和当前应用冲突,例如设置

3.2使用Java脚本探测

TestToast.java代码

#java
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities; import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit; public class TestToast {
private AndroidDriver driver;
@Before
public void setUp() throws MalformedURLException { DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName","android");
desiredCapabilities.setCapability("deviceName","demo");
desiredCapabilities.setCapability("appPackage","io.appium.android.apis");
desiredCapabilities.setCapability("appActivity",".ApiDemos");
desiredCapabilities.setCapability("noReset",true);
desiredCapabilities.setCapability("newCommandTimeout",120);
desiredCapabilities.setCapability("automationName","UiAutomator2");
URL RemoteURL = new URL("http://localhost:4723/wd/hub");
driver = new AndroidDriver(RemoteURL,desiredCapabilities);
driver.manage().timeouts().implicitlyWait(20000,TimeUnit.SECONDS); }
public boolean Exist(String str) throws InterruptedException{
try{
driver.findElementByXPath(str);
return true;
}catch (Exception e){
return false;
} }
@Test
public void test() throws InterruptedException{
SwipeClass swipe = new SwipeClass();
driver.findElementByXPath("//*[@text='Views']").click();
for(int i=0;i<5;i++){
if (Exist("//*[@text=\"Popup Menu\"]")){
driver.findElementByXPath("//*[@text=\"Popup Menu\"]").click();
break;
}else {
swipe.swipeToUp(driver);
Thread.sleep(1000);
}
}
Thread.sleep(2000);
driver.findElementByXPath("//*[@text=\"MAKE A POPUP!\"]").click();
Thread.sleep(1000);
driver.findElementByXPath("//*[@text=\"Search\"]").click();
Thread.sleep(1000);
System.out.println(driver.findElementByXPath("//*[@class=\"android.widget.Toast\"]").getText());
//System.out.println(driver.findElementByXPath("//*[@package=\"com.android.settings\"]").getText());
//System.out.println(driver.findElementByXPath("//*[contains(@text,'Clicked popup')]").getText());
}
@After
public void tearDown(){
driver.quit();
}
}

4.扩展:自动化滚动

官方示例

java_client/android/AndroidSearchingTest.java

  • 方法1
    public void testAutoSwipe1(){
System.out.println("MethodName: "+Thread.currentThread().getStackTrace()[1].getMethodName());
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@text='Views']")));
driver.findElementByXPath("//android.widget.TextView[@text=\"Views\"]").click();
WebElement radioGroup = driver
.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"
+ ".resourceId(\"android:id/list\")).scrollIntoView("
+ "new UiSelector().text(\"Popup Menu\"));");
driver.findElementByXPath("//*[@text='Popup Menu']").click();
driver.findElementByXPath("//*[@text=\"MAKE A POPUP!\"]").click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@text=\"Search\"]")));
driver.findElementByXPath("//*[@text=\"Search\"]").click();
System.out.println(driver.findElementByXPath("//*[@package=\"com.android.settings\"]").getText());
}


* 方法2,滑动找到元素直接点击
```#java
public void testAutoSwipe2(){
System.out.println("MethodName: "+Thread.currentThread().getStackTrace()[1].getMethodName());
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@text='Views']")));
driver.findElementByXPath("//android.widget.TextView[@text=\"Views\"]").click();
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"
+".resourceId(\"android:id/list\")).scrollIntoView("
+"new UiSelector().text(\"Popup Menu\"));").click();
driver.findElementByXPath("//*[@text=\"MAKE A POPUP!\"]").click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@text=\"Search\"]")));
driver.findElementByXPath("//*[@text=\"Search\"]").click();
System.out.println(driver.findElementByXPath("//*[@class=\"android.widget.Toast\"]").getText());
}
```

  • 方法3
    public void testAutoSwipe3() throws InterruptedException{
System.out.println("MethodName: "+Thread.currentThread().getStackTrace()[1].getMethodName());
WebDriverWait wait = new WebDriverWait(driver,10);
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//android.widget.TextView[@text=\"Views\"]")));
driver.findElementByXPath("//android.widget.TextView[@text=\"Views\"]").click();
WebElement list = driver.findElement(By.id("android:id/text1"));
MobileElement webview = list.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+"new UiSelector().text(\"Popup Menu\"));"));
driver.findElementByXPath("//android.widget.TextView[@text=\"Popup Menu\"]").click();
Thread.sleep(1000);
driver.findElementByXPath("//*[@text=\"MAKE A POPUP!\"]").click();
Thread.sleep(1000);
driver.findElementByXPath("//*[@text=\"Search\"]").click();
Thread.sleep(1000);
System.out.println(driver.findElementByXPath("//*[contains(@text,'Clicked popup')]").getText()); }

FAQ

1.自动滚动去寻找元素,会提示引用类型错误的提示


解决办法:只要将MobileElement替换为WebElement即可

appium 3-31626 toast识别的更多相关文章

  1. python3+Appium自动化04-Toast元素识别

    什么是toast? 如下图,“再按一次退出程序”,这就是toast 如何定位toast元素? Appium1.6.3开始支持识别Toast内容,主要基于UiAutomator2 想定位toast元素, ...

  2. python+Appium自动化:toast定位

    Toast简介 Toast是一种简易的消息提示框. 当视图显示给用户,在应用程序中显示为浮动.和Dialog不一样的是,它永远不会获得焦点,无法被点击. 用户将可能是在中间键入别的东西.Toast类的 ...

  3. Python+Appium自动化测试(13)-toast定位

    一,前言 在app自动化测试的过程中经常会遇到需要对toast进行定位,最常见的就是定位toast或者获取toast的文案进行断言,如下图,通过定位"登录成功"的toast就可以断 ...

  4. 解决Appium 抓取toast

    首先我们先看看这个gif,图中需要,要抓取的字符串--->请输入转让份数 1.要导入java-client-5.0.0-SNAPSHOT.jar 包的地址:链接:http://pan.baidu ...

  5. robotframework + appium 获取android toast

    android toast 获取主要方式是在出现toast的时候查找元素:xpath=//*[contains(@text,'记同步')]  ,该xpath 表示为toast信息含有  "记 ...

  6. python appium 封装获取toast方法

    获取toast text封装,传入toast 部分文本,返回全部文本 def get_toast_text(self,text): try: toast_loc = (By.XPATH, " ...

  7. appium简单使用

    App 测试通常会用到的工具 adb :Android 的控制工具,用于获取Android的各种数据和控制 Appium Desktop:内嵌了Appium Server和Inspector的综合工具 ...

  8. Appium之测试微信小程序

    坚持原创输出,点击蓝字关注我吧 作者:清菡 博客:Oschina.云+社区.知乎等各大平台都有. 目录 一.往期回顾 二.测试微信小程序 1.准备工作 2.操作步骤 3.注意 4.强制设置安卓的进程 ...

  9. 测试需要了解的技术之基础篇四__UI自动化测试体系

    UI自动化测试体系 1.Andriod 自动化测试:Appium 环境安装与架构介绍.Appium Desktop用例录制.Appium测试用例流程.元素定位方法 IA/AID/XPATH/UISel ...

随机推荐

  1. L1-013 计算阶乘和

    对于给定的正整数N,需要你计算 S=1!+2!+3!+...+N!. 输入格式: 输入在一行中给出一个不超过10的正整数N. 输出格式: 在一行中输出S的值. 输入样例: 3 输出样例: 9   #i ...

  2. 不使用ref

    为什么 尽量避免ref? 使用ref原因:react功能来访问DOM元素,这种功能的需求往往来自于提交表单的操作,再提交表单的时候,需要读取当前表单中input元素的值 而react的产生就是为了避免 ...

  3. prop和state的区别

    1.prop用于定义外部接口,state用于记录内部状态: 2.prop的赋值在外部世界使用组件时,state的赋值在组件内部: 3.组件不应该改变prop的值,但是state的存在目的就是让组件来改 ...

  4. 弹出的ViewController半透明效果

    在第一个ViewController做如下设置 let controller = UIStoryboard(name: "Main", bundle: nil).instantia ...

  5. 解决tensorflow的"Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA Using TensorFlow backend."警告问题

    问题描述 程序开始运行的时候报出警告:I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructio ...

  6. FreeOpcUa compile

    /********************************************************************************* * FreeOpcUa compi ...

  7. Hihocoder1883 : 生成树问题(并查集+树剖+线段树)

    描述 有一个无向图,有n个点,m1条第一类边和m2条第二类边.第一类边有边权,第二类边无边权.请为第二类的每条边定义一个边权,使得第二类边可能全部出现在该无向图的最小生成树上,同时要求第二类边的边权总 ...

  8. Future接口和FutureTask类【FutureTask实现了Runnable和Future接口】

    Future API: public interface Future<V> { /** * Attempts to cancel execution of this task. This ...

  9. BZOJ4310: 跳蚤 【后缀数组+二分】

    Description 很久很久以前,森林里住着一群跳蚤.一天,跳蚤国王得到了一个神秘的字符串,它想进行研究.首先,他会把串 分成不超过 k 个子串,然后对于每个子串 S,他会从S的所有子串中选择字典 ...

  10. hasura graphql-engine v1.0.0-alpha26 版本新功能试用

      hasura graphql-engine v1.0.0-alpha26 已经发布了,有好多新的变动,测试使用docker 环境,同时pg 数据库使用了citus citus 是一个方便扩展的pg ...