logo

The best IT Trainig Institute In Gurgaon

Use Assertion in Selenium TestNG

package asc;

import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class VerifyTitle {

	@Test
	public void titleTest() {
		
		
		String expectedTitle = "Electronics, Cars, Fashion, Collectibles & More | eBay";
		WebDriverManager.chromedriver().setup();
		ChromeDriver driver = new ChromeDriver();
		driver.get("https://www.ebay.com/");
		
		String actualtitle = driver.getTitle();
		System.out.println(actualtitle);
		Assert.assertEquals(actualtitle, expectedTitle);
		
		driver.close();
		
		
	}
}
                
Package and Imports
package asc;

import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

                
                    
  • package asc;: Declares the package name 'asc' for this class.
  • import org.openqa.selenium.chrome.ChromeDriver;: Imports the ChromeDriver class from Selenium, which is used to control Google Chrome.
  • import org.testng.Assert;: Imports the Assert class from TestNG, which is used to perform assertions in tests.
  • import org.testng.annotations.Test;: Imports the Test annotation from TestNG, used to denote a method as a test method.
  • import io.github.bonigarcia.wdm.WebDriverManager;: Imports WebDriverManager, a utility to automatically manage (download and configure) WebDriver binaries like chromedriver.
Test Class and Method
public class VerifyTitle {
    @Test
    public void titleTest() {
        String expectedTitle = "Electronics, Cars, Fashion, Collectibles & More | eBay";
        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.ebay.com/");
        
        String actualtitle = driver.getTitle();
        System.out.println(actualtitle);
        Assert.assertEquals(actualtitle, expectedTitle);
        
        driver.close();
    }
}
            
                
Explanation:
  1. Class Definition:
    • public class VerifyTitle: Defines a public class named VerifyTitle.
  2. Test Method:
    • @Test: The titleTest method is annotated with @Test, indicating that it is a TestNG test method.
  3. Expected Title:
    • String expectedTitle = " Electronics, Cars, Fashion, Collectibles & More | eBay ";: This string holds the expected title of the eBay homepage.
  4. Setting Up WebDriver:
    • WebDriverManager.chromedriver().setup();:
      This line automatically sets up the ChromeDriver for the test. WebDriverManager manages the browser driver, making it easy to get the correct version of chromedriver.
  5. Creating WebDriver Instance:
    • ChromeDriver driver = new ChromeDriver();:
      This creates an instance of the Chrome browser.
  6. Navigating to URL:
    • driver.get("https://www.ebay.com/");: 
      This line instructs the browser to open the specified URL (eBay homepage).
  7. Getting the Actual Title:
    • String actualtitle = driver.getTitle();:
      This retrieves the title of the current webpage and stores it in the variable actualtitle.
  8. Printing the Actual Title:
    • System.out.println(actualtitle);:
      This line prints the actual title of the page to the console for reference.
  9. Assertion:
    • Assert.assertEquals(actualtitle, expectedTitle);:
      This assertion checks if the actualtitle matches the expectedTitle. If the titles do not match, the test will fail, and TestNG will report the error.
  10. Closing the Browser:
    • driver.close();:
      This closes the browser after the test is completed.