有如下的基于web的组合搜索功能,需要创建自动化测试用例:

我们需要在所有的输入框输入不同的值,进行非常多次的搜索,然后验证
二、实现
创建一个类 ClientBasicSearchFields 去代表所有的输入框,创建一个方法 doClientBasicSearch(ClientBasicSearchFields csf)去执行搜索的操作,通过ClientBasicSearchFields将参数传入
public void doClientBasicSearch(ClientBasicSearchFields csf){ type("element identifier1", csf.getClientName()); type("element identifier2", csf.getClientShortName()); type("element identifier3", csf.getAddress()); type("element identifier4", csf.getCity()); select("element identifier5", csf.getState()); type("element identifier6", csf.getZip()); type("element identifier7", csf.getFirstClientSerRep()); type("element identifier8", csf.getSedClientSerRep()); select("element identifier9", csf.getBillingFre()); select("element identifier10", csf.getClinicalFre()); clickAndPause("submit"); }
接下去是重点,使用Builder模式的java类ClientBasicSearchFields
public class ClientBasicSearchFields { private final String clientName; private final String clientShortName; private final String address; private final String city; private final String state; private final String zip; private final String firstClientSerRep; private final String sedClientSerRep; private final String billingFre; private final String clinicalFre; private ClientBasicSearchFields(Builder builder){ clientName = builder.clientName; clientShortName = builder.clientShortName; address = builder.address; city = builder.city; state = builder.state; zip = builder.zip; firstClientSerRep = builder.firstClientSerRep; sedClientSerRep = builder.sedClientSerRep; billingFre = builder.billingFre; clinicalFre = builder.clinicalFre; } public String getClientName() { return this.clientName; } public String getClientShortName(){ return this.clientShortName; } public String getAddress(){ return this.address; } public String getState(){ return this.state; } public String getCity(){ return this.city; } public String getZip(){ return this.zip; } public String getFirstClientSerRep(){ return this.firstClientSerRep; } public String getSedClientSerRep(){ return this.sedClientSerRep; } public String getBillingFre(){ return this.billingFre; } public String getClinicalFre() { return this.clinicalFre; } public static class Builder { private String clientName = ""; private String clientShortName = ""; private String address = ""; private String city = ""; private String state = ""; private String zip = ""; private String firstClientSerRep = ""; private String sedClientSerRep = ""; private String billingFre = ""; private String clinicalFre = ""; public Builder(){ } public Builder clientName(String value){ clientName = value; return this; } public Builder clientShortName(String value){ clientShortName = value; return this; } public Builder address(String value){ address = value; return this; } public Builder city(String value){ city = value; return this; } public Builder state(String value){ state = value; return this; } public Builder zip(String value){ zip = value; return this; } public Builder firstClientSerRep(String value){ firstClientSerRep = value; return this; } public Builder sedClientSerRep(String value){ sedClientSerRep = value; return this; } public Builder billingFre(String value){ billingFre = value; return this; } public Builder clinicalFre(String value){ clinicalFre = value; return this; } public ClientBasicSearchFields build(){ return new ClientBasicSearchFields(this); } } }
最后,在测试方法中这样使用
public void testSearchByClientName() { // 进入我们需要测试的页面 logInToTheSystem(); navigateToSearchClientPage(); // 根据设置的参数进行搜索 ClientBasicSearchFields csf = new ClientBasicSearchFields.Builder().clientName( “testName”).build(); doClientBasicSearch(csf); // 验证结果 verifySearchResultsPage(); }
如果需要同时输入多个值进行搜索,可以采用如下方式:
ClientBasicSearchFields csf = new ClientBasicSearchFields.Builder().clientName( "某某某").city("杭州").zip("310000") .build();
在每一个测试方法中,我们可以创建一个csf,然后通过这种方法初始化测试数据,我们每次可以设置的参数个数都是可以选的,可以很简单得去创建更多我们需要的测试用例。
三、总结
如果用普通的javabean, 也可以实现同样的功能。用builder模式,看上去更加阅读,而且比JavaBean更加安全(Effective Java第二版)