【问题标题】:Appium Collecting all the text on any screenAppium 收集任何屏幕上的所有文本
【发布时间】:2018-11-08 00:40:36
【问题描述】:

我正在为一个应用程序(iOS 和 android appium 版本 1.7.0)开发翻译自动化。这意味着我应该浏览应用程序的所有页面,然后我使用 gcloud api 来识别语言。

我需要帮助找到一种有效的方法来收集特定屏幕上显示的所有文本。

目前我正在使用这个方法,如下面的代码:

  1. 我找到页面上的所有元素
  2. 我得到了带有 Text 属性的那些

    public void displayText()
    {
    
        System.out.println("i will display all the text and each of their languages");
    
        // I find all the elements on the page as such
        List<MobileElement> list = driver.findElements(By.xpath("//*"));
    
    
        assertTrue(list.size()>0) ;
    
        System.out.println(list.size());
    
        //foreach of the elements detected I determine the language
    
    
    
        for(int i=0;i<list.size();i++)
        { if (list.get(i).getText()!= null) {
            String SeenText = list.get(i).getText();
            System.out.println(SeenText);
    
            //Lang detection
    
        List<Detection> detections 
        =translate.detect(ImmutableList.of(list.get(0).getText()));
        System.out.println("Language(s) detected:");
        for (Detection detection : detections) {
        System.out.printf("\t%s\n", detection);}
    
    
        }}
    
    System.out.println(driver1.getPageSource());
    

但这仅适用于元素很少的页面。内容繁重的分页需要很长时间,以至于 appium 会话超时。

我考虑过手动解析“driver.getPageSource())”的结果,但我不确定这是否可靠。

有什么更好更实用的方法来获取屏幕上的所有文本的想法吗?

谢谢!

【问题讨论】:

    标签: java xpath appium


    【解决方案1】:

    您已经注意到,使用findElements() 将整个 UI 收集为 MobileElements 可能是一个非常繁重的过程,尤其是在使用 XPath 的情况下。

    我的建议是,您可以探索应用程序中通常(或希望仅)包含任何文本内容的元素类类型,并通过 driver.findElements(By.className("")); 获取所有这些元素

    在 Android 上,所有文本内容都使用 TextViews 的可能性很大: driver.findElements(By.className("android.widget.TextView"));

    在 iOS 上,文本内容通常在 XCUIElementTypeStaticText 中找到 driver.findElements(By.className("XCUIElementTypeStaticText"));

    driver.getPageSource() 将是保持验证逻辑高效的最后手段。在这种情况下,您将向 Appium 服务器发送一个请求并立即接收您正在寻找的所有信息。在本地解析 XML 并不繁重,但确实需要一些努力来实现。为了使 driver.getPageSource() 调用内容可靠,您可能应该首先断言页面已完全加载。

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 2021-02-10
      • 1970-01-01
      • 2023-03-10
      • 2018-08-22
      • 2017-12-21
      • 1970-01-01
      • 2016-06-08
      • 2016-11-17
      相关资源
      最近更新 更多