基于数据驱动的web UI自动化测试

来源:互联网 发布:夏天手工坊淘宝网 编辑:程序博客网 时间:2024/06/08 23:39

简介

使用testng的dataprovider注解做数据驱动测试,在接口测试中使用的比较普遍,在基于UI自动化测试中其实一样可以收获奇效。下面以工作用写的一个非常普遍的测试case为例,简要说一下。

基本功能:重置安保问题

主要包括下面两个页面:验证安保问题和输入新的安保问题


步骤

  • 核心代码

@Test(dataProvider = "aa" )public void resetSecurityQuestionTest(Map<String, String> data) {securityPage.resetSecurityQuestion(data.get("answer"),data.get("question"),data.get("newanswer"),data.get("flag"),data.get("expectText"));}@DataProvider(name = "aa" )public Iterator<Object[]> dataFortestMethod(Method method) throws IOException {   return new ExcelDataProvider(this.getClass().getName(),method.getName());}


  • 数据准备


记住“4个保持一致”就好:excel文件名和测试类名保持一致,excel sheet名和测试方法名保持一致,代码中引用的参数和数据表的表头保持一致

  • 测试过程
执行上述测试case的时候,会依次从excel表格中读取每一行,作为参数执行一次测试。上图有6条测试数据,所以执行过程中,会依次执行6次测试,对应打开的如下6个页面:

  •  测试结果

总结

操作和数据彻底分离,结构清晰。

UI自动化给回归测试带来了很大的便利,数据驱动使得测试的完备性得到极大提升。


0 0