2013-08-27 29 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.非常感谢! –

相关问题