TestNG – Test Automation with Selenium

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

Comments 29

  • 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();

    }

    }

  • 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

  • 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

    • 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

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

  • 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.

  • 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?

    • 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

  • Hi Seetaram,

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

    Thanks
    Chaithra.

  • 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

  • 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

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

  • 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

  • Hi there,

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

    Kindly advise.

    Many thanks.

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

  • 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

  • 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

  • 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 ?

  • 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…

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

    Thanks in Advance..

  • 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.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.