我正在使用 POM 框架,在其中为我的应用程序页面创建页面类。假设我的应用程序中有 2 页 1. 事件 2. 时间线。所以我创建了 2 个页面类
事件页面.java
public class RCON_D_EventPage
{
@FindBy(xpath="//input[@placeholder='Search for entered records']")
public WebElement eventSearchBox;
@FindBy(xpath="//button[@class='btn rc-gray-bg rc-dashboard-contact-btn ng-scope']")
public WebElement eventSearchButton;
@FindBy(xpath="//p[@ class='rc-found-record no-padding ng-binding ng-scope']")
public WebElement eventSearchResult;
@FindBy(xpath="//div/span[@class='ng-scope']")
public WebElement searchResultNotFound;
@FindBy(xpath="//li/button[@ng-click='goToFirstPage()']")
public WebElement nextPageButton;
@FindBy(xpath="//button[@ng-click='clearFilters()'][1]")
public WebElement clearFilterButton;
WebDriver driver;
public RCON_D_EventPage(WebDriver driver)
{
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10), this);
this.driver=driver;
}
public void enterTextInEventSearchBox(String text)
{
eventSearchBox.clear();
eventSearchBox.sendKeys(text);
}
public void clickEventSearchButton()
{
eventSearchButton.click();
}
public String getEventSearchResult()
{
return eventSearchResult.getText();
}
public String getNoRecordFoundMessage()
{
return searchResultNotFound.getText();
}
public void clickNextPageButton()
{
nextPageButton.click();
}
public void clickClearFilterButton()
{
clearFilterButton.click();
}
}
时间线页面.java
public class RCON_D_TimelinePage
{
@FindBy(xpath="//input[@placeholder='Search for entered records']")
public WebElement timelineSearchBox;
@FindBy(xpath="//button[@class='btn rc-gray-bg rc-dashboard-contact-btn ng-scope']")
public WebElement searchButton;
@FindBy(xpath="//p[@class='rc-found-record no-padding ng-binding ng-scope']")
public WebElement searchResult;
@FindBy(xpath="//div[@class='row ng-scope']")
public List<WebElement> totalFoundRecords;
@FindBy(xpath="//span[text()='No data found']")
public WebElement noResultMessage;
@FindBy(xpath="//button[@ng-click='clearFilters()'][1]")
public WebElement clearFilterButton;
public RCON_D_TimelinePage(WebDriver driver)
{
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10), this);
this.driver=driver;
}
public void enterTextInSearchBox(String text)
{
timelineSearchBox.sendKeys(text);
}
public void clickSearchButton()
{
searchButton.click();
}
public String getSearchResult()
{
return searchResult.getText();
}
public int getFoundRecordCount()
{
return totalFoundRecords.size();
}
public String getNoResultFoundMessage()
{
return noResultMessage.getText();
}
public void clickClearFilterButton()
{
clearFilterButton.click();
}
}
因此,在这两个页面中,都有一些常见的 WebElement,例如//input[@placeholder='Search for entered records']
or//button[@class='btn rc-gray-bg rc-dashboard-contact-btn ng-scope']
等等。那么有什么方法可以管理页面对象模型中的这种冗余吗?