• Appium入门自动化测试(6)—— Appium 常用方法的自己动手封装


    Appium 常用方法的自己动手封装

    前言

    阅读此文大概需要5分钟,之后需要自己动手实践。

    之前我们已经对Appium的一些常用的API有所了解,在实际测试过程中,大多数自动化测试工程师,尤其是UI自动化测试工程师,遇到更多的问题是Element经常变化和代码复用与维护,有句话叫开发手一抖,auto忙一宿。说的就是Element如果有改变,自动化测试工程师就需要花很多时间和精力维护。而如何能让你的自动化测试体系更容易维护,就需要一个科学合理的测试框架体系。今天我们先不谈框架体系,先去做一些框架基础,先去做基本方法的封装。我希望我能给你带来一些思路。

    在测试过程中,一定会遇到很多的手势操作,比如向上下左右滑动屏幕,虽然Appium给出一个swipe方法,但这个方法比较原始,对于实际测试中需要进一步优化,这样我们就可以抽象出几个swipe的方法,专门针对上下左右滑动屏幕。首先获取屏幕的尺寸,可以把手机屏幕当作一个二维坐标体系。如下图:

     

    swipe方法封装

    根据之前我们了解的swipe的用法,封装为上下左右滑动的这几个方法。

    1.  /**
    2.      * This Method for swipe up
    3.      * 
    4.      * @author Young
    5.      * @param driver
    6.      * @param during
    7.      */
    8.     public void swipeToUp(AndroidDriver<MobileElement> driver, int during)
    9.     {
    10.         int width = driver.manage().window().getSize().width;
    11.         int height = driver.manage().window().getSize().height;
    12.         driver.swipe(width / 2, height * 5 / 8, width / 2, height / 8, during);
    13.     }
    14.     /**
    15.      * This Method for swipe down
    16.      * 
    17.      * @author Young
    18.      * @param driver
    19.      * @param during
    20.      */
    21.     public void swipeToDown(AndroidDriver<MobileElement> driver, int during)
    22.     {
    23.         int width = driver.manage().window().getSize().width;
    24.         int height = driver.manage().window().getSize().height;
    25.         driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);
    26.     }
    27.  /**
    28.      * This Method for swipe Left
    29.      * 
    30.      * @author Young
    31.      * @param driver
    32.      * @param during
    33.      */
    34.     public void swipeToLeft(AndroidDriver<?> driver, int during)
    35.     {
    36.         int width = driver.manage().window().getSize().width;
    37.         int height = driver.manage().window().getSize().height;
    38.         driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, during);
    39.     }
    40.     /**
    41.      * This Method for swipe Right
    42.      * 
    43.      * @author Young
    44.      * @param driver
    45.      * @param during
    46.      */
    47.     public void swipeToRight(AndroidDriver<MobileElement> driver, int during)
    48.     {
    49.         int width = driver.manage().window().getSize().width;
    50.         int height = driver.manage().window().getSize().height;
    51.         driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, during);
    52.         // wait for page loading
    53.     }

    甚至,你还可以把swipe方法封装为查找元素出现,比如有N个页面滑动到最后一个,查找某个页面的元素。

    我们也可以封装一个截屏的方法,将自动化过程中需要的关键步骤截图,或者是失败截图。

    截图方法,封装TakeScreenshot,保存到指定路径

    1.  /**
    2.      * This Method create for take screenshot
    3.      * 
    4.      * @author Young
    5.      * @param drivername
    6.      * @param filename
    7.      */
    8.     public static void snapshot(TakesScreenshot drivername, String filename)
    9.     {
    10.         // this method will take screen shot ,require two parameters ,one is
    11.         // driver name, another is file name
    12.         String currentPath = System.getProperty("user.dir"); // get current work
    13.                                                              // folder
    14.         File scrFile = drivername.getScreenshotAs(OutputType.FILE);
    15.         // Now you can do whatever you need to do with it, for example copy
    16.         // somewhere
    17.         try
    18.         {
    19.             System.out.println("save snapshot path is:" + currentPath + "/" + filename);
    20.             FileUtils.copyFile(scrFile, new File(currentPath + "\\" + filename));
    21.         } catch (IOException e)
    22.         {
    23.             System.out.println("Can't save screenshot");
    24.             e.printStackTrace();
    25.         } finally
    26.         {
    27.             System.out.println("screen shot finished, it's in " + currentPath + " folder");
    28.         }
    29.     }

    今天有事就更新到这里,明天将继续更新。敬请期待!


    最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

    这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….

    如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加入下方我的qq群大家一起讨论交流,里面也有各种软件测试资料和技术交流。

  • 相关阅读:
    中枢听觉处理障碍的行为干预方法
    第四章:人工智能深度学习教程-激活函数(第三节-Pytorch 中的激活函数)
    夏天的诗歌
    油田勘探问题
    网安等保-Linux服务器之最新Ubuntu-22.04-LTS系统内核优化与安全加固配置脚本使用分享
    最近公共祖先算法详解 + 模板题 建议新手收藏 信息学奥赛一本通 祖孙询问
    热成像夜视仪和夜视仪的优缺点
    Java学习笔记(三十六)
    一文学习yolov5 实例分割:从训练到部署
    HTTP和HTTPS
  • 原文地址:https://blog.csdn.net/m0_58026506/article/details/126991235