• 技术分享 | app自动化测试(Android) 特殊控件 Toast 识别


    原文链接

    本文节选自霍格沃兹测试开发学社内部教材

    Toast 是 Android 系统中的一种消息框类型,它属于一种轻量级的消息提示,常常以小弹框的形式出现,一般出现 1 到 2 秒会自动消失,可以出现在屏幕上中下任意位置。它不同于 Dialog,它没有焦点。Toast 的设计思想是尽可能的不引人注意,同时还向用户显示信息希望他们看到。

    测试 APP 下载地址:

    https://github.com/appium/sample-code/raw/master/sample-code/apps/ApiDemos/bin/ApiDemos-debug.apk

    首先将上面地址的 apk 包下载到本地,并安装到模拟器中;在模拟器中打开 API Demos,依次点击“Views”-“Popup Menu”-"Make a Popup"-"Search",就会弹出消息提示框,如图:

    image|800x374

    上图中 “Clicked popup menu item Search” 就是 Toast,但它通常在页面上停留的时间只有 2 秒左右,通过 Appium Inspector 一般不容易获取到这个元素。

    获取Toast

    在模拟器中打开 API Demos 应用,依次点击 “Views”-“Popup Menu”-"Make a Popup"-"Search",查看页面 Toast 元素。

    示例代码如下:

    # 设置 capabilities
    caps = {}
    caps["platformName"] = "Android"
    caps["appPackage"] = "io.appium.android.apis"
    caps["appActivity"] = ".ApiDemos"
    #必须使用uiautomator2框架
    caps["automationName"] = "uiautomator2"
    caps["deviceName"] = "hogwarts"
    # 与Appium Server 建立连接
    driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
    # 设置隐式等待
    driver.implicitly_wait(5)
    
    # 点击 Views
    driver.find_element_by_accessibility_id("Views").click()
    # 滑动页面
    TouchAction(driver).press(380, 1150)\
      .move_to(380, 150).release().perform()
    # 点击 `Popup Menu` 项目
    driver.find_element_by_xpath(
      "//*[@content-desc='Popup Menu']").click()
    # 点击 `Make a Popup`
    driver.find_element_by_xpath(
      "//*[@content-desc='Make a Popup!']").click()
    # 点击 'Search'
    driver.find_element_by_xpath("//*[contains(@text,'Search')]").click()
    toastXPath = "//*[@class='android.widget.Toast']"
    #打印 toastXPath
    print(driver.find_element_by_xpath(toastXPath))
    #打印 toastXPath 获取的 text
    print(driver.find_element_by_xpath(toastXPath).text)
    
    
    @BeforeAll
    public static void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appPackage", "io.appium.android.apis");
        desiredCapabilities.setCapability("appActivity", ".ApiDemos");
        desiredCapabilities.setCapability("automationName", "uiautomator2");
        desiredCapabilities.setCapability("deviceName", "hogwarts");
    
        URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    
    @Test
    public void toastTest() {
        //点击 Views
        driver.findElement(MobileBy.AccessibilityId("Views")).click();
        //滑动页面
        TouchAction action = new TouchAction(driver);
        PointOption pressPointOne = PointOption.point(380, 1150);
        PointOption movePointOne = PointOption.point(380, 150);
        action.press(pressPointOne).moveTo(movePointOne).release();
        //点击 `Popup Menu` 项目
        driver.findElement(By.xpath("//*[@content-desc='Popup Menu']")).click();
        //点击 `Make a Popup`
        driver.findElement(By.xpath("//*[@content-desc='Make a Popup!']")).click();
        //点击 'Search'
        driver.findElement(By.xpath("//*[contains(@text,'Search')]")).click();
        By toastXPath = By.xpath("//*[@class='android.widget.Toast']");
        //打印 toastXPath
        System.out.println(driver.findElement(toastXPath));
        //打印 toastXPath 获取的 text
        System.out.println(driver.findElement(toastXPath).getText());
    }
    
    

    这里定位 Toast 使用了 Xpath 表达式进行定位,因为 Toast 的 class 属性比较特殊,在当前页面上一般会出现一次 class="android.widget.Toast" 的元素,所以使用 Xpath 定位方式搭配隐式等待就可以很轻松的可以定位到。

    查看执行结果

    ⬇️ 你好呀,喜欢这篇文章的话烦请点个“赞”哦!大家的支持很重要~() PS:有问题可以联系我们哦~v ceshiren001

    >>更多技术文章分享和免费资料领取

  • 相关阅读:
    iis应用程序池定时自动回收
    js获取1100之间的随机整数
    一个简单的方法实现ASP.NET网站实现http访问强制转https(不需要URL Rewrite)
    js 生成的html class属性失效问题
    此网站无法提供安全连接(客户端和服务器不支持一般 SSL 协议版本或加密套件。)TLS 1.1/TLS 1.2配置
    nextcloud & aria2 搭建 芒果
    AndroidStudio开发Flutter使用技巧
    Jenkins配置gitlab合并分支后自动构建
    Python 中 import module 和 package 方法简单记录
    https域名证书cer转pem格式
  • 原文地址:https://www.cnblogs.com/hogwarts/p/16403959.html
Copyright © 2020-2023  润新知