【问题标题】:how to use page object factory framework for child elments of a webelement如何为 webelement 的子元素使用页面对象工厂框架
【发布时间】:2016-08-17 03:15:31
【问题描述】:

我有一个搜索结果页面,其中包含许多作为搜索结果生成的产品项目。我可以得到产品项目的列表。 但是在这些 web 元素中作为子元素包含价格、折扣、运费等产品详细信息。如何使用页面对象工厂模型通过 @FindBy 注释自动获取这些子元素的值。

目前我不知道如何通过在 findElement(By) 函数中显式提供父 WebElement 的上下文而不是 POF 的自动机制来显式获取它们。问题是如何将上下文作为 WebElement 而不是 PageObject 类的驱动程序对象。我找不到任何可以理解的网络文章来解释如何做到这一点。有人可以用一个例子来解释一个简单的方法来做到这一点......我真的很感激。 下面是解释测试类和代表产品的类的代码。

public class TestProducts {

.....
 //I can get the list of products from a 
 List<WebElement> we_prod_box_list = driver.findElements(By.xpath("//div[@id='content']/descendant::div[starts-with(@class,'product_box_container')]"));
.....
}

public class ProductItem {

private WebDriver driver = null;
private Actions action = null;
private WebElement we_prod_box = null;

public String we_prod_box_title = "";
public WebElement we_pt = null;
public String slideImgTitle = "";
public String oldPrice = "";
public String oldPriceTitle = "";
public String subPrice = "";
public WebElement we_purch_disc = null;
private WebDriverWait wait = null;
public String shippingText = "";
public String shippingCost = "";
public String discPrice = "";
    .....

    we_pt = we_prod_box.findElement(By.xpath(".//div[contains(@class, 'subcategor_price_tag')]"));
slideImgTitle = we_pt.findElement(By.xpath(".//div[contains(@class, 'subcategor_slideimgtitle')]")).getText();
oldPrice = we_prod_box.findElement(By.xpath(".//div[contains(@class, 'oldprice')]")).getText();
oldPriceTitle = we_prod_box.findElement(By.xpath(".//div[contains(@class, 'oldprice')]")).getAttribute("title");
subPrice = we_prod_box.findElement(By.xpath(".//span[contains(@class, 'sub-price')]")).getText();
     ......
}

【问题讨论】:

    标签: java selenium selenium-webdriver webdriver pageobjects


    【解决方案1】:

    您可以使用@FindBy 将包含产品属性的 4 个 web 元素放入 4 个不同的列表中。只需将这些元素的完整 xpath 传递给 FindBy。 您可以迭代列表以获取单个值以测试或创建产品项目对象等...

    【讨论】:

    • 感谢您的建议。我面临的问题是我无法唯一地识别动态生成的搜索结果产品项目。那么如何才能唯一地标识搜索结果产品项的子 Web 元素呢?所以我一直在寻找其他东西,它也可以使用 POF @FindBy 注释自动设置子元素的值。我在stackoverflow.com/questions/24251739/… 找到了答案
    【解决方案2】:

    您可能希望在 TestProducts 中有一个“工厂”方法来生成 ProductItem 实例:

    TestProducts.getProductItems() {
        List<ProductItem> items = new ArrayList<ProductItem>();
        List<WebElement> we_prod_box_list = driver.findElements(By.xpath("//div[@id='content']/descendant::div[starts-with(@class,'product_box_container')]"));
        for (WebElement item : we_prod_box_list) {
            ProductItem newItem = new ProductItem(driver, item.getUniqueIdOfItemContainer);
            items.add(newItem);
    

    当然,这意味着 ProductItem 有一个构造函数,该构造函数接受一个参数,该参数表示该项目的唯一 id...我经常在项目周围的 div 包装器上看到一个属性。然后构造函数设置 id,例如itemWrapperId,供其他方法使用。

    ProductItem 方法然后使用 findElements 从该根获取项目信息。例如。 ProductItem 中的 subPrice

    private itemWrapperLocator = "//div[@id='content']/descendant::div[@class,'product_box_container" + itemWrapperId + "')]";
    
    public String subPriceLocator = ".//span[contains(@class, 'sub-price')]";
    public getSubPrice() {
        this.driver.findElement(By.xpath(this.itemWrapperLocator)).findElement(By.xpath(subPriceLocator)).getText();
    

    请注意,我只是在这里输入了这个,所以请原谅语法错误...这里应该有足够的内容让您理解。

    【讨论】:

    • 感谢您的建议。我面临的问题是我无法唯一地识别动态生成的搜索结果产品项目。那么如何才能唯一地标识搜索结果产品项的子 Web 元素呢?所以我一直在寻找其他东西,它也可以使用 POF @FindBy 注释自动设置子元素的值。我在stackoverflow.com/questions/24251739/… 找到了答案
    【解决方案3】:

    这是对我有用的真正解决方案,也是我一直在寻找的。 Selenium Webdriver: Page factory initialization using paths relative to other elements?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      相关资源
      最近更新 更多