Site Loader
Auckland, New Zealand
Testing application using Selenium can be done by using your normal Java class file as discussed in the post,which can become complex in course of time, since your test can have multiple tests and modules for same application. You can create multiple class file and have functionality in each class file for more readability, but doing so put you in trouble, since you need to hardcode the calling routines, less organic code structure etc. Since there are many downsides of using it, many Java developers and automation test engineers as you and me felt to create a solution for this, which they call as frameworks. What is framework, we will discuss about it in detail in future posts, but for now, just consider framework is an “organized way of keeping your coding and keeping it best of industry standard readable. There are two best such framework for testing which is best used for testing by both developers and tester. They are
  • TestNG
  • JUnit
Anyways, we are going to deal with TestNG in this post and you can find a lot of information on TestNG from here. The first and foremost things to be done before working with TestNG are its “Setup”. TestNG as like Selenium is just a simple JAR file and need to be downloaded, its available here. Since we are going to run our test using famous Eclipse IDE, we need to download a TestNG Plug-in for Eclipse from here and installation procedure is given hereWith that said, we are all equipped to run our first program using TestNG in Selenium. Step 1 Let’s create a new Java project and give it a name as “SeleniumTestNGclip_image002 Click on libraries tab and add the reference libraries as shown below. clip_image003 Note, we have added a testing-5.14.1.jar file in our project reference along with selenium jar files. Step 2 Now your project explorer should look like this. clip_image004 Step 3 Congratulations!!! You have successfully created a TestNG project and ready to write your first test. Our next step is to add a Class file. The twist here is, you are not going to add a normal Java Class file, rather, you are going to add a TestNG Classas shown. clip_image006 Step 4 Select the TestNG class and click “Next” button, the wizard take you to the next screen as shown, here you can select the package name, source folder name and actual TestNG class name, we are going to add details as shown below clip_image008 There are lot of checkbox in the Annotations, well we will deal with them later, but for now, let’s them leave untouched. There is also a XML suite file, which is basically used by TestNG to run test in the configuration specified, well, let’s not go deep in to that, we will leave them untouched as of now. Step 5 Now your test looks something like this
package FirstTest;

import org.testng.annotations.Test;

public class NewTest {
  @Test
  public void f() {

  }
}
Let take a close look at the code, as oppose to our normal Java class file, testNG class file doesn’t have “Main” method. It has some special character @ followed by Test. Well, its called as “Annotations”, remember we saw lot of annotation checkbox in our TestNG class wizard in Step 4, but by default TestNG class will have a Test annotation Step 6 Lets change our code to open a browser and open website our www.executeautomation.com
package FirstTest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;

public class NewTest {
  @Test
  public void OpenSite() {
	  WebDriver driver = new InternetExplorerDriver();
	  driver.get("www.executeautomation.com");
  }

}
Step 7 To run TestNG class file, right click on the IDE and select run as TestNG image That’s it !!! Karthik KK

Post Author: Karthik kk

3 Replies to “How to run test using TestNG in Selenium”

  1. hi kartik,
    same prblm, the image of this page has some prblm..
    i cnt see the image..
    wen u wil fix it..

Leave a Reply to admin Cancel reply

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