2016-08-17 53 views
1

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

目前我不知道如何顯式地通過在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(); 
    ...... 
} 

回答

0

你可以得到4個webelements通過使用@FindBy包含產品項目分爲4個不同的列表屬性。只需將這些元素的完整xpath傳遞給FindBy即可。 可以遍歷列表以獲得單個值測試或創建一個產品編號對象等等...

+0

感謝您的建議。我面臨的問題是我無法唯一識別即時生成的搜索結果產品項目。那麼我怎樣才能唯一標識搜索結果產品項目的子webelement?所以我正在尋找其他可以使用POF @FindBy註釋自動設置子元素值的其他東西。我在http://stackoverflow.com/questions/24251739/selenium-webdriver-page-factory-initialization-using-paths-relative-to-other-el?rq=1 – win

0

你會希望有一個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有一個構造函數,該構造函數接受一個表示該項目唯一標識的參數......我經常在項目的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(); 

注意,所以請原諒語法錯誤......應該有足夠的在這裏,你得到的漂移。

+0

找到答案感謝您的建議。我面臨的問題是我無法唯一識別即時生成的搜索結果產品項目。那麼我怎樣才能唯一標識搜索結果產品項目的子webelement?所以我正在尋找其他可以使用POF @FindBy註釋自動設置子元素值的其他東西。我在http://stackoverflow.com/questions/24251739/selenium-webdriver-page-factory-initialization-using-paths-relative-to-other-el?rq=1找到答案 – win

相關問題