java如何消除页面对象模型中的重复WebElement
我正在使用POM框架,我已经为我的应用程序页面创建了页面类。假设在我的应用程序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();
}
}
TimeLinePage。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();
}
}
因此,在这两个页面中都有一些常见的web元素,例如//input[@placeholder='Search for entered records']
或//button[@class='btn rc-gray-bg rc-dashboard-contact-btn ng-scope']
等等。所以,有没有办法在页面对象模型中管理这种冗余
# 1 楼答案
在这种情况下,您可以使用组合(is-A,has-A关系)
有关系
您可以创建一个用于搜索的页面类,并复制该类中的所有方法。而所有其他包含此元素的Page类,您只需创建此页面的对象
下面的示例显示了
has-A
关系是一种关系
您也可以使用
is-A
关系实现同样的效果。我的意思是,您可以使用SearchPage
类扩展每个需要搜索功能的类就我个人而言,我建议使用has-A关系,因为它在编程方面比下面提到的is-A更有意义
# 2 楼答案
听起来好像你有一个共同的标题,或者可能只是两个页面上的搜索框。在这种情况下,只需为标题/搜索框区域创建一个页面对象。它将包含这些元素
然后可以从两个现有页面对象中删除。无论您在哪个页面上,只要需要,都可以实例化header page对象
不要认为页面对象代表整个页面。把它们想象成一个小部件对象,小部件可以是一个完整的页面,也可以只是页面的一部分,其功能可以在不同的页面上重复
请在此处阅读更多信息:
https://martinfowler.com/bliki/PageObject.html