2016-03-15 66 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());