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 !!!!