在每個測試用例的末尾,我正在通過調用以下代碼來檢查是否存在錯誤。我遇到的問題是,即使不存在錯誤,代碼也會拋出一個NoSuchElementException
,並且會導致測試用例失敗。如果出現錯誤,則測試用例會通過。Selenium - NoSuchElementException錯誤檢查
如何修改我的代碼,以便如果不存在錯誤,測試將通過,如果出現錯誤,則測試用例將失敗。
public static void chk_ErrorIsNotEnabled()
{
try
{
element = driver.findElement(By.id("ctl00_Content_ulErrorList"));
if(element.getText().equals(""))
{
Log.info("Warning error is not dispayed."); // The test should pass if element is not found
}
else
{
Log.error("Warning error is dispayed when it shouldnt be.");
} //The test should fail if element is found
}
catch (NoSuchElementException e){}
}
感謝您的快速回復。奇蹟般有效 – CoffeeTime 2015-02-05 16:14:10