如何使用uiautomator2提高Appium 1.6.3的get toast正确率?

How to improve get toast correct rate on Appium 1.6.3 with uiautomator2?

我尝试使用 Appium 1.6.3 在 android 设备上测试获取 toast 消息,但令我失望的是,获取 toast 的正确率非常低。有人帮帮我吗?

1.It 取决于动态数据的到来方式。 2.如果想在滑动的时候得到吐司数据,那么很难得到准确的数据。

    Step 1:
public static BufferedImage getTOASTSnapShot(AppiumDriver appiumDriver, String event) {
            BufferedImage originalImage=null;
            BufferedImage resizedImage=null;
             {
                try {
                    System.out.println("Starting\n\n\n\n");
                     scrFile = ((TakesScreenshot) appiumDriver).getScreenshotAs(OutputType.FILE);
                     System.out.println("after scrfile\n\n\n\n");
                     originalImage = ImageIO.read(scrFile);
                     System.out.println("after originalFile\n\n\n");

                } catch (Exception) {
                    e.printStackTrace();
                }
            }
            return originalImage;


    Step 2 :
    public BufferedImage clickfortoast(String element)
        {
            try{
            WebElement webElement = appiumDriver.findElement(By.xpath(element));
            webElement.click();
        BufferedImage Toast=    Report.getTOASTSnapShot(appiumDriver, "Click: " );
            System.out.println("Click element: " +element);
            return Toast;
            }
            catch(Exception exception)
            {
                exception.printStackTrace();
                return null;
            }
        }

    Step 3 :
    BufferedImage bufferedImage=dummyPage.clickfortoastimage(PROFILE_PAGE_LOGIN_TO_WALLET_PAGE_SIGN_IN);
            String result1=dummyPage.togettext_out_of_toast(bufferedImage);
            String TOAST_MESSAGE_WRONG_CREDENTIALS= "  " ;
            String mes2=" Toast message to be checked for  to be checked";
            boolean check = StringUtils.containsIgnoreCase(result1,mes1);
                System.out.println(check);
                boolean check1=StringUtils.containsIgnoreCase(result1,mes2);

参考此示例代码片段以使用 appium uiautomator2 驱动程序验证 toast 消息

assertNotNull(wait.until(ExpectedConditions.presenceOfElementLocated(
            By.xpath("//*[@text='Clicked popup menu item Search']"))));

https://github.com/appium/java-client/blob/72b9e6a70d3ebfba9bf6b74813e9644b2f2a9e08/src/test/java/io/appium/java_client/android/UIAutomator2Test.java#L94