2016-03-15 47 views
-3

我想聲明出現在文本字段中的值。文本字段的ID是blNo。我無法使用正確的斷言。尋找一些建議。要聲明文本字段

+0

你能夠提取價值?你的問題是不完整的......你有你想要比較價值的先決條件數據 –

回答

0

您可以使用assertTrue

String text = driver.findElement(By.id("blNo")).getText(); 
assertTrue("The text doesn't contains the value", text.contains("value")); 

的消息將出現在的情況下assert失敗。

0

我使用Testng和Selenium一起使用。我還有一個問題實現了Selenium的斷言,所以這就是我創建的。歡迎提供建設性的反饋。

針對WebElement對象通過getText()檢索文本。

WebElement object = driver.findElement(By.id("blNo")); 
String text = object.getText(); 

然後發送WebElement對象像下面的AreEqual方法的方法。

public class Assert 
{ 

    public static void AreEqual(String comparethis, String tothis, String falseMsg) 
    { 

     try 
     { 
      Log.Info("Compare this object: " + comparethis); 
      Log.Info("  to this object: " + tothis); 
      if(comparethis.equals(tothis)) 
      { 
       Log.Info(comparethis + " is equal to " + tothis); 
      } 
      else 
      { 
       Log.Info(comparethis + " is not equal to " + tothis); 
       Fail(); 
      } 
     } 

     catch(Exception e) 
     { 
      Log.Error("[EXCEPTION CAUGHT] : Assert.AreEqual() | " + falseMsg + " | Exception: " + e); 
      throw(e); 
     } 

    }// AreEqual method 

}// Assert Class 

fail方法

public static void Fail() 
{ 
    org.testng.Assert.fail("[TEST FAILED]"); 
    Log.Error("[TEST FAILED] : Assert.Fail()"); 
} 

...

0

您可以使用下面的代碼

Assert.assertEquals("Expected Text",driver.findElement(By.id("blNo")).getText());