Friday, 27 December 2013

Moving from TestNG to Cucumber implementation

If you are having a well written automation framework written on TestNG framework and planning to move towards Cucumber implementation, you could do the same by pragmatically creating TestNG suites from cucumber.
Advantages: We could reuse the existing automation framework, Time and resource for creating a new framework will be saved.
Disadvantage: Reporting feature of the cucumber cannot be utilized (TestNG Reports will be used), Debugging could be difficult in this case.

Assumptions:
You already have a knowledge of TestNG and Cucumber.

Person.feature file looks like this.
##############################################
Scenario: Person A view his/her information
Given PersonA is logged in
When he click on my account information
Then PersonA account details are listed

Scenario: End of test suite
Given End of test suite
##############################################

Create a common class for creating testNG related objects.

TestNGObjects.java
public class TestNGObjects {
    public static int scenarioCount = 0;
    public static TestNG testng = new TestNG();
    public static XmlSuite tempSuite = new XmlSuite();
    public static List<XmlTest> listOfTests= new ArrayList<XmlTest>();
    public static List<XmlSuite> listOfSuites = new ArrayList<XmlSuite>();
    public static XmlTest tempTest= null;
}

Create a common class for creating a test suite related methods.

CukeCommon.java
public class CukeCommon {

//Below method is called at the end of test suite for test executing in testng
    @Given("^End of test suite$")
    public static void theEndOfTestSuite() {
        TestNGObjects.tempSuite.setTests(TestNGObjects.listOfTests);
        TestNGObjects.tempSuite.setAllowReturnValues(true);

        TestNGObjects.listOfSuites.add(TestNGObjects.tempSuite);
        TestNGObjects.testng.setXmlSuites(TestNGObjects.listOfSuites);
        Class<?> listenerClass = ClassHelper
                .forName("AssertionHandling.CustomTestListener");
        Object listenerObj = ClassHelper.newInstance(listenerClass);
        IInvokedMethodListener listener = (IInvokedMethodListener) listenerObj;
        //Listners of you have any
        listenerClass = ClassHelper.forName("Startup.InitializeSuiteName");
        listenerObj = ClassHelper.newInstance(listenerClass);
        ISuiteListener listener1 = (ISuiteListener) listenerObj;
        //Listners of you have any
        listenerClass = ClassHelper.forName("Startup.InitializeTestName");
        listenerObj = ClassHelper.newInstance(listenerClass);
        ITestListener listener2 = (ITestListener) listenerObj;
        //
        //
        TestNGObjects.testng.addListener(listener);
        TestNGObjects.testng.addListener(listener1);
        TestNGObjects.testng.addListener(listener2);
        TestNGObjects.testng.run();
    }

    public static void initializeTestObj(String testName, String testClass,
            String parameter) {
        try {
            System.out.println(parameter + "\n");
            testName = "Scenario" + TestNGObjects.scenarioCount + "_"
                    + testName;
            Map<String, String> testngParams = new HashMap<String, String>();
            testngParams.put("prams", parameter);
            TestNGObjects.tempTest= new XmlTest(TestNGObjects.tempSuite);
            TestNGObjects.tempTest.setName(testName);
            TestNGObjects.tempTest.setParameters(testngParams);

            List<XmlClass> myClasses = new ArrayList<XmlClass>();
            myClasses.add(new XmlClass(testClass));
            TestNGObjects.tempTest.setXmlClasses(myClasses);

            TestNGObjects.listOfTests.add(TestNGObjects.tempTest);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Implementation of scenario:

Person.java file.

public class Person {
 @Given("^(.+?) is logged in$")
public void person_a_is_logged_in(String person) {
//param is a the parameter for testng method
param = person;
//This method initialized the testng classes
CukeCommon.initializeTestObj("Customre login",
                "customer.Login", param);
}

@When("he click on my account information")
public void when_he_click() {
 //param is a the parameter for testng method
param = "AccountInfomation";
//This method initialized the testng classes
CukeCommon.initializeTestObj("Click on account info",
                "customer.SelectAcctInfo", param);
}
@Then("(.+?) account details are listed")
public void person_a_account_details(String person) {

//param is a the parameter for testng method
param = person;
//This method initialized the testng classes
CukeCommon.initializeTestObj("account info",
                "customer.VerifyAccountInfo", param);
}
}

here is how above example is executed:
When .feature file is executed
1)  Person class is called and all the testng objects are initialized.
2) when End of test suite line is executed, testng method is finalized with all the listeners and testng.run method is called.
3) TestNG execute all the tests and testng reports are generated.

No comments:

Post a Comment