2013-08-27 32 views

回答

1

方法1: 首先,寫出下面的方法:

private boolean isTextPresent(String text){ 
     try{ 
      boolean b = driver.getPageSource().contains(text); 
      return b; 
     } 
     catch(Exception e){ 
      return false; 
     } 
    } 

現在做斷言預期的消息是否是在頁面上存在或不通過調用上述方法:

assertTrue(isTextPresent("Value is required and can't be empty"), "Msg is absent/wrong/misspelled"); 

路2: 另一種方法如下:

import java.util.regex.Pattern; 
import java.util.concurrent.TimeUnit; 
import org.junit.*; 
import static org.junit.Assert.*; 
import static org.hamcrest.CoreMatchers.*; 
import org.openqa.selenium.*; 

private StringBuffer verificationErrors = new StringBuffer(); 

try { 
     assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Value is required and can't be empty[\\s\\S]*$")); 
    } catch (Error e) { 
     verificationErrors.append(e.toString()); 
    } 
    } 
+0

第二個是更可取的 –

+0

兩個選項都工作正常。非常感謝! –

+0

但最後我用第二個。 –

1

可以使用assert檢查必填字段警報出現與否,用的try-catch

try { 
    assertEquals("Value is required and can't be empty", driver.findElement(By.xpath("XPATH_OF_LABLE")).getText()); 
} catch (Error e) { 
//code for the case when the texts are not same 
    verificationErrors.append(e.toString()); 
} 

的例子中,使用XPath,但你可以使用任何其他定位方法這一點。

+0

你的一個也workin g.非常感謝! –

相關問題