TestNG – Test Automation with Selenium

by seetaram on January 15, 2012

TestNG framework can be used for automation testing with Selenium (web application automation testing tool). Then, definitely a question will popup in our mind “why this framework is needed?” Is it possible to execute Selenium tests without frameworks like JUnit or TestNG. Answer is “Yes”, it is possible to execute Selenium tests without using these frameworks. But, is it possible to get proper reports without framework? How the verification points or checkpoints are going to be handled? How the exception handling is done? If we do not have any frameworks, then it is difficult to get proper reports, handle checkpoints, or exception handling.

Either we should use any of the frameworks available like JUnit, TestNG or we should design our own framework.

In one of the previous post, I have explained how to setup TestNG with Eclipse IDE.  It is better go through that post before doing anything explained in this post.

Let us have a look at the code snippet below:

package com.selftechy.testng;

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

public class TestNGDemo extends SeleneseTestBase{
	public Selenium selenium;
	
	@BeforeMethod
	public void setUp()throws Exception{
		selenium = new DefaultSelenium("localhost",4444,"*chrome","http://selftechy.com");
		selenium.start();
		selenium.windowMaximize();
	}
	
	@Test
	public void testNGDemo() throws Exception {
		selenium.open("/");
		selenium.click("link=TestNG (Next Generation Testing Framework) – Understanding Annotations");
		Thread.sleep(10000);
		verifyTrue(selenium.isTextPresent("Annotation:"));
		selenium.click("link=Selenium");
		Thread.sleep(10000);
		selenium.click("css=a[title=\"Introduction to Selenium\"]");
		Thread.sleep(10000);
	}
	
	@Test
	public void testTestAbout() throws Exception {
		selenium.open("/");
		selenium.click("link=About");
		selenium.waitForPageToLoad("30000");
		verifyTrue(selenium.isTextPresent("Selenium, QTP, Java"));
		selenium.click("link=Home");
		selenium.waitForPageToLoad("30000");
		selenium.click("link=TestNG – Next Generation Testing Framework");
		selenium.waitForPageToLoad("30000");
		verifyTrue(selenium.isTextPresent("Need for a Testing Framework:"));
	}
	
	@AfterMethod
	public void tearDown(){
		selenium.stop();
		
	}
	
}

The Java class “TestNGDemo” is implemented in Eclipse IDE using TestNG framework.

In the above code, there are two test methods, which are marked with @Test annotation. There are two other methods, “setUp” and “tearDown” which are marked with @BeforeMethod and @AfterMethod annotations. Hence Before executing each test, setUp method will be executed. After the execution of each test, tearDown gets executed. Hence, Selenium gets instantiated and browser gets opened and closed twice during the execution.

How to execute the test?

Click Run –> Run As –> TestNG Test

TestNGRunAs

Output of the execution should be as below:

[TestNG] Running:
  C:\Documents and Settings\parwathi\Local Settings\Temp\testng-eclipse-1339593504\testng-customsuite.xml

PASSED: testNGDemo
PASSED: testTestAbout

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.EmailableReporter@1a679b7: 15 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@1e51060: 16 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@337d0f: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@e102dc: 0 ms
[TestNG] Time taken by [TestListenerAdapter] Passed:0 Failed:0 Skipped:0]: 0 ms

Test execution also creates set of XML & HTML reports. TestNG creates “test-output” folder in the root folder, inside which we can see the reports.  Below are the screenshots of some of the reports created by TestNG.

emailable-report

index

methods

What we have learnt so far?

  1. Need for TestNG framework
  2. How to create a Selenium test with TestNG
  3. Annotations used in the test
  4. How to execute the Selenium tests with TestNG
  5. What are reports generated and how they look
  6. What is the output of the execution

{ 29 comments… read them below or add one }

Rony January 19, 2012 at 12:59 pm

Hi Seeta,
Your Blog is really very helpful!!!
I have tried your code above and after some changes I am able to run your code. First @Test always executed and passed but the second @Test always skipped. I do not know why this is happening…Pasting my code here. Please help.

Thanks,
Rony

package com.selftechy.testng;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import org.openqa.selenium.server.SeleniumServer;

