簡體   English   中英

如何將頁面對象工廠框架用於webelement的子元素

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

我有一個搜索結果頁面,其中包含許多作為搜索結果生成的產品項目。 我可以得到產品清單。 但是有價格,折扣,運費等產品細節作為子元素在這些元素中。 如何使用頁面對象工廠模型通過@FindBy注釋自動獲取這些子元素的值。

目前我不知道如何通過明確地在findElement(By)函數中提供父WebElement的上下文而不是POF的自動機制來明確地獲取它們。 問題是如何將上下文作為WebElement而不是PageObject類的驅動程序對象。 我無法找到任何可理解的網絡文章來解釋如何做到這一點。 有人可以通過一個例子來解釋一個簡單的方法嗎?我真的很感激。 下面是解釋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();
     ......
}

您可以使用@FindBy將包含產品項屬性的4個webelements分成4個不同的列表。 只需將這些元素的完整xpath傳遞給FindBy即可。 您可以迭代列表以獲取單個值來測試或創建Product Item對象等等...

您可能希望在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);

當然,這意味着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();

請注意,我只是在這里輸入了這個,所以請原諒語法錯誤......這里應該有足夠的東西讓你得到漂移。

這是真正的解決方案,對我和我一直在尋找。 Selenium Webdriver:使用相對於其他元素的路徑進行頁面工廠初始化?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM