Install TestNg on Eclipse
Create Testcase
Run the Testcase
Installing TestNG on Eclipse
follow the simple steps below to install the TestNG plug-in on the eclipse IDE.
- Open the Install/Update window from Help->Software Updates->Find and Install
- Click on the radio button "Search for new feature to install" and click "Next" button
- Click on the "New remote site..." and supply name as "TestNG" and URL as "http://beust.com/eclipse" and click on "OK"
- Make sure the TestNG is checked and click on the "Finish" Button
- Now follow the other steps and TestNG will be installed.
Create TestCase
To create the testcase in the eclipse you also need to create a project and import some require JAR files. So let's being with that.
- Create the project
Now to begin with the the eclipse you need to first create the project. it will contain all the packages and packages will contain all necessary Java files (testcases ) and configuration files ( xml files )
So just navigate to the file->new->Java Project. Give it some appropriate name and create it.
- Import required JAR files
Now to support the testNG framework and selenium API you need to import their jar files.
So just click on the Libraries tab and then click on the "Add External Jars.." import the following libraries from you hard disk.
selenium-java-client-driver.jar
selenium-server.jar
testng-5.9-jdk15.jar
If you have already created the project. Just open the project properties dialog box from Project->Properties.
Now click on the "Java Build Path" and then click on the "Libraries" tab and import all the libraries mentioned above.
- Write testcase.
Now we need to create a new class file and need to write down the testcase. Just Right click on your project and click on the new->Class. Provide the name as google in the dialog box.
Now just replace the code the Google.java file with the below code.
Google.java
import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class Google {
private Selenium selenium;
@BeforeClass
public void startSelenium() {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.com");
selenium.start();
selenium.open("http://www.google.com");
}
@AfterClass(alwaysRun=true)
public void stopSelenium() {
this.selenium.stop();
}
@Test(groups="search")
public void googling() {
try {
selenium.type("name=q", "qtp-help.blogspot.com");
selenium.click("name=btnG");
selenium.waitForPageToLoad("60000");
assertTrue(selenium.isTextPresent("qtp-help.blogspot.com"));
} catch (SeleniumException e) {
fail(e.getMessage());
}
}
}
Run the Testcases.
Now you have created the testcases. But before we run it. Let's start the selenium server.
Now to run the testcase right click on the project and then click on the Run As->Open Run Dialog box
Now right click on the TestNg in the left pannel and click "New". Now you need to create a run configuration based on how you would like to run you testcases(s).
Here you will find five option to run your testcases(s). Let's see one by one what all these options are.
First browse your project by clicking on the browse button.
Class: If you will select this, all the testcases under the selected class will run on b y one.
Method: Choose this option if you want only particular testcase to run.
Group: This option will allows you to run the Groups of the testcases if you have mention that in your class. You can browse all the Groups in the class and can choose particular group to run.
For All the above option a temporary config will be created in you project folder.
Package:Choose this option if you want to run all the classes inside a package.
Suite: Use this option if you want to run any of the testsuit from your class. However to use this you need to create the TestNG configuration file.