Wednesday, May 13, 2009

TestNG with Eclipse

After exploring both JUnit and TestNG, I finally decided to go with the TestNG. And I moved on to eclipse IDE aswell, as it will provide ease in writing and running the testcases ( as compared to the dos prompt and notepad). The following blog will show you how to install TestNG plug-in in eclipse and how to create and run testcase.
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.

Tuesday, May 5, 2009

TestNG

TestNG is yet an another framework to work in JAVA. Both looks almost same on the surface but there are vast differences between their frameworks. JUnit is designed to do the unit testing, and it's doing it very well while the TestNG has designed to do the higher level thing. So there are many features available in the TestNG that you will not find in JUnit.

Before we checkout the features provided by the TestNG, let's take a look at how to make the simple testcase using TestNG and run it.


TestNG Annotations
Following are some of the annotations we have used in our example
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@Parameters: Describes how to pass parameters to a @Test method.
@Test: Marks a class or a method as part of the test.

If you look at the @BeforeClass and @AfterClass annotation they are same as of JUnit, the only difference is the method annoted with @BeforeClass or @AfterClass need not to be static as compared to the JUnit.

TestNG Testcase

Google.java

import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;

public class Google {

private Selenium selenium;

@BeforeClass
@Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url"})
public void startSelenium(String host, String port, String browser, String url) {

this.selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, url);
this.selenium.start();
this.selenium.open(url);
}

@AfterClass(alwaysRun=true)
public void stopSelenium() {
this.selenium.stop();
}

@Test
@Parameters({"search","expected"})
public void googling(String search, String expected) {
try {
selenium.type("name=q", search);
selenium.click("name=btnG");
selenium.waitForPageToLoad("60000");
assertTrue(selenium.isTextPresent(expected));

} catch (SeleniumException e) {
fail(e.getMessage());
}
}
}



Configuration file.
Configuration file is a normal xml file. which defines all the test cases, test suits, their names,all the parameters and their values and many more things.

Let's look at configuraion file for the above testcase
GoogleTestng.xml



















Set CLASSPATH
Before you run your testcase from the command line, You need to set the classpath.
Find out the file "testng-5.9-jdk15.jar" and set it's path in classpath variable.
The name of the file may differ depending upon the version of TestNG you are using.
To set the class path do as follows:
1. just right click on My Computer icon, Click on the Advance tab, then click on the Environment Button.
2. In the System Variable double click on the CLASSPATH.
3. In the variable value add the full path of your "testng-5.9-jdk15.jar" file.
Run the Testcase.
  • Make sure that the Selenium server is running.
  • Save the above Google.java file at your desired location
  • Compile the file using the following command on the shell prompt.
    javac Google.java
  • Run the file using the following command on the shell prompt.
    java org.testng.TestNG GoogleTestng.xml
  • Use the following command to see the result.
    start test-output\index.html

Once you will run the testcase on more interesting thing you will find about the TestNG, the reporting. TestNg has built in primary reporting facility which will let you know how many test cases from the particular test suit has run, how many of then passed and how many of them failed.