2012-07-19 92 views
0

我使用Selenium Web Driver和JUnit Eclipse。我有這樣的類:如何在Selenium WebDriver的頁面對象(PageFactory)中使用數組?

import org.openqa.selenium.By; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.CacheLookup; 
import org.openqa.selenium.support.FindBy; 

//Path 
public class Path { 
int i; 
String[]path_name; 
@FindBy(linkText=path_name[i]) 
@CacheLookup 
private WebElement path; 
public void path(int number,String[]path_name){ 
for (i=1; i<number; i++){ 
    path.findElement(By.linkText(path_name[i])).click(); 
} 
} 
} 

它打開一個接一個定義的鏈接數量。這個課程與我的主要考試分開。要調用類路徑我使用頁面對象(頁面工廠)。 這是我的主要測試代碼:

Path path= PageFactory.initElements(driver, Path.class); 
path.path (2, new String[]{ "First", "New text"}); 

,但我得到的錯誤The value for annotation attribute FindBy.linkText must be a constant expression上線@FindBy(linkText=path_name[i])。我用什麼方式將我的數組聲明爲頁面對象?或者也許有另一種方法從類Path調用方法路徑? 如果我刪除這部分代碼,則會導致錯誤消失: @FindBy(linkText = path_name [i]) @CacheLookup 但是在這種情況下,JUnit找不到數組的元素。

+0

可能重複:http://stackoverflow.com/questions/8008138/selenium-webdriver-and-pagefactory-initialize- listwebelement元素 – 2012-07-22 10:43:02

回答

0

註釋@FindBys是你在找什麼。它返回一個列表。

如果你想要一個數組,你可以從列表轉換或使用this answear ...

相關問題