2016-10-14 22 views
1

我在我的項目中使用了硒頁面對象,並且還使用WebDriverWait等待添加元素。在WebDriverWait中使用Selenium頁面對象模式混合的問題

@FindBy(how = How.ID, using = "username") 
private WebElement username; 

@FindBy(how = How.ID, using = "password") 
private WebElement password; 

public void login(String username, String password) { 
    WebDriverWait waiter = new new WebDriverWait(driver, 5, 200); 
    waiter.until(ExpectedConditions.presenceOfElementLocated(
     By.id("username"))); 
    this.username.sendKeys(username) 
} 

兩個問題:

  1. 因爲我們只需要:

    waiter.until(ExpectedConditions.presenceOfElementLocated( By.id("username"))).sendkey(username);

,而不是頁面對象的用戶名來返回你想要的元素,是頁面對象模式無用?

  1. 如果頁面對象模式是必須的,我該如何處理字符串「username」?我需要一個新的類保持常量,如:

    public final static String USERNAME = "username";

,所以我們可以把它叫做我的網頁上?

+0

希望下面的鏈接回答你的問題 - http://stackoverflow.com/questions/18843581/wait-for-elment-webdriver-pageobject-pattern –

回答

0

使用方法,如

waiter.until(ExpectedConditions.visibilityOf(username)); 
//org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf(WebElement element) 

,而不是

waiter.until(ExpectedConditions.presenceOfElementLocated(By.id("username"))); 
//org.openqa.selenium.support.ui.ExpectedConditions.presenceOfAllElementsLocatedBy(By locator) 
1

「......是網頁對象模式沒用?」

很難!例如,您的登錄方法尚未在密碼字段中輸入值。因此,如果登錄時沒有使用LoginPage.login()方法,那麼您最少需要在每個測試中登錄兩行較長的代碼。

如果可以識別LoginPage.login()方法,則可以添加其他值像登錄頁面上預期的錯誤,當它可以拋出您的測試可以響應的自定義異常。我敢打賭,該登錄頁面上還有其他一些可能需要與之交互的內容,所以需要添加到LoginPage類的其他方法。

我需要一個新的類保持常量

我一般喜歡保持字符串,他們將要使用的類中的定位器。所以,我將在LoginPage中爲USERNAME_ID設置一個私有變量,然後在其他地方使用它。

相關問題