First test using TestNG Framework.

First TestNG Test – How to run selenium test using TestNG framework? We have seen basic details about TestNG framework, how useful it is.

Suggested post to Read:      Introduction to TestNG

Now, let us see, how can we run the selenium WebDriver test using TestNG framework.

The basic pre-requisite for this is, to have testing jar file.
To know about how to install TestNG, refer below post.

Suggested post to Read: How to install TestNG in Eclipse IDE

Let us consider very basic test to launch gmail.com & get the title of the page.
Note that, when we use TestNG framework, there is no need to write main method on our test. We can run the test cases without main method.
In order to write a test case in TestNG framework, it is very simple. Just create some method for your test. Write test code in that method.
Now, here is role of TestNG framework. We can use @Test annotation for running the code from that method without writing main method.
Make sure that, you configure testing.jar in your project.
After this, when you are asked to import “Test” annotation, make sure that, you import it from testing. Refer below screen shot.
here is the sample code for this –
package basicsOfSelenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class FirstSeleniumTest {
 @Test
 public  void gmailTest() {
   WebDriver driver = new FirefoxDriver();
   driver.navigate().to("https://www.google.co.in");
   String TitleOfPage = driver.getTitle();
   System.out.println("Title of Current page is : "+TitleOfPage);
     driver.close();
 }
}
In above code, @Test annotation is used to run the method – gmailTest().
You can right click on the program, Run As -> TestNG Test
After the run is successful,  You can see the report as in below image –
Note that, when you run TestNG test, the XML file will be generated in a temp folder & system will use that testing.xml file to run our test.
To know more about TestNG.xml –

Refer post: what is TestNG.xml 

Hope this helps !!!!

Introduction to TestNG

TestNG is a Testing Framework to group Test Cases, Prioritize Test Cases, Run Test Batches, and Generate Detailed Test Reports.
TestNG is not a Test Tool to conduct Testing operations directly, It won’t recognize Software Objects / Software Elements, but Selenium is used for Testing Operations, and It can recognize Software Objects/Software Elements.
Below image shows that TestNG plays an important role in automation testing.

TestNG is an open source framework where NG means Next Generation.
TestNG is specially designed to cover all types testing categories like Unit, Functional testing, Integration testing, End-to-end etc.
TestNG is a preferred framework of QA analysts as it allows you to generate test reports in both HTML and XML formats.
TestNG enable you to group test cases easily which is not possible in JUnit.
TestNG is an advance framework designed in a way to leverage the benefits by both the developers and testers.
TestNG is an open source framework which is distributed under the Apache software License and is readily available for download.
TestNG is considered to be superior to JUnit because of its advance features Features of TestNG
    Support for Annotations
    Supports parallel testing
    Support for parameterization
    Support for Data Driven Testing using @Dataproviders
    Can include or exclude test methods test execution.
    Setting execution priorities for the test methods
    Readily supports integration with various tools and plug-ins like build tools (Ant, Maven etc.), Integrated Development Environment (Eclipse).
    Predifindly generates effective Report Generation using ReportNG
I like many things about TestNG.
  1. The methods that use the BeforeClass and AfterClass annotations are not static. Because of this, the WebDriver object does not have to be static either.
  2. The testng.xml file allows lots of flexibility in selecting the test classes and test methods to be executed. You can exclude or include test methods or exclude and include groups.
  3. You can specify the listener to be used by all test classes in the testng.xml file
  4. You can specify how the test scripts can be run in parallel in the testng.xml file
  5. When using parameters, it is possible to have multiple data providers. It is also possible to move a data provider to another class.
  6. It is so easy to re-run only the failed tests
  7. There are default reports in TestNG; the default HTML report can be improved by adding additional information to it; there is also a one file HTML report that is suitable for sending by email.
  8. It is possible to have test scripts dependent on other test scripts or dependent on test groups
  9. You can use soft assertions for grouping multiple normal assertions together; everything included in a soft assertion is executed even if some of assertions fail
  10. Page Factories

 

 Hope this helps !!!!

What is ITestContext in testng? (Selenium). How to use ITestContext in Selenium?

Sharing data across Tests in different TestClasses – 

ITestContext is basically used to store and share data across the tests in selenium by using TestNG framework.
Let us consider below scenario –
We have 10 test cases (@Test methods) to be executed to complete one end to end test case. Now in call 10 test cases (@Test methods) we are sharing some data like “Customer_id”, which should be unique and same in our end to end test case i.e. in our 10 @Test methods. During the execution of end to end test case, it should be same.
To handle this scenario, we have two ways – 
            1.      If all 10 @Test methods are in same class, then we can store “Customer_id”  in a class level variable (instance variable)  and share it. But it may require high maintenance.
          
            2.      Another way to handle this is, ITestContext
Let us see How ITestContext works.
In any @Test method, we can use “iTestContext” by passing it as a parameter to the method.  Syntax as below – 

 @Test
public void test1a(ITestContext context){

}
Here, we can set the value that we want to share  in ITestContex, as below –
 @Test
public void test1a(ITestContext context){

String Customer_id = "C11012034";
context.setAttribute("CustID", Customer_id);
}
Now, we can get this value, which is stored in ITestContext variable with other tests, as below –

  String Customer_id1 = (String) context.getAttribute("CustID");
Below piece of code can help you to understand how ITestContext works.

package iTestContextLearn;

import org.testng.ITestContext;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test1 {

@BeforeTest
public void SetData(ITestContext context){

String Customer_id = "C11012034";
context.setAttribute("CustID", Customer_id);
System.out.println("Value is stored in ITestContext");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");

}
@Test
public void Test1a(ITestContext context){
String Customer_id1 = (String) context.getAttribute("CustID");
System.out.println("In Test1, Value stored in context is: "+Customer_id1);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");

}
@Test
public void Test2a(ITestContext context){
String Customer_id1 = (String) context.getAttribute("CustID");
System.out.println("In Test2, Value stored in context is: "+Customer_id1);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");

}
}
Note: All the @Test methods that we are going to run must be in one class only. We will be calling methods from different other classes. We can share the data with methods of different classes.
Hope this helps !!!! Post your comments / questions