2016-07-14 37 views
0

我有點擔心我面臨的小問題。我有一個測試案例,我需要從下拉列表中選擇任何值。選擇整個網頁後重新加載,我有2個選項。如果出現警告消息,我需要再次點擊下拉菜單並選擇另一個值。如果警告消息沒有出現,只需點擊「繼續」按鈕。我滴內的所有數值都下降,只有在不同的結局相同的ID:如何使用Selenium初始化Java中的方法內的另一個WebElement

uxMiniFinderVoyageSelect_chzn_o_1 
uxMiniFinderVoyageSelect_chzn_o_2 
uxMiniFinderVoyageSelect_chzn_o_3 
uxMiniFinderVoyageSelect_chzn_o_4 

直到30.我做了我叫Random類,我從1設置變量 - 30

Random random = new Random(); 
int x = random.nextInt(30) + 1; 

現在我找我的元素這樣

WebElement valueFromDropDown = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x)); 

但是,如果出現警告信息,我需要點擊另一個值,我的代碼遍地拿起相同的值。問題是如何正確使用較少的代碼點擊下拉另一個元素?滿級是這樣的

public class SomeClassName{ 

     Random random = new Random(); 
     int x = random.nextInt(30) + 1; 
     @FindBy(xpath = "someXpathExpression") private WebElement dropDown; 
     @FindBy(xpath = "someXpathExpression") private WebElement warningMessage; 
     @FindBy(xpath = "someXpathExpression") private WebElement continueButton; 

public void fillForm() throws Exception{ 
    WebElement valueFromDropDown = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x)); 
    dropDown.click(); 
    valueFromDropDown.click(); 
    if(user will see that warningMessage suddenly apppears){ 
    dropDown.click(); 
    valueFromDropDown.click(); -> this is where I want to click on another value 
    }else{ 
    contunieButton.click(); 

回答

1

介紹的方法返回下拉webelement隨機像下面

public WebElement getDropDownValueRandomly() { 
    Random random = new Random(); 
    int x = random.nextInt(30) + 1; 
    WebElement valueFromDropDown = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x)); 

    return valueFromDropDown; 

} 

你的類將是這樣的:

public class SomeClassName{ 

    @FindBy(xpath = "someXpathExpression") private WebElement dropDown; 
    @FindBy(xpath = "someXpathExpression") private WebElement warningMessage; 
    @FindBy(xpath = "someXpathExpression") private WebElement continueButton; 

public void fillForm() throws Exception{ 
    WebElement valueFromDropDown = getDropDownValueRandomly(); 
    dropDown.click(); 
    valueFromDropDown.click(); 
    if(user will see that warningMessage suddenly apppears){ 
    dropDown.click(); 
    getDropDownValueRandomly().click(); -> this is where I want to click on another value 
    }else{ 
    contunieButton.click(); 
    } 
} 

public WebElement getDropDownValueRandomly() { 
    Random random = new Random(); 
    int x = random.nextInt(30) + 1; 
    WebElement valueFromDropDown = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x)); 

    return valueFromDropDown; 

} 

}

希望這有助於

+0

@Rama getDropDownValueRandomly方法名稱不能單擊。 – GordonFreaman

相關問題