TestNG in Selenium Java

Share

TestNG in Selenium Java

  1. TestNG is a popular testing framework that is used in conjunction with Selenium to run automated web tests written in Java. TestNG is inspired by JUnit but introduces some new functionalities that make it more powerful and easier to use. It’s widely used for configuring test suites in a flexible way, and it provides capabilities such as parallel test execution, data-driven testing, and more.

    Here’s a basic guide to using TestNG with Selenium in Java:

    1. Add Dependencies: If you’re using Maven, you can add the TestNG and Selenium dependencies to your pom.xml file:

      xml
      <dependencies>
      <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.1.0</version> <!-- or latest version -->
      <scope>test</scope>
      </dependency>
      <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version> <!-- or latest version -->
      </dependency>
      </dependencies>
    2. Write Test Class: You can write a test class using the @Test annotation provided by TestNG.

      java
      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.testng.Assert;
      import org.testng.annotations.AfterClass;
      import org.testng.annotations.BeforeClass;
      import org.testng.annotations.Test;

      public class ExampleTest {
      private WebDriver driver;

      @BeforeClass
      public void setUp() {
      driver = new FirefoxDriver(); // Or other browser driver
      driver.get("https://www.example.com");
      }

      @Test
      public void verifyTitle() {
      String expectedTitle = "Example Domain";
      String actualTitle = driver.getTitle();
      Assert.assertEquals(actualTitle, expectedTitle);
      }

      @AfterClass
      public void tearDown() {
      if (driver != null) {
      driver.quit();
      }
      }
      }

    3. Run Tests: You can run the test through your IDE (most modern IDEs like IntelliJ and Eclipse have support for TestNG) or via command line using Maven or Gradle.

    4. Data-Driven Testing: TestNG allows you to easily create data-driven tests using the @DataProvider annotation.

      java
      @DataProvider(name = "input-data")
      public Object[][] inputData() {
      return new Object[][] { {"input1", "expectedOutput1"}, {"input2", "expectedOutput2"} };
      }

      @Test(dataProvider = "input-data")
      public void testWithData(String input, String expectedOutput) {
      // Your test code here
      }

    5. Other Features: TestNG also provides many other features like grouping of tests, parallel execution, support for different test configurations (using the @BeforeSuite, @BeforeTest, etc. annotations), and more.

    6. TestNG XML Configuration: You can create an XML file to handle your suite of tests, which gives you control over the execution order, parallel execution, and dependencies between methods and groups.

    7. Reporting: TestNG provides good reporting capabilities that allow you to create HTML reports, including information on the number of tests run, the number of tests passed, etc.

    By combining Selenium with TestNG, you get a powerful toolset for writing robust, maintainable automated tests for web applications. It offers great flexibility and a wide range of features to make your testing process more efficient and effective.

Demo Day 1 Video:

 
You can find more information about Selenium in this Selenium Link

 

Conclusion:

Unogeeks is the No.1 IT Training Institute for Selenium Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on  Selenium here – Selenium Blogs

You can check out our Best In Class Selenium Training Details here – Selenium Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook:https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *