Selenium教程

Selenium 断言

断言确定应用程序的状态是否与我们期望的相同。如果断言失败,则测试用例失败并停止执行。
要在Web Driver中使用断言,您需要下载Testng jar文件并将其添加到eclipse中。从下面给出的链接下载Testng jar文件: https://mvnrepository.com/artifact/org.testng/testng/6.7
有两种断言:
硬断言 软断言 Assertions

硬断言

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

AssertFalse()

Assertion验证条件返回的布尔值。如果布尔值是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()包含返回false值的条件。因此,它通过了测试用例。
控制台上的输出
Assertions 条件为true
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()方法包含真实条件。因此,断言失败,这意味着测试用例也失败了。断言失败将异常终止执行。
控制台上的输出
Assertions

AssertTrue()

Assertion验证条件返回的布尔值。如果布尔值是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()方法内部的参数返回的是真实值,因此测试用例通过。
输出
Assertions
控制台上的输出
Assertions

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 声明
控制台上的输出
Assertions

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 lidihuo");
        
    }
}
在上面的代码中,实际字符串(即,Hello)不等于预期的字符串(即,您好)。因此,该断言通过了测试用例。这将执行下一条语句,下一条语句是 System.out.println("H​​ello ... This is lidihuo");、
输出 >
声明 当实际字符串等于预期字符串时。
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 lidihuo");
}}
输出
Assertions

AssertNull()

AssertNull()是一种验证对象是否为null的方法。如果对象为空,则断言通过测试用例,测试用例标记为"通过";如果对象不为空,则断言中止测试用例,并将测试用例标记为"失败"。 AssertNull()方法的语法如下:
Assert.assertNull(object);
让我们通过一个例子来理解。
对象为空。
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 lidihuo");
}}
输出
声明 当对象不等于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);
让我们通过一个例子来理解。
当对象不为空时。
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");
        
}}
输出
声明 对象为空。
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中使用一个软断言。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4