【问题标题】:How to handle dynamic html tables in page object model using page fatory in selenium?如何使用 selenium 中的 pagefactory 处理页面对象模型中的动态 html 表?
【发布时间】:2018-05-22 16:33:12
【问题描述】:

我有一个 HTML 页面并创建了该页面的页面对象。此页面包含一个动态 html 表。我无法将它用作页面对象,因为它是动态创建的。我想在我的测试用例中使用页面的任何行和列。

页面对象类:

public final class AgentsPage 
{
@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//input[@id='gwt-debug-Phone_Number']")
public WebElement phoneNumber;

public String getTextOfPhoneNumber()
{
    return phoneNumber.getAttribute("value");
}

public void setTextForPhoneNumber(String value)
{
    phoneNumber.clear();
    phoneNumber.sendKeys(value);
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//input[@id='gwt-debug-Name']")
public WebElement name;

public String getTextOfName()
{
    return name.getAttribute("value");
}

public void setTextForName(String value)
{
    name.clear();
    name.sendKeys(value);
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//input[@id='gwt-debug-Reseller_ID']")
public WebElement resellerId;

public String getTextOfResellerId()
{
    return resellerId.getAttribute("value");
}

public void setTextForResellerId(String value)
{
    resellerId.clear();
    resellerId.sendKeys(value);
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//input[@id='gwt-debug-Terminal_Serial']")
public WebElement terminalSerial;

public String getTextOfTermialSerial()
{
    return terminalSerial.getAttribute("value");
}

public void setTextForTerminalSerial(String value)
{
    terminalSerial.clear();
    terminalSerial.sendKeys(value);
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//select[@id='gwt-debug-Rows_Per_Page']")
public WebElement rowsPerPage;

public String getSelectedValueOfRowsPerPage()
{
    Select select=new Select(rowsPerPage);
    return select.getFirstSelectedOption().getText();
}

public void setValueForRowsPerPage(String visibleText)
{
    if(!(visibleText.equals("")||visibleText==null))
    {
        Select select=new Select(rowsPerPage);
        select.selectByVisibleText(visibleText);
    }
}

public void setValueForRowsPerPage(int index) throws Exception
{
    Select select=new Select(resellerId);
    select.selectByIndex(index);
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//button[@id='gwt-debug-submit_button']")
public WebElement submit;

public String getTextOfSubmit()
{
    return submit.getText();
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//button[@id='gwt-debug-clear_button']")
public WebElement reset;

public String getTextOfReset()
{
    return reset.getText();
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//button[@id='gwt-debug-nextPage']")
public WebElement next;

public String getTextOfNext()
{
    return next.getText();
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//button[@id='gwt-debug-prevPage']")
public WebElement previous;

public String getTextOfPrevious()
{
    return previous.getText();
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//button[@id='gwt-debug-lastPage']")
public WebElement last;

public String getTextOfLast()
{
    return last.getText();
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug-com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//button[@id='gwt-debug-firstPage']")
public WebElement first;

public String getTextOfFirst()
{
    return first.getText();
}

@FindBy(how=How.XPATH, using="//table[@id='gwt-debug- com.seamless.ers.client.agentPortal.client.view.screens.SubAgentsReportScreen']//div[contains(text(),'Page:') and contains(text(),'/')]")
public WebElement pageCount;

public String getTextOfPageCount()
{
    return pageCount.getText();
}
//Need page objects for the dynamic html table
}

![页面图片][1]:http://i.stack.imgur.com/o94F0.png

我不想在我的测试用例中使用任何 xpath 或任何循环来遍历表的行。我只想像使用任何其他静态页面对象元素(如 AgentsPage.submit 按钮)一样使用它。如何实现?

【问题讨论】:

  • 版主注意:请不要破坏您的帖子。一旦您发布问题,它们就属于该网站及其用户。即使它不再对您有用,它也可能对将来的某人有所帮助。回答者也会努力写下他们的答案,如果您从帖子中删除了内容,这将不再有用。另外,请注意,通过在 Stack Exchange 网络上发布,您已授予 SE 分发该内容的不可撤销的权利(根据 CC BY-SA 3.0 许可)。根据 SE 政策,任何破坏行为都将被撤销。
  • 我理解这个案例。但是这些代码被错误地从我的组织中获取,我被告知将其删除,因为这纯粹是违规行为。然而,这些代码并没有那么有用,因为问题描述了问题。谢谢你告诉我。

标签: selenium-webdriver pageobjects


【解决方案1】:

如果没有 xPath,您甚至无法找到所需的表格元素(您可以使用 CssSelectors,但它是相同的)。

做对了我建议你创建一个包含表格行的网络元素列表(<tr> 标签)。此列表应由@FindBy 属性初始化。

那么我认为为您的数据创建“get”方法是个好主意。

例如:

public String getName(int row, int cell)
{
    return theTable[row].findElement(By.xpath(".//td[" + cell + "]")).getText();
}

【讨论】:

  • 感谢您的帮助。真的很感激。
【解决方案2】:

将行号、电话号码、姓名等网络元素存储在自己的列表中。您可以使用流轻松将数据作为字符串获取。

@FindBy(xpath="xpath to the row number")
private List<WebElement> rowNum;

@FindBy(xpath="xpath to the phone number")
private List<WebElement> phoneNum;

其他两个也是如此。数据将按照页面上的顺序排列。您可以使用列表中的索引轻松访问数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 2020-05-17
    相关资源
    最近更新 更多