Kohinoor Odia Calendar 2024 Download | CHSE Odisha +2 Time Table 2024 For Arts, Science, Commerce | Odisha Govt Calendar 2024 PDF Download

Ad 468 X 60

How to integrate testng in selenium

 TestNG is a popular testing framework for Java, which provides various annotations, assertions, and configurations to make testing easier and more effective. Here are the general steps to integrate TestNG in Selenium:


Set up your Selenium project by downloading and installing the necessary drivers, such as the ChromeDriver, GeckoDriver, or EdgeDriver.

Add the TestNG dependency to your project by adding the following lines to your project's pom.xml file (if using Maven):

<dependency>

    <groupId>org.testng</groupId>

    <artifactId>testng</artifactId>

    <version>7.4.0</version>

</dependency>

Create a new TestNG class by right-clicking on your project folder, selecting New -> Other -> TestNG -> TestNG Class.

Write your test methods in the new TestNG class using TestNG annotations such as @Test, @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest, and so on. These annotations help TestNG to identify which methods are test methods, which methods are setup methods, and which methods are teardown methods.

In your test methods, use the Selenium WebDriver to perform the necessary actions and assertions on the web page.

Run your TestNG tests by right-clicking on your TestNG class and selecting Run As -> TestNG Test.
Here is an example TestNG test class that uses Selenium WebDriver to search for a keyword on Google:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;

public class GoogleSearchTest {
    WebDriver driver;

    @BeforeMethod
    public void setUp() {
        // Set up the Chrome driver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void testGoogleSearch() {
        // Navigate to Google
        driver.get("https://www.google.com/");

        // Enter the search keyword
        driver.findElement(By.name("q")).sendKeys("TestNG");

        // Click the search button
        driver.findElement(By.name("btnK")).click();

        // Verify that the search results page contains the expected text
        String expectedText = "TestNG - Welcome to TestNG";
        Assert.assertTrue(driver.getPageSource().contains(expectedText));
    }

    @AfterMethod
    public void tearDown() {
        // Close the browser
        driver.quit();
    }
}
This example TestNG class sets up the ChromeDriver, navigates to Google, enters a search keyword, clicks the search button, and verifies that the search results page contains the expected text. The @BeforeMethod annotation sets up the Chrome driver before each test, the @Test annotation specifies the test method, and the @AfterMethod annotation closes the browser after each test.



SHARE THIS POST   

  • Facebook
  • Twitter
  • Myspace
  • Google Buzz
  • Reddit
  • Stumnleupon
  • Delicious
  • Digg

0 comments:

© Copyright 2024 Yes Odisha, All Rights are Strictly Reserved.