简体   繁体   English

如何将页面对象工厂框架用于webelement的子元素

[英]how to use page object factory framework for child elments of a webelement

I have a search result page which contains many product items which are produced as a search result. 我有一个搜索结果页面,其中包含许多作为搜索结果生成的产品项目。 I can get the list of the product items. 我可以得到产品清单。 But there are product details like price, discount, shipping price which are inside these webelements as child elements. 但是有价格,折扣,运费等产品细节作为子元素在这些元素中。 How can I use page object factory model to get the values of these child elements automatically by using the @FindBy annotation. 如何使用页面对象工厂模型通过@FindBy注释自动获取这些子元素的值。

At the moment I do not know how so I am getting them explicitly by providing the context of the parent WebElement in the findElement(By) function explicitly rather than the automatic mechanism of POF. 目前我不知道如何通过明确地在findElement(By)函数中提供父WebElement的上下文而不是POF的自动机制来明确地获取它们。 Question is how to make the context as a WebElement rather than a driver object for a PageObject class. 问题是如何将上下文作为WebElement而不是PageObject类的驱动程序对象。 I have not been able to find any understandable web article explaining how to do it. 我无法找到任何可理解的网络文章来解释如何做到这一点。 Could someone explain a SIMPLE way to do it hopefully with an example.... I would be really grateful. 有人可以通过一个例子来解释一个简单的方法吗?我真的很感激。 Below is the code to explain the Test Class and the class to represent the product. 下面是解释Test Class和代表产品的类的代码。

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();
     ......
}

You can get the 4 webelements which contains the attribute of the product item into 4 different lists by using @FindBy. 您可以使用@FindBy将包含产品项属性的4个webelements分成4个不同的列表。 Just pass the complete xpath of these elements to FindBy. 只需将这些元素的完整xpath传递给FindBy即可。 You can iterate the lists to get individual values to test or create a Product Item object etc etc... 您可以迭代列表以获取单个值来测试或创建Product Item对象等等...

You would want to have a "factory" method in TestProducts to generate ProductItem instances: 您可能希望在TestProducts中使用“factory”方法来生成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);

Of course, this implies that ProductItem has a constructor that takes an argument that represents a unique id for that item...I often see an attribute on the div wrapper around items. 当然,这意味着ProductItem有一个构造函数,它接受一个表示该项的唯一id的参数......我经常在项目周围的div包装器上看到一个属性。 The constructor then sets the id, eg itemWrapperId, for use by other methods. 然后构造函数设置id,例如itemWrapperId,供其他方法使用。

The ProductItem methods then use findElements to get item information based from that root. 然后,ProductItem方法使用findElements从该根获取项目信息。 Eg for subPrice in ProductItem 例如,对于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();

Note that I just typed this into here, so please excuse syntax errors...there should be enough here that you get the drift. 请注意,我只是在这里输入了这个,所以请原谅语法错误......这里应该有足够的东西让你得到漂移。

This is the real solution that worked for me and what I was looking for. 这是真正的解决方案,对我和我一直在寻找。 Selenium Webdriver: Page factory initialization using paths relative to other elements? Selenium Webdriver:使用相对于其他元素的路径进行页面工厂初始化?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM