testng.xml : Creating Single Or Multiple Tests For Multiple Classes In WebDriver

As i have described in my previous post, We can configure our webdriver test or webdriver test suits for software testing project in testng.xml file. In my previous post, we have seen how to create testng.xml file to run single test class for software web application. Also If you don't know how to create and run first TestNG-WebDriver test, You can VIEW THIS POSTNow supposing you have two/multiple classes in your test suite for software web application then how will you run them? We can run both the classes in same test as well in 2 different tests too for software web application.

First of all, Create 3 classes under project = TestNGOne and package = TestNGOnePack as bellow.
  • "BaseClassOne" will be used for initializing and closing webdriver instance, 
  • "ClassOne" and "ClassTwo" will be used as test classes.
VIEW ALL WEBDRIVER-TESTNG TUTORIAL POSTS

1. BaseClassOne.java
package TestNGOnePack;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseClassOne {
    //Declared as public static to use same webdriver instance publicly
    public static WebDriver driver = null;

    //@BeforeSuite annotation describes this method has to run before all suites
    @BeforeSuite 
    public void setup() throws Exception { 
         System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  driver = new FirefoxDriver();
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.get("http://only-testing-blog.blogspot.com/2013/11/new-test.html"); 
    } 
    //@AfterSuite annotation describes this method has to run after execution of all suites
    @AfterSuite
         public void tearDown() throws Exception { 
         driver.quit(); 
    } 
}
Above class will be used as base class to initialize and close webdriver instance.

2. ClassOne.java
package TestNGOnePack;

import org.testng.annotations.Test;

public class ClassOne extends TestNGOnePack.BaseClassOne{
 
 //@Test annotation describes this method as a test method
 @Test
  public void testmethodone() {
    String title = driver.getTitle();
    System.out.print("\nCurrent page title is : "+title);
    String Workdir = System.getProperty("user.dir");
    String Classpackname = this.getClass().getName();
    System.out.print("\n'"+Workdir+" -> "+Classpackname+" -> testmethodone' has been executed successfully");
  }
}

Above ClassOne is inherited from BaseClassOne.

3. ClassTwo.java
package TestNGOnePack;

import org.testng.annotations.Test;

public class ClassTwo extends TestNGOnePack.BaseClassOne{
 
 //@Test annotation describes this method as a test method
 @Test
  public void testmethodone() {
  driver.navigate().to("http://only-testing-blog.blogspot.com/2014/01/textbox.html");
  String title = driver.getTitle();
  System.out.print("\nCurrent page title is : "+title);
  String Workdir = System.getProperty("user.dir");
  String Classpackname = this.getClass().getName();
  System.out.print("\n'"+Workdir+" -> "+Classpackname+" -> testmethodone' has been executed successfully");
  }
}

Above ClassTwo is also inherited from BaseClassOne.

Now your class structure will be looks like bellow in eclipse.



Configure testng.xml to run two classes in one test
Now let me show you how to configure testng.xml to run both classes in single test. Copy-paste bellow given lines in your testng.xml file and then Run it as TestNg Suite. Here, both classes are included in single test (Test One).
<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGOnePack.ClassOne" />
   <class name="TestNGOnePack.ClassTwo" /> 
  </classes>
 </test> 
</suite>

When execution completed, View execution result reports. It will looks like bellow.



Configure testng.xml to run two classes in two tests
Now if you wants to run both classes as separate test then you have to configure your testng.xml file as bellow.
<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGOnePack.ClassOne" />  
  </classes>
 </test> 
 <test name="Test Two" >
  <classes>
   <class name="TestNGOnePack.ClassTwo" /> 
  </classes>
 </test> 
</suite>


Here, "ClassOne" is included in 'Test One" test and "ClassTwo" is included in 'Test Two" test. Now run bellow given testng.xml file and verify result.


Now compare both the results. In 1st example, Both classes are executed under same test but in 2nd example both classes are executed under separate tests. This way you can configure testng.xml file as per your requirement.

17 comments:

  1. Hi ,
    When we run "TestNGOnePack.ClassOne" & "TestNGOnePack.ClassTwo" classes, two browsers will launch at the same time and performs the actions as instructed under @Test methods.
    Is it possible to run two classes with single browser means -->
    First, browser should launch and perform the actions as instructed in ClassOne and then close the browser, again launch browser and perform the action as instructed in ClassTwo and then close browser.
    Please suggest.

    ReplyDelete
    Replies
    1. If you have configured your test as instructed above then it will run in only single browser instance.. Do you have tried it?

      Delete
    2. Yes still facing same problem.

      Delete
  2. I am facing same problem as Nagesh.Any suggestions please.

    ReplyDelete
  3. No, it is running only in single browser...

    ReplyDelete
  4. No. it will run in a single browser. two browsers will NOT launch at the same time.

    ReplyDelete
  5. Facing same issue as Nagesh,can u plz help us

    ReplyDelete
  6. Hi,
    While running the classes I created as instructed above, I'm getting the below error.
    Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

    I'd set the Systemproperty and also downloaded the latest chromedriver application too. But still the error persists. Please help in debugging.

    ReplyDelete
  7. i am writing one test script in selenium. but in one page i got one flash id button. it will open file browser dialogue box . no sikui. if the sikuli is working in my system. it is not working
    in other system where the screen resolution is different

    ReplyDelete
  8. i tried same code in my project, after run the project first class executed successfully. But for in second class got null pointer exception error to driver.
    How to resolved this issue. Please help.
    Thanks in advance.

    ReplyDelete
    Replies
    1. Check your testng.xml if any tags are missing

      Delete
    2. I am facing same issue. If you have already resolved this issue then please help!
      Thanks in advance

      Delete
    3. I was also getting same issue and then came to know that driver's value becomes null in ClassTwo. May be you are facing same issue. Check if you have defined WebDriver as "public static" or just "public". Your line should look like this if you are using firefox driver:
      public static WebDriver driver = new FirefoxDriver();

      At least this fixed my issue.

      Delete
    4. making driver as public static fixed the above error.

      Delete
  9. Getting this error......


    org.testng.TestNGException:
    An error occurred while instantiating class TestNG.ClassOne: com/google/common/base/Function
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:385)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:285)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
    at org.testng.TestClass.getInstances(TestClass.java:104)
    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:90)
    at org.testng.TestClass.init(TestClass.java:82)
    at org.testng.TestClass.(TestClass.java:45)
    at org.testng.TestRunner.initMethods(TestRunner.java:422)
    at org.testng.TestRunner.init(TestRunner.java:252)
    at org.testng.TestRunner.init(TestRunner.java:222)
    at org.testng.TestRunner.(TestRunner.java:171)
    at org.testng.remote.support.RemoteTestNG6_9_10$1.newTestRunner(RemoteTestNG6_9_10.java:28)
    at org.testng.remote.support.RemoteTestNG6_9_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_9_10.java:61)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:604)
    at org.testng.SuiteRunner.init(SuiteRunner.java:170)
    at org.testng.SuiteRunner.(SuiteRunner.java:117)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1359)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1346)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
    at org.testng.TestNG.runSuites(TestNG.java:1124)
    at org.testng.TestNG.run(TestNG.java:1096)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
    Caused by: java.lang.NoClassDefFoundError: com/google/common/base/Function
    at TestNG.BaseClassOne.(BaseClassOne.java:14)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:373)
    ... 24 more
    Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 31 more

    ReplyDelete
  10. Anyone resolved above issue?

    ReplyDelete