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

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

根据之前我们了解的swipe的用法,封装为上下左右滑动的这几个方法。
- /**
-
- * This Method for swipe up
-
- *
-
- * @author Young
-
- * @param driver
-
- * @param during
-
- */
-
- public void swipeToUp(AndroidDriver<MobileElement> driver, int during)
-
- {
-
- int width = driver.manage().window().getSize().width;
-
- int height = driver.manage().window().getSize().height;
-
- driver.swipe(width / 2, height * 5 / 8, width / 2, height / 8, during);
-
- }
-
-
-
- /**
-
- * This Method for swipe down
-
- *
-
- * @author Young
-
- * @param driver
-
- * @param during
-
- */
-
- public void swipeToDown(AndroidDriver<MobileElement> driver, int during)
-
- {
-
- int width = driver.manage().window().getSize().width;
-
- int height = driver.manage().window().getSize().height;
-
- driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);
-
- }
-
- /**
-
- * This Method for swipe Left
-
- *
-
- * @author Young
-
- * @param driver
-
- * @param during
-
- */
-
- public void swipeToLeft(AndroidDriver<?> driver, int during)
-
- {
-
- int width = driver.manage().window().getSize().width;
-
- int height = driver.manage().window().getSize().height;
-
- driver.swipe(width * 3 / 4, height / 2, width / 4, height / 2, during);
-
- }
-
-
-
- /**
-
- * This Method for swipe Right
-
- *
-
- * @author Young
-
- * @param driver
-
- * @param during
-
- */
-
- public void swipeToRight(AndroidDriver<MobileElement> driver, int during)
-
- {
-
- int width = driver.manage().window().getSize().width;
-
- int height = driver.manage().window().getSize().height;
-
- driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, during);
-
- // wait for page loading
-
- }
甚至,你还可以把swipe方法封装为查找元素出现,比如有N个页面滑动到最后一个,查找某个页面的元素。
我们也可以封装一个截屏的方法,将自动化过程中需要的关键步骤截图,或者是失败截图。
截图方法,封装TakeScreenshot,保存到指定路径
- /**
-
- * This Method create for take screenshot
-
- *
-
- * @author Young
-
- * @param drivername
-
- * @param filename
-
- */
-
- public static void snapshot(TakesScreenshot drivername, String filename)
-
- {
-
- // this method will take screen shot ,require two parameters ,one is
-
- // driver name, another is file name
-
-
-
- String currentPath = System.getProperty("user.dir"); // get current work
-
- // folder
-
- File scrFile = drivername.getScreenshotAs(OutputType.FILE);
-
- // Now you can do whatever you need to do with it, for example copy
-
- // somewhere
-
- try
-
- {
-
- System.out.println("save snapshot path is:" + currentPath + "/" + filename);
-
- FileUtils.copyFile(scrFile, new File(currentPath + "\\" + filename));
-
- } catch (IOException e)
-
- {
-
- System.out.println("Can't save screenshot");
-
- e.printStackTrace();
-
- } finally
-
- {
-
- System.out.println("screen shot finished, it's in " + currentPath + " folder");
-
- }
-
- }
今天有事就更新到这里,明天将继续更新。敬请期待!
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….
如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加入下方我的qq群大家一起讨论交流,里面也有各种软件测试资料和技术交流。
