📜  Selenium 断言

📅  最后修改于: 2020-11-06 04:55:38             🧑  作者: Mango

断言

断言确定应用程序的状态是否与我们期望的相同。如果断言失败,则测试用例失败并停止执行。

要在Web Driver中使用Assertion,您需要下载Testng jar文件并将其添加到eclipse中。从下面给出的链接下载Testng jar文件:

有两种类型的断言:

  • 硬断言
  • 软断言

硬断言

硬断言是在测试用例失败时引发AssertException的断言。在硬断言的情况下,您可以使用java异常之类的catch块来处理错误。假设我们在一个套件中有两个测试用例。套件中的第一个测试用例具有失败的断言,如果我们要在诉讼中运行第二个用例,则需要处理断言错误。硬断言包含以下方法:

  • 断言等于
  • 断言不等于
  • 断言真实
  • 断言
  • 断言为空
  • AssertNotNull

AssertFalse()

断言验证条件返回的布尔值。如果布尔值是false,则断言通过测试用例,如果布尔值是true,则断言将通过异常中止测试用例。 AssertFalse()方法的语法如下:

Assert.AssertFalse(condition);

让我们通过一个例子来理解:

  • 条件为假时
package mypack;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_test {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.navigate().to("https://www.spicejet.com/");
        Assert.assertFalse(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
        System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
        
    }

}

在上面的代码中,Assert.assertFalse()包含返回假值的条件。因此,它通过了测试用例。

在控制台上输出

  • 当条件为真时
package mypack;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_test {

public static void main(String[] args) 
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.spicejet.com/");
Assert.assertFalse(true);
System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
        
}}

在上面的代码中,Assert.assertFalse()方法包含真实条件。因此,断言失败,这意味着测试用例也失败了。断言失败将异常终止执行。

在控制台上输出

AssertTrue()

断言验证条件返回的布尔值。如果布尔值是true,则断言通过测试用例;如果布尔值是false,则断言通过异常中止测试用例。 AssertTrue()方法的语法如下:

Assert.AssertTrue(condition);

让我们通过一个例子来理解。

package mypack;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_test 
{

    public static void main(String[] args) 
           {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.navigate().to("https://www.spicejet.com/");
        driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).click();
          Assert.assertTrue(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
System.out.println(driver.findElement(By.cssSelector("input[id*='SeniorCitizenDiscount']")).isSelected());
        
    }

}

在上面的代码中,driver.findElement(By.cssSelector(“ input [id * =’SeniorCitizenDiscount’]”)))。click();该语句用于选择“高级公民”框。在下一条语句中,我们将应用断言来检查测试用例是否失败。 Assert.assertTrue()方法内部的参数返回的是真值,因此测试用例通过。

输出量

在控制台上输出

AssertEquals()

AssertEquals()是一种用于比较实际结果和预期结果的方法。如果实际结果和预期结果都相同,则断言毫无例外地通过,并且测试用例被标记为“通过”。如果实际结果和预期结果都不相同,则断言将失败,并带有异常,并且测试用例将标记为“失败”。下面给出了AssertEquals()方法的语法:

Assert.assertEquals(actual,expected);

让我们通过一个例子来理解。

  • 成人人数为5岁时。
package mypack;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Checkbox_test {
public static void main(String[] args) 
{
  // TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\\\work\\\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.spicejet.com/"); Assert.assertEquals("5Adult",driver.findElement(By.id("divpaxinfo")).getText());
System.out.println(driver.findElement(By.id("divpaxinfo")).getText());
}}


  • 当成人人数不等于5

在控制台上输出

AssertNotEquals()

它与AssertNotEquals()方法的函数相反。 AssertNotEquals()是一种用于比较实际结果和预期结果的方法。如果实际结果与预期结果不同,则断言毫无例外地通过,并且测试用例被标记为“通过”。如果实际结果和预期结果相同,则断言失败,并带有异常,并且测试用例被标记为“失败”。下面给出AssertNotEquals()方法的语法:

AssertNotEquals(actual,expected,message);

让我们通过一个例子来理解。

  • 当实际字符串不等于预期字符串。
package mypack;
import org.junit.Assert;
public class Checkbox_test {
public static void main(String[] args) {
        // TODO Auto-generated method stub
        Assert.assertNotEquals("Hello", "How are you");
        System.out.println("Hello...This is javaTpoint");
        
    }

}

在上面的代码中,实际字符串(即Hello)不等于预期的字符串(即How you)。因此,该断言通过了测试用例。这将执行下一条语句,下一条语句是System.out.println(“ Hello … This is javaTpoint”);。

输出量

  • 当实际字符串等于预期字符串。
package mypack;
import org.junit.Assert;
public class Checkbox_test {
public static void main(String[] args) 
{
// TODO Auto-generated method stub
Assert.assertNotEquals("Hello", "Hello");
System.out.println("Hello...This is javaTpoint");
}}

输出量

AssertNull()

AssertNull()是一种验证对象是否为null的方法。如果对象为空,则断言通过测试用例,测试用例标记为“通过”;如果对象不为空,则断言中止测试用例,并将测试用例标记为“失败”。 AssertNull()方法的语法如下:

Assert.assertNull(object);

让我们通过一个例子来理解。

  • 当对象为null时。
package mypack;
import org.junit.Assert;
public class Checkbox_test {

public static void main(String[] args) {
        
Assert.assertNull(null);
System.out.println("Hello...This is javaTpoint");
}}

输出量

  • 当对象不等于null时。
package mypack;
import org.junit.Assert;
public class Checkbox_test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
   Assert.assertNull(10);
    System.out.println("Hello World");
        
    }

}

输出量

AssertNotNull()

AssertNotNull()是一种验证对象是否为null的方法。如果对象不为空,则断言通过测试用例,测试用例标记为“通过”;如果对象为空,则断言中止测试用例,并将测试用例标记为“失败”。 AssertNotNull()方法的语法如下:

Assert.assertNotNull(object);

让我们通过一个例子来理解。

  • 当对象不为null时。
package mypack;
import org.junit.Assert;
public class Checkbox_test
{
  public static void main(String[] args) {
// TODO Auto-generated method stub
 Assert.assertNotNull(10);
System.out.println("C Language");
        
}}

输出量

  • 当对象为null时。
package mypack;
import org.junit.Assert;
public class Checkbox_test {
public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Assert.assertNotNull(null);
        System.out.println("C Language");
        
    }

}

输出量

软断言

到目前为止,我们已经了解了使用Testng框架进行Web驱动程序中的硬断言。在硬断言中,如果断言失败,则中止测试用例,否则继续执行。有时,即使断言失败,我们也希望执行整个脚本。在硬断言中这是不可能的。为了克服这个问题,我们需要在testng中使用一个软断言。