我正在使用 Selenium 来自动化一些围绕使用多种条件组合进行搜索的测试用例。在我的搜索页面的 PageObject 中,我创建了这样的 Builder 类
public class AdvancedSearchPage extends PageObject {
public static class Query {
private WebDriver driver;
private String commonName;
private String contactEmail;
private int certificateType;
private int status;
public Query(WebDriver driver) {
this.driver = driver;
}
public Query setCommonName(String value) {
this.commonName = value;
return this;
}
public Query setContactEmail(String value) {
this.contactEmail = value;
return this;
}
public Query setCertificateType(int value) {
this.certificateType = value;
return this;
}
public Query setStatus(int value) {
this.status = value;
return this;
}
public AdvancedSearchPage build() {
return new AdvancedSearchPage(driver,this);
}
}
private AdvancedSearchPage(WebDriver driver, Query q) {
super(driver);
// Navigates to AdvancedSearchPage url and authenticates if necessary
this.init(URL);
this.setCommonName(q.commonName);
this.setContactEmail(q.contactEmail);
this.setCertificateType(q.certificateType);
this.setStatus(q.status);
}
}
在我的私有(private)构造函数中,我获取这些值并使用 AdvancedSearchPage
对象的“setters”,它只是设置相关 WebElement
的值。
当我仅使用通用名称构建查询时,会设置其他字段。如何修改私有(private)构造函数以仅设置构建查询时定义的 WebElement
的值?
最佳答案
您的意思是仅设置不为空的 AdvancedSearchPage 值吗?如果是的话你可以试试这个。
private AdvancedSearchPage(WebDriver driver, Query q) {
super(driver);
// Navigates to AdvancedSearchPage url and authenticates if necessary
this.init(URL);
if(q.commonName != null) this.setCommonName(q.commonName);
if(q.contactEmail != null) this.setContactEmail(q.contactEmail);
if(q.certificateType != null) this.setCertificateType(q.certificateType);
if(q.status != null) this.setStatus(q.status);
}
关于java - 使用构建器模式和 Selenium 通过不同的条件组合执行不同的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43737342/