2012-01-19 46 views
0

我正在測試Selenium中的下拉列表值。我在Java工作。在Selenium中測試下拉列表值 - 控制檯不打印不匹配

下拉列表中有40個值。我希望我的代碼能夠獲取DDL中的所有值,並將其與應該的值進行比較。

如果DDL中的值與我期望的硬編碼值不匹配,那麼我的代碼將會失敗。它會打印一條說明值不匹配的消息。但它不告訴我哪個值沒有匹配。

達到此目的的最佳方法是什麼?

我正在使用selenium.getSelectOptions來獲取DDL值。然後使用if語句來檢查值是否相同,如果不是則失敗。但它不告訴我哪個價值是不一樣的。

+1

可以ü分享您的代碼和輸出? – Amey

回答

0

沒有代碼就沒有代碼很難幫你。我會盡力勾勒出一點幫助我怎麼處理這個:

public void chooseProduct(String product){ 
    List<WebElement> Options = new ArrayList<WebElement>(); 
    productChoooser = driver.findElement(By.id("id_of_the_selectbox")); 
    productChoooser.click(); 
    Select select = new Select(productChoooser); 
    Options = select.getOptions(); 
    for (WebElement option:Options){ 
     if(option.getText().equals(product)){ 
      option.click(); 
     } 
    } 

上面的代碼從下拉一個項目。您可以輕鬆地修改它bymodyfying的如果聲明在此,說:

boolean foundIt = false; 
for (WebElement option:Options){ 
     if(option.getText().equals(product)){ 
      System.out.println("The product " + product + "was found in the drop down list"); 
foundIt = true; 
     } 

} 
if (!foundit){ 
    System.out.println("The product " + product + "was NOT found in the drop down list"); 
} 
} 

請跟我重新檢查了,我直接寫代碼的答案窗口