目录
前言:
背景
需求
分析
解决思路
解决方案
测试流程图
实现的功能
用例代码
两种测试方式
随机测试
指定测试
总结
前言:
Selenium是一个广泛使用的自动化测试工具,用于Web应用程序的测试。它提供了一组功能强大的API,可以模拟用户在浏览器中的操作,如点击、输入、选择等。通过使用Selenium,测试人员可以自动执行各种测试用例,以验证Web应用程序的功能和用户体验。
在黑盒测试中,测试人员只关注应用程序的输入和输出,而不考虑内部的实现细节。这种测试方法可以模拟真实用户的行为,从而更全面地评估应用程序的功能和性能。
报表是许多Web应用程序中常见的功能之一,它用于展示和分析数据。在报表自动化测试中,测试人员使用Selenium来模拟用户在应用程序中生成和查看报表的操作。通过自动化测试,可以确保报表的正确性、可靠性和可用性。
背景
公司的 ERP 系统有很多报表页面,每个月都会有一两个报表的功能进行修改,页面查询条件很多,
功能测试的同事尽可能的穷举出各种参数组合去查询,但由于参数组合的数量太大,人工测试的用例只能算九牛一毛,
依然有大部分漏测的场景可能发现问题,上线之后被用户投诉。
基于这个背景,引入自动化测试,旨在通过程序穷举出各种查询场景自动进行测试,并记录各场景的测试结果。
需求
从背景中收集到以下需求:
- 页面上有很多查询条件
- 穷举查询条件的组合
- 记录各组合的测试结果
分析
先看看页面长什么样子。
由此页面我们可以得出参数的大致列表:
<aa></aa>
<bb></bb>
<cc></cc>
<dd></dd>
<ee></ee>
……
我们把这些参数进行组合,(此处省略 N 字。。。组合公式自行去查阅资料)可以得到以下数量
组合数量 = 2的参数个数次幂-1
假设页面有 3 个参数 aa,bb,cc,那么组合出来的参数为:{[aa], [bb], [cc], [aa,bb], [aa,cc], [bb,cc], [aa,bb,cc]}
如果页面有 10 个参数,则这些参数组合的数量为:2 的 10 次方-1 = 1023
但上面求出来的数量仅仅只是参数的值为固定数量的组合个数,但实际上某个参数的取值可能是以下情况:
<aa>value1,value2,value3</aa>
or
<aa>value1</aa>
or
<aa>value1,value2</aa>
or
……
我们可以得知
页面条件组合出来的数量无穷大!!!
解决思路
从实际场景出发,了解业务初衷
我们从上面的分析可以得出一个结论,这是一个看似无法完成的任务。怎么把一个理论上来说不可能完成
的任务,变成一个可行的解决方案,就需要我们对实际的业务进行分析了。
先说自动化面临的问题:
- 自动化用例量庞大。假设页面有 10 个参数,每个参数固定一个值,则有 1023 个组合
- 自动化用例耗时长。一个完整的页面输入参数点击查询的用例大概需要 5~10 秒的时间,再乘以以千为单位的用例个数。。。 不说其他,单这两项就够秒杀该功能不适合做自动化的。 怎么办呢?竟然正常的路走不通,那我们就想点非常规的路子。
咱们不妨抛开业务,想想什么自动化用例才是好用例:
- 脚本通用,一份脚本支持所有用例执行。
- 脚本执行时间短,最好分分钟跑完。
- 覆盖更多测试场景,这个当然是越多越好。
回到正题,竟然这个业务本身就非常特殊,我们就没有必要去遵循自动化用例的设计原则了,直接按照我们心目中的完美用例去实现。
- 脚本通用。我们可以写一份脚本,然后通过循环读取参数去页面查询。
- 执行时间。如此庞大的用例量自然无法通过人工来写的,我们就想办法通过程序去自动生成用例。
- 覆盖度高。从业务角度来设置查询场景,如:参数全设置查询,参数不设置查询,等各种场景。
解决方案
最终我们得出一个比较满意的测试解决方案:
- 使用数据驱动模式,维护参数配置文件,一份代码,所有用例都可执行
- 测试用例通过参数组合生成,提供开关控制用例生成数量
- 指定场景测试 + 随机测试(从参数组合中抓取随机数量的组合)
测试流程图
实现的功能
- 代码与业务分离。不再需要写大量场景的自动化用例代码,仅需维护参数配置文件
- 测试用例根据配置文件中的参数随机生成测试场景,场景数量可控(配置文件提供指定数量)
- 2 层随机机制(查询条件随机组合,查询条件中具体条件中的值随机组合),保证了场景的覆盖度
- 2 种测试方式(随机场景测试,指定场景测试)
- 明确的测试结果。只关注点击查询后的页面结果,忽略自动化操作页面元素失败干扰测试结果(用例在页面操作失败依然往下执行)
用例代码
public class RandomParamatersQueryTest {
private WebDriver webDriver = null;
private SoftAssert softAssert = new SoftAssert();
WebManager webManager = null;
WebElementCheck webCheck = null;
@Test
public void randomParamatersQueryTest() {
String parameterXmlPath = ConfigReader.getInstance().getValue("parameterXmlPath");
String randomCaseXmlPath = ConfigReader.getInstance().getValue("randomCaseXmlPath");
String randomexcelPath = ConfigReader.getInstance().getValue("randomexcelPath");
// 使用Firefox浏览器
webDriver = WebDriverManager.GetDriver(BroswerType.FIREFOX);
webManager = new WebManager(webDriver, softAssert);
webCheck = new WebElementCheck(webDriver, softAssert);
PublicMethod common = new PublicMethod(webManager, webCheck);
SalesReport sReport = new SalesReport(webManager);
// 登录、跳转到指定报表页
common.login(ConfigReader.getInstance().getValue("loginURL"), ConfigReader.getInstance().getValue("loginEmail"),
ConfigReader.getInstance().getValue("loginPassword"));
sReport.navigateToSalesReport();
sReport.switchToFrame();
// 参数生成参数配置文件
ReportProvider.saveParamatersToCaseXML(parameterXmlPath, randomCaseXmlPath);
webManager.threadWait(3);
// 读取参数配置文件,做UI自动化测试
XmlReader xmlReader = XmlReader.getInstance(randomCaseXmlPath);
// 创建测试报告Excel
String reportName = "sales report";
ExcelOperate.getInstance().createExcel(reportName, randomexcelPath);
List<String> configIds = xmlReader.getAttributeValue("id");
for (String configId : configIds) {
try {
// 设置查询参数
sReport.setQueryParas(randomCaseXmlPath, configId);
sReport.clickSearch();
try {
// 判断错误提示框是否出现,如果出现,判定该用例执行失败
webManager.dynamicWaitAppearAndThrowException(WebElementType.XPATH,
"//div[@class='ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-resizable']",
2);
webManager.threadWait(1);
String message = webCheck.getProperty(WebElementType.XPATH, "//div[@id='alert_dialog']",
CheckPropertyType.INNERTEXT);
ExcelOperate.getInstance().excelWrite2003(randomexcelPath, ExcelUpdateMode.ADD, reportName,
configId, "fail", message);
} catch (Exception e) {
// 错误框未出现,判定用例执行成功
ExcelOperate.getInstance().excelWrite2003(randomexcelPath, ExcelUpdateMode.ADD, reportName,
configId, "success", "");
} finally {
webManager.refreshBroswer();
sReport.navigateToSalesReport();
sReport.switchToFrame();
}
} catch (Exception e) {
// 任何错误都不抛出,保证用例的循环执行
}
}
common.logout();
}
}
两种测试方式
随机测试与指定测试的区别在于用例配置文件一个是根据参数随机生成,一个是用户自己指定场景的参数
随机测试
参数文件
<?xml version="1.0" encoding="UTF-8"?>
<Testcases xmlns="testcase">
<parameters>
<grouptype>1,2,3</grouptype>
<market>504</market>
<ps>2877</ps>
<orggroupby>1,2,3,4,5</orggroupby>
<channel>287</channel>
<channelcountry></channelcountry>
<productline></productline>
<pm></pm>
<cm>2877</cm>
<salesdate>2017-02-01,2017-02-02</salesdate>
<country>US,UK,FR</country>
<curno>USD</curno>
<shippingperiod>1</shippingperiod>
<model>TT-BH07</model>
<sku>53-10007-001</sku>
</parameters>
</Testcases>
生成的随机测试参数配置文件每次都会改变,这样保证了用例的场景覆盖度(生成的 case 数量可控,在配置文件中设置即可)
<?xml version="1.0" encoding="UTF-8"?>
<Testcases xmlns="testcase">
<Testcase xmlns="" id="case1">
<country>US,UK,FR</country>
<channel>287</channel>
<curno>USD</curno>
<shippingperiod>1</shippingperiod>
<grouptype>1,2,3</grouptype>
<salesdatefrom>2017-02-01</salesdatefrom>
<salesdateto>2017-02-02</salesdateto>
<sku>53-10007-001</sku>
</Testcase>
<Testcase xmlns="" id="case2">
<cm>2877</cm>
<shippingperiod>1</shippingperiod>
</Testcase>
<Testcase xmlns="" id="case3">
<country>US,UK,FR</country>
<ps>2877</ps>
<cm>2877</cm>
<curno>USD</curno>
<market>504</market>
<orggroupby>1,2,3,4,5</orggroupby>
<salesdatefrom>2017-02-01</salesdatefrom>
<salesdateto>2017-02-02</salesdateto>
<sku>53-10007-001</sku>
</Testcase>
<Testcase xmlns="" id="case4">
<country>US,UK,FR</country>
<channel>287</channel>
<cm>2877</cm>
<market>504</market>
<grouptype>1,2,3</grouptype>
<orggroupby>1,2,3,4,5</orggroupby>
<salesdatefrom>2017-02-01</salesdatefrom>
<salesdateto>2017-02-02</salesdateto>
<sku>53-10007-001</sku>
</Testcase>
<Testcase xmlns="" id="case5">
<shippingperiod>1</shippingperiod>
<market>504</market>
<orggroupby>1,2,3,4,5</orggroupby>
<model>TT-BH07</model>
<sku>53-10007-001</sku>
</Testcase>
<Testcase xmlns="" id="case6">
<country>US,UK,FR</country>
<channel>287</channel>
<curno>USD</curno>
<shippingperiod>1</shippingperiod>
<model>TT-BH07</model>
</Testcase>
<Testcase xmlns="" id="case7">
<country>US,UK,FR</country>
<ps>2877</ps>
<channel>287</channel>
<curno>USD</curno>
<market>504</market>
<orggroupby>1,2,3,4,5</orggroupby>
<model>TT-BH07</model>
<sku>53-10007-001</sku>
</Testcase>
<Testcase xmlns="" id="case8">
<cm>2877</cm>
<curno>USD</curno>
<market>504</market>
<grouptype>1,2,3</grouptype>
<salesdatefrom>2017-02-01</salesdatefrom>
<salesdateto>2017-02-02</salesdateto>
<sku>53-10007-001</sku>
</Testcase>
<Testcase xmlns="" id="case9">
<country>US,UK,FR</country>
<ps>2877</ps>
<curno>USD</curno>
<shippingperiod>1</shippingperiod>
<market>504</market>
<grouptype>1,2,3</grouptype>
<model>TT-BH07</model>
<sku>53-10007-001</sku>
</Testcase>
<Testcase xmlns="" id="case10">
<ps>2877</ps>
<curno>USD</curno>
<market>504</market>
<model>TT-BH07</model>
</Testcase>
</Testcases>
指定测试
参数文件
<?xml version="1.0" encoding="UTF-8"?>
<Testcases xmlns="testcase">
<Testcase id="allparamaters">
<grouptype>1,2,3,4,5,6,7,8,9,53,54,55</grouptype>
<market>504</market>
<ps>3817,3782,2877,1620,3619,3661</ps>
<orglist>1,2,3,4,5</orglist>
<channel>12</channel>
<channelcountry></channelcountry>
<productline></productline>
<pm>3660,3619,1620,2887,3813</pm>
<cm>3661,2877,3782,1620,3817</cm>
<salesdatefrom>2017-02-01</salesdatefrom>
<salesdateto>2017-02-02</salesdateto>
<country>CA,CN,DE,ES,FR,HK,IT,JP,UK,US</country>
<curno></curno>
<shippingperiod></shippingperiod>
<model></model>
<sku></sku>
</Testcase>
<Testcase id="noneparamaters">
<grouptype></grouptype>
<market></market>
<ps></ps>
<orglist></orglist>
<channel></channel>
<channelcountry></channelcountry>
<productline></productline>
<pm></pm>
<cm></cm>
<salesdatefrom></salesdatefrom>
<salesdateto></salesdateto>
<country></country>
<curno></curno>
<shippingperiod></shippingperiod>
<model></model>
<sku></sku>
</Testcase>
</Testcases>
总结
通过指定测试场景与随机测试场景,达到报表黑盒自动化测试的目的。
作为一位过来人也是希望大家少走一些弯路
在这里我给大家分享一些自动化测试前进之路的必须品,希望能对你带来帮助。
(软件测试相关资料,自动化测试相关资料,技术问题答疑等等)
相信能使你更好的进步!
点击下方小卡片