public class TestNGDemo extends SeleneseTestBase{
public Selenium selenium;
public SeleniumServer sel;
@BeforeMethod
public void setUp()throws Exception{
sel = new SeleniumServer();
selenium = new DefaultSelenium(“localhost”,4444,”*chrome”,”http://selftechy.com”);
sel.boot();
selenium.start();
selenium.windowMaximize();
}

@Test
public void testNGDemo() throws Exception {
selenium.open(“/”);
selenium.click(“link=TestNG (Next Generation Testing Framework) – Understanding Annotations”);
Thread.sleep(10000);
verifyTrue(selenium.isTextPresent(“Annotation:”));
selenium.click(“link=Selenium”);
Thread.sleep(10000);
selenium.click(“css=a[title=\”Introduction to Selenium\”]”);
Thread.sleep(10000);
}

@Test
public void testTestAbout() throws Exception {
selenium.open(“/”);
selenium.click(“link=About”);
selenium.waitForPageToLoad(“30000”);
verifyTrue(selenium.isTextPresent(“Selenium, QTP, Java”));
selenium.click(“link=Home”);
selenium.waitForPageToLoad(“30000”);
selenium.click(“link=TestNG – Next Generation Testing Framework”);
selenium.waitForPageToLoad(“30000”);
verifyTrue(selenium.isTextPresent(“Need for a Testing Framework:”));
}

@AfterMethod
public void tearDown(){
selenium.stop();

}

}

Reply

narasimha March 6, 2012 at 1:46 pm

what is the diff b/w Ant and TestNG. Both will be useful to display reports only? or Is there any specific difference. Please update

Reply

seetaram March 24, 2012 at 9:34 am

ant is a build tool… testng is a test framework…

Reply

vinit March 7, 2012 at 6:48 pm

Hey Sitaram,
wonderful job man… Thanks for tremendous help

I have interview tomorrow can u help me how to call scripts in selenium and
> – How to execute scripts using various browsers.
I will never forget your help.

Thanks i advance

Reply

seetaram March 24, 2012 at 9:33 am

your question should be very specific.. for the second question.. you need to mention the browser name while creating the DefaultSelenium object… “*firefox”, “*iexplore”, “*googlechrome”, etc

Reply

satish March 30, 2012 at 9:30 am

pls ,any one can give vbscript to count number of objects on desktop

Reply

CJ May 25, 2012 at 9:49 am

Hi Seetaram,
I tried running your code and Rony’s code. Neither test would run for me. Every time I try to run the test, I get the message “Errors exist in required project(s). Proceed with launch?”
I tried creating a new project just for the TestNG tests but got the same message when I attempted to run the tests.
Thanks for any help you can provide.

Reply

seetaram May 25, 2012 at 3:06 pm

Can you give me the errors, let me look into them…

Reply

CJ May 25, 2012 at 10:10 am

Also, in the code I get the warning message, “The import org.testng cannot be resolved”. Perhaps Eclipse cannot find TestNG. Is there something special I need to do with the path?

Reply

seetaram May 25, 2012 at 3:06 pm

CJ,

This might be because the TestNG plugin is not installed on eclipse. Try to install the TestNG plugin, then this should get resolved.

–Seetaram

Reply

Chaithra June 14, 2012 at 10:48 am

Hi Seetaram,

I could not locate Date picker calendar control. Please help me to get logic for locating this.

Thanks
Chaithra.

Reply

Niroshan N July 16, 2012 at 2:55 pm

Hi Seetaram,

Many thanks for your your codes, its been really useful to me as I’m new to Selenium WebDriver your codes saved a hella of lot of time.I’m currently working on reporting testcases & test suits using TestNG on eclipse.

***it would be a great favour if you can help.****

Because at the moment for me TestNG produces high level reporting features for test cases ,methods & suites on test- output folder, similar to this http://testng.org/test-output/index.html.

I prefer a way to have screenshots with the reports, something similar to loggingSelenium helped me to achieve this in Selenium RC http://loggingselenium.sourceforge.net/samples/sampleResultSuccess.html

The reason I prefer this way is its easier, because this way I know exactly what selenium commands which were executed , I would have correct information what goes wrong, and when this occurs and the user input parameters belong to specific selenium commands

I don’t know if its possible in testNG, have you got any idea? is so would you be able to assist please? would be be able to shed some light on this problem for me please, the

Once again many thanks for your codes

Thanks
Shri

Reply

Anand October 12, 2012 at 10:55 am

Hi Seetaram Sir,
This is Anand here, I am new to selenium. I saw your blogs and I like the way you explained each and every step in simple way. After seeing your blogs I am able to run my code in eclipse. Thank you very much for your useful information.
Currently I am stuck in one issue, see if you can help me. I am getting below error
com.thoughtworks.selenium.SeleniumException: ERROR: Element user_name not found
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106)
at com.thoughtworks.selenium.DefaultSelenium.type(DefaultSelenium.java:317)
at CRMLoginTests.Login.testLogin(Login.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
My code is mentioned below:
package CRMLoginTests;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.thoughtworks.selenium.*;

public class Login extends SeleneseTestBase{
public Selenium selenium;

@BeforeMethod
public void setup()throws Exception{
selenium=new DefaultSelenium(“localhost”,4444,”*firefox”,”http://127.0.0.1:90/sugarcrm/index.php?action=Login&module=Users”);
selenium.start();
selenium.windowMaximize();
}
@Test
public void testLogin() throws Exception{
selenium.open(“/”);
selenium.type(“user_name”, “admin”);
selenium.type(“name=user_password”,”admin”);
selenium.click(“name=Login”);
selenium.waitForPageToLoad(“30000”);
selenium.click(“logout_link”);
}

@AfterMethod
public void tearDown() throws Exception{
selenium.stop();

}

}

Question:Is there any other method to identify objects using TestNG framework? (Same code works fine in Junit)
Please help me.

Thanks/Regards
Anand.Patil

Reply

Pri January 3, 2013 at 8:22 am

Thanks a lot..!
It worked for me..& m so happy for that.

Reply

Sunil Babu January 10, 2013 at 5:59 am

Hi this is Sunil,

Please can any one answer me that how to explain Selenium TestNG And JUnit Framework in front of a Interviewer

Thanks

Sunil

Reply

Objectivity January 25, 2013 at 7:46 pm

Hi there,

Eclipse is not generating the ‘test-output’ folder (after I run my TestNG code).

Kindly advise.

Many thanks.

Reply

harshal March 20, 2013 at 6:43 am

I have tried with above code, it gives me error: FAILED CONFIGURATION: @AfterMethod tearDown,
Pls do needful.

Reply

seetaram August 26, 2013 at 6:06 am

Need more info

Reply

Roxana May 22, 2013 at 3:25 pm

Hi Seetaram,

You blog is very helpful and I managed to set up my environment using the information gathered from this website.

I was wondering if there is any way to configure testng in order to always run all classes? I have a @AfterClass which fails and all java classes afterwards are skipped. I would like to turn off the skipping functionality of testng.

Thanks,
Roxana

Reply

Sebastian August 17, 2013 at 7:17 pm

I am running on Ubuntu 13.04 I ma getting this error:

java.lang.NoClassDefFoundError: com/google/common/base/Charsets
at com.thoughtworks.selenium.HttpCommandProcessor.getOutputStreamWriter(HttpCommandProcessor.java:149)
at com.thoughtworks.selenium.HttpCommandProcessor.getCommandResponseAsString(HttpCommandProcessor.java:176)
at com.thoughtworks.selenium.HttpCommandProcessor.executeCommandOnServlet(HttpCommandProcessor.java:118)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:101)
at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:275)
at com.thoughtworks.selenium.HttpCommandProcessor.start(HttpCommandProcessor.java:237)
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:100)
at com.selftechy.testng.TestNGDemo.setUp(TestNGDemo.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Charsets
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
… 34 more

Reply

seetaram August 20, 2013 at 9:19 am

Runtime not finding the class, make sure that dependent jars are available

Reply

seetaram August 21, 2013 at 8:53 am

Noclassdeffound exception is thrown when Java is not finding the class at runtime.. dependent jar might be missing

Reply

rajani August 22, 2013 at 6:03 am

Hi sreeram,

i have defined the driver start as different function and rest of the application navigaton as different function & driver quit as differnt function in a java program.Can i call these methods directly in testng class ?for exmple below is my Tesng test
public class EmailCreate {

SSClogin obj1;
@BeforeMethod
public void start()
{
obj1=new SSClogin();
obj1.startSSC();
obj1.login();

}
/* @Test
public void AddemailAccount()
{
obj1.EmailSetup();
}
*/
}

My main program is below

public class SSClogin {
public WebDriver driver;
public XpathRead obj;
public void startSSC(WebDriver driver)
{
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
driver.get(“http://xyz.motive.com/ssc”);
obj=new XpathRead();
System.out.println(“end of startSSC”);
}
public void login()
{
System.out.println(“start login”);
driver.findElement(By.id(obj.ReturnSSCXpath(“UserId”))).sendKeys(“9898989898”);
driver.findElement(By.id(obj.ReturnSSCXpath(“LoginPasswd”))).sendKeys(“password”);
driver.findElement(By.id(obj.ReturnSSCXpath(“Login”))).click();
}
}

But when i try to run it is i ma getting following result

[TestNG] Running:
C:\Documents and Settings\Admin\Local Settings\Temp\testng-eclipse–1960094770\testng-customsuite.xml

===============================================
Default test
Tests run: 0, Failures: 0, Skips: 0
===============================================

===============================================
Default suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@1546e25: 15 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@18a7efd: 0 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@efd552: 0 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@cdedfd: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@173831b: 32 ms

but i couldnt see as it is opening the application and calling those methods
please help me to find the solution ?

Reply

seetaram August 26, 2013 at 6:02 am

I am not seeing any issue with your code, can you check the browser compatibility? I mean your mozilla might be latest and the Webdriver is not supporting.

Reply

Khamer Taj September 16, 2013 at 6:54 pm

hello Seetaram…
This is Khamer,
1.can you Please share the challenges you have face while working with WebDriver,TestNG,Ant and CI(Jenkins) and the solutions for each and every challenge?
2.why TestNg better than Junit??
3.what are the roles and responsibilities of a selenium automation tester??

its very urgent…

Reply

seetaram September 17, 2013 at 7:19 am

Is it for interview ? …. 🙂

Definitely will do that….

Cheers!!
Seetaram

Reply

Khamer Taj September 18, 2013 at 2:39 pm

Hello Seetaram,
you have not yet answers for my questions..please send the answers soon…

Thanks in Advance..

Reply

seetaram September 20, 2013 at 3:57 am

Give me some more time as this needs a post

Reply

swathi September 22, 2015 at 12:42 pm

hi,
i have a doubt. what are the difference between @BeforeMethod, @BeforeClass, @BeforeSuite, @BeforeTest and @AfterTest,@AfterSuite,@AfterMethod,@AfterClass .
Can we use all the annotations at a time in a single Script.

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email