Tuesday 29 October 2013

Running selenium tests with TestNg (Unit testing tool)

While exploring different selenium forums , I have observed that ,Many test engineers asks the question “Is it mandatory to use any unit testing tool (Java : testNg,Junit or dotNet: Nunit Or python :PyUnit) with selenium for running the tests
If someone will ask this to me I will answer it as a ‘NO’ ,we can run our selenium tests in a suite without using TestNg/Junit/Nunit or any other unit testing tool.

How can we run selenium tests without using TestNg,Junit or any Unit testing tool.
Simply write a client class with main method as follow and give the call to methods that you want to execute in a suite.
public class SeleniumClient {
      public static void main(String[] args) {
            SeleniumTest testClient = new SeleniumTest();
            testClient.LoginToApplication("loginName", "loginPassword");
            boolean isSuccessfullyLoggedIn = testClient.VerifySuccessfulLogin();
      }
}

What is a drawback of doing this ?
Testwriter has to explicitly call his methods to get it executed in a suite which is a big overhead for him.

What is a best way to achieve this i.e executing selenium tests in a suite using any unit testing tool ?
First You can configure testNg plugin to eclipse IDE as per the documentation given on testNg SIte
For the Eclipse plug-in, we suggest using the update site:

  1. Select Help / Software updates / Find and Install.
  2. Search for new features to install.
  3. New remote site.
  4. For Eclipse 3.4 and above, enter http://beust.com/eclipse.
  5. Make sure the check box next to URL is checked and click Next.
  6. Eclipse will then guide you through the process.
Once you have configured testNg  with your eclipse there are two ways to run the tests in a suite :
You can either write testNg class as per the documentation given here 
Or You can write a testNg.xml as per your need to run the tests in a suite, mentioned here
How to use TestNg features while integrating it with selenium.

I observed many automation teams are integrating selenium with testNG.While doing this exercise of integrating Selenium with lot of questions comes in mind of automation engineer,here I am trying to answer few of the use cases that I come across while using selenium with testNg.

How to classify selenium tests in different groups using testng ?
Requirement 1 : If I have a requirement to categorize all my automated tests in 3 different groups
                “Smoke Test” ,”Critical Test” and “Regression Test” how can I Differentiate this tests in different group.

@Test(groups = { "Smoke Test" })
      void testMethod1() {
            >>>>>Method definition>>>>>>>>
      }

      @Test(groups = { "Critical Test" })
      void testMethod2(String... b) {
            >>>>>Method definition>>>>>>>>
      }

      @Test(groups = { "Regression Test" })
      void testMethod3(int a, int b) {
            >>>>>Method definition>>>>>>>>
      }
Now I have to execute only Smoke test, how to handle it with testNg.xml
                                <groups>
                  <run>
                        <include name="Smoke Test"/>
                  </run>
            </groups>
              
Requirement 2 :  I have 100 tests automated out of that I have to exclude the tests which is having a product bug.
@Test(groups = { "Product Test", "Smoke Test" })
      void testMethod1() {
            >>>>>Method definition>>>>>>>>
      }

      @Test(groups = { "Product Test", "Critical Test" })
      void testMethod2() {
            >>>>>Method definition>>>>>>>>
      }

      @Test(groups = { "Product Test", "Regression Test", "Bug" })
      void testMethod3() {
            >>>>>Method definition>>>>>>>>

      }
I don’t want to execute the tests which are already having a product bug, remaining all tests needs to be executed.
                                <groups>
                  <run>
                        <include name="Product Test"/>
                        <exclude name="Bug"/>
                  </run>
            </groups>
     

What is a SoftDependancy and HardDependancy in TestNg ?
                Most of the time we need to deal with 2 type of dependencies ,
1.       Hard Dependency:  If there are two methods and method2 needs to be executed only if method1 is passing then this is a Hard Dependency.

@Test(groups = { "Product Test" })
      void startUp() {
            System.out
                        .println("this method will be responsible for all the prerequisites ");
      }

      @Test(groups = { "Product Test" }, dependsOnMethods = { "startUp" })
      void hardDependantTest() {
            System.out.println("Perform actual test execution code.");

      }
                                Here hardDependantTest() will get executed only if startup() gets passed.

2.       Soft Dependency:  If we bother only with the sequence of the test, means method2 needs to be executed only after method1, irrespective of its execution status pass or fail ( mehtod2 needs to be executed even after method1 fails)
@Test(groups = { "Product Test" })
      void testMethod1() {
            System.out.println("Perform actual test execution code.");
      }

      @Test(groups = { "Product Test" }, dependsOnMethods = { "testMethod1" }, alwaysRun = true)
      void testMethod2() {
            System.out.println("Perform actual test execution code.");

      }
More complex part related to selenium and testNg will get covered in next blog !!

No comments:

Post a Comment