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.

21 comments:

Freenty said...

Im receive some error:
Exception in thread "main" org.testng.TestNGException:
Test element must define the name attribute
at org.testng.xml.TestNGContentHandler.xmlTest(TestNGContentHandler.java:225)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:443)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.testng.xml.Parser.parse(Parser.java:162)
at org.testng.TestNG.setTestSuites(TestNG.java:507)
at org.testng.TestNG.configure(TestNG.java:956)
at org.testng.remote.RemoteTestNG.configure(RemoteTestNG.java:49)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:122)
I do not know that is not so ..

Unknown said...

Hi Freenty,
From the error log it seems like you are using the configuration file ( testng.xml) to run the testcases. Are you ??

If yes there is some parsing error in the file.
Else the code I have posted on the forum is working fine on my machine.

jay said...

Hi Gaurang,
Fisrt of thanx for that procedure to create a test case.
I followed all the steps but failed in viewing class..
I am actually not able to see any classes when clicked on browse button in Run As -> Open Run Dialog box.
check out these logs ..
Exception in thread "main" org.testng.TestNGException:
Test element must define the name attribute
at org.testng.xml.TestNGContentHandler.xmlTest(TestNGContentHandler.java:225)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:443)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.testng.xml.Parser.parse(Parser.java:162)
at org.testng.TestNG.setTestSuites(TestNG.java:507)
at org.testng.TestNG.configure(TestNG.java:956)
at org.testng.remote.RemoteTestNG.configure(RemoteTestNG.java:49)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:122)

Please help me fix this issue.

Unknown said...

Hi Jay,
When you open the Run dialog box.
Did you choose the project first before going to browse on the class/method/groups ???
If you have choose the project and still not getting the class/method/groups the problem is Eclipse is not able to identify the Class as TestNG class. Might be due to the libraries are not imported properly.

Nagaraj Hebbar said...

Hi i just want to know how to take the report.....Which test case is passed, which test case is failed

Unknown said...

Project_Folder/test-output/index.html
Will contain the overall result. the number of test cases passed, failed, skipped

Freef said...

Tnx all simple!
But make better video resolution)

Nagaraj Hebbar said...

Hi,
Facing problem with Selenium...i want your mail ID so i can send the screen shot where exactly am facing the problem

Anonymous said...

Hi,

Thanks for this detailed info, can you please mentioned steps abuot from where to get selenium and testng jar files , as it'll be useful to new users.

Thanks

Anonymous said...

The thought of 'parameterizing' seems like a really good idea for efficiency and test case maintenance. I'm going to be using Seleenium+TestNG+Eclipse. I was reading your publication about TestNG where you mentioned a configuration file ['testng.xml'] could be created and included on the command line when starting a test. I like the idea of this configuration file but how does that carry over to Eclipse? Ideally I was hoping to administering the test case runs via Eclipse. I see in Eclipse, under 'Run As', there is a 'Run Configurations' option but not sure how that synchs up with the 'testng.xml' information. I was also reading about using a @DataProvider annotation.

Thank you. I'll keep looking in the Eclipse documentation in the meantime.

Anonymous said...

Will there be any tutorial for TestSuite instead of just a TestCase?
I've followed your tutorial and it works great but I'm not trying to use TestSuite and get a "lot" of issues.
Thanks

Anonymous said...

Hi,
Hi,
I have eclipse 3.4 and trying to install TestNG plugin from http://beust.com/eclipse. It seems like there are no plugins available in this link. Please help me in providing the correct link from where i can download TestNG plugin for Eclipse...

Thanks
Susheel

Unknown said...

Link is same. Check out that in browser. May be some antivirus or firewall is blocking it. If nothing works download the plugin manuall from below link and extract it into your plugins directory.
http://testng.org/testng-eclipse-5.9.0.0.zip

Anonymous said...

Hi Gaurang,

Thanks for your reply. But even the direct download link http://testng.org/testng-eclipse-5.9.0.0.zip seems to be a broken link. Your comments would be helpful on this.

Thanks,
Susheel

Unknown said...

Yeah, sorry I posted the link without testing it.
Check out this
http://testng.org/testng-5.9.zip

Anonymous said...

Hi Gaurang,

The link that you had sent http://testng.org/testng-5.9.zip is for TestNG API download. But I am looking for a download link for testng-eclipse plugin. Please help...

Unknown said...

Yeah.. right. My mistake. See I have zipped the file from my plugin directory and have put it on the net. Download it and extract in you plugin directory. Hope it works.
Download TestNG Eclipse plugin

Ajay said...

I have followed the steps that you have clearly mentioned above but when i run that script using testNG,i get an error "you need to specify atleast one testNG.xml or one class".what does that mean??

Rahul P said...

Hii Guarang I got thsi error...


[Parser] Running:
D:\Workspace\DNBI\temp-testng-customsuite.xml

FAILED CONFIGURATION: @BeforeClass startSelenium
java.lang.RuntimeException: Could not start Selenium session: Internal Server Error
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:89)
at Google.startSelenium(Google.java:12)
Caused by: com.thoughtworks.selenium.SeleniumException: Internal Server Error
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:97)
at com.thoughtworks.selenium.HttpCommandProcessor.getCommandResponseAsString(HttpCommandProcessor.java:168)
at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:104)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:86)
at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:262)
at com.thoughtworks.selenium.HttpCommandProcessor.start(HttpCommandProcessor.java:223)
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:81)
... 23 more
... Removed 22 stack frames
SKIPPED: googling

===============================================
Google
Tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 0
===============================================


===============================================
DNBI
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 0
===============================================

Ravi Chandra said...

Hi All,

I am using Junit with Eclipse. I am able to record usign selenium IDE and run the tests as Junit tests in eclipse. I see the regular Junit report and if I export the test result, It is in xml format with number of runs and failures.

Is there any way that I can get a detailed test report for these JUnit test cases in eclipse??

Thanks in Advance,
Chandu.

Unknown said...

HI Gaurang
Can u suggest me the exact configuration for instaliing selenium ide and using it with TESTNG in ecllipse..
please tell me the jar files to add too.

Actually I am getting configuration Failure while in run the test case with Testng plugin.

Thanks in Advance

Post a Comment