How to disable Selenium Test Cases using TestNG Feature – @Ignore

HOME

The previous tutorial discussed prioritizing the Test Cases using TestNG. In this tutorial, we will see how we can disable the Test Cases using TestNG. 

Imagine there are 100 test cases in a Regression Test Suite. We need to execute 99 test cases in a release and do want not to execute any particular test case. But we do not want to delete that test case from the Test Suite also. In this case, TestNG has a feature that allows skipping a particular test case by setting the parameters to.

@Enabled Annotation

@Test(enabled = false)

To use two or more parameters in a single annotation, separate them with a comma:

@Test(priority = 3, enabled = false)

To Run the TestNG program, Right-click on the program, and select Run As TestNG Test.

Below is an example to implement the above-mentioned scenario.

import org.testng.annotations.Test;
 
public class TestNGDisableDemo {
 
    @Test(priority = 3)
     public static void FirstTest() {
           System.out.println("This is Test Case 1, but after priority Test Case 3");
     }
 
     @Test(priority = 4)
     public static void SecondTest() {
          System.out.println("This is Test Case 2, but after priority Test Case 4");
     }
 
     @Test(enabled = false)
     public static void ThirdTest() {
           System.out.println("This is Test Case 3, but now skipped");
     }
 
     @Test(priority = 1)
     public static void FourthTest() {
            System.out.println("This is Test Case 4, but after priority Test Case 1");
     }
}


The output of the above program is

@Ignore Annotation

There is another way of skipping the tests by using the @Ignore annotation.

TestNG lets you ignore all the @Test methods :

  • In a class (or)
  • In a particular package (or)
  • In a package and all of its child packages

Ignore a Test

Below is an example where we have skipped the execution of a particular test.

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

public class IgnoreDemo{
	
	@Test
	public void FirstTest() {
	
		System.out.println("This is Test Case 1");
    
	}
	
	@Test
	public void SecondTest() {
	
		System.out.println("This is Test Case 2");
    
	}
	
	@Ignore
	@Test
	public void ThirdTest() {
	
		System.out.println("This is Test Case 3");
    
	}
	
	@Test
	public void FourthTest() {
	
		System.out.println("This is Test Case 4");
    
	}

}

In the above example, we have assigned @Ignore to the third test. So, this test should be skipped while the execution.

The output of the above program is

Ignore a Class

To understand this concept, we need to create 2 test classes – IgnoreDemo and IgnoreDemo2. Imagine we want to ignore all the tests in the class, so we can use @Ignore at the class level.

IgnoreDemo

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

@Ignore
public class PriorityDemo {
	
	@Test
	public void FirstTest() {
	
		System.out.println("This is Test Case 1");
    
	}
	
	@Test
	public void SecondTest() {
	
		System.out.println("This is Test Case 2");
    
	}
	

	@Test
	public void ThirdTest() {
	
		System.out.println("This is Test Case 3");
    
	}
	
	@Test
	public void FourthTest() {
	
		System.out.println("This is Test Case 4");
    
	}

}

IgnoreDemo2

package com.example;

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;


public class PriorityDemo2 {
	
	@Test
	public void FirstTest() {
	
		System.out.println("This is Test Case 1 of Class 2");
    
	}
	
	@Test
	public void SecondTest() {
	
		System.out.println("This is Test Case 2 of Class 2");
    
	}
	

	@Test
	public void ThirdTest() {
	
		System.out.println("This is Test Case 3 of Class 2");
    
	}
	
	@Test
	public void FourthTest() {
	
		System.out.println("This is Test Case 4 of Class 2");
    
	}

}

To run both the classes together, we need to create a testng.xml. The easiest way is to select both the classes and Right-Click and select TestNG -> Convert to TestNG.

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="com.example.PriorityDemo2"/>
      <class name="com.example.PriorityDemo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Right-click on the testng.xml and select Run As -> TestNG Suite.

As we have assigned the @Ignore annotation at the class level of PriorityDemo class, it will ignore all the 4 tests present in that particular class.

The output of the above program is

We are done. Congratulations on making it through this tutorial and hope you found it useful!

TestNG Framework – How to download and install TestNG in Eclipse

HOME

In the previous tutorial, we discussed what is TestNG and why it is important. This tutorial will discuss how can we download and install TestNG in Eclipse and how to use it.

Pre-Requisite 

1) Eclipse should be installed and configured. Please refer to Install and Configure to set up Eclipse on your system.

Install/Setup TestNG

1) Launch Eclipse and go to the “Help” option present at the top and select –“Install New Software”.

2) A dialog box will appear, click the Add button.

3) A new dialog box will appear. Mention the Name as TestNG and the location asTestNG P2 – https://testng.org/testng-p2-update-site&#8221; and click the Add button.

4) This time we will see TestNG is added to Install dialog box.

5) Accept the terms and conditions and then click the Finish button.

6) Once the installation is completed, you will get a message to Restart the Eclipse. Select Restart the Eclipse

7) To verify if TestNG is installed successfully or not, go to Window, select Show View, and then Other.

8) Select Java and see, within the Java folder, you will see TestNG. This shows that TestNG is successfully installed on the machine.

Steps to follow to create a TestNG class

1) Create a new TestNG class. Right-click on the Folder where you want to create the TestNG class. Select TestNG and then Create the TestNG class as shown in the below image.

2) In the below image, we can see that the Source folder is the name of the folder we want to create the class, and we can mention the name of the class in the Class name. Under annotations, I have checked @BeforeTest and @AfterTest and click the Finish button.

3) We can see that the structure of the new TestNG class looks like as shown below.

4) In the below example, we want to navigate to an Amazon page and search for Hard Drive.

@BeforeTest : Launch Firefox and direct it to the Base URL

@Test : Search for HardDrive

@AfterTest : Close Firefox browser

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;

public class TestNG_Demo {

   public WebDriver driver;

   @BeforeTest
    public void beforeTest() {
      
     System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("https://www.amazon.com//");
 }

@Test
public void Validation() {
    driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")).sendKeys("hard drive");
    //XPath for search button
      driver.findElement(By.xpath("//*[@class='nav-input']")).click();
   }

@AfterTest
public void afterTest() {
    driver.quit();
  } 
}

5) To execute this program, we need to Right-click and select Run as – TestNG Test.

6) The result will look like something shown below. Here, we can see that Test Case Passed is 1, Failed 0, and Skipped 0.

7) As we know that TestNG also produce HTML Reports. To access the report, go to the Eclipse folder, and you can see a folder with name test-output inside the Project where we have created TestNG class. Here, it is  C:\Users\vibha\Downloads\eclipse-workspace\Demo

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

TestNG Framework – Introduction to TestNG

HOME