Saturday, August 20, 2016

Update TestSet Test status and upload file in ALM 11 OTA code using JACOBS (using Java)

Hi All,

Using VB, C#, VBScript to access COM is very straight forward and we can easily develop scripts/code based on Application API, you can get intellisense in IDEs, Excel Macros, etc., So, all methods are visible and documentation would be easily to find on each of them.

But, When we got situation to work on any COM using Java, it's not so easy as with VB/C#. we've multiple ways to deal with COM with Java, e.g. COM4j, JACOBS, etc., each jar has their own methods and way to access COM.

Mainly, now a days Web Automation is looking forward to Selenium - Java, But most of the projects would be running on ALM. Expectations from managements as to use Selenium, But integration with ALM for Test management.

I would like to show you guys how we can Access ALM - Testset(TestLab) Test and update test results, Attach any file in Test attachment with description.

Note: you can tweak this as per your need. 

You must have Jacob dlls (32 and 64 bit) and place as shown below.


 /**
 * PreCondition to ALM Update code - Initialize Jacobs
 * */
import com.jacob.com.LibraryLoader;

public class Core extends AppVariables{
    public void initializeJacobs(){
        String jacobDllVersionToUse;
        if (jvmBitVersion().contains("32")){
            jacobDllVersionToUse = "jacob-1.18-x86.dll";
        }
        else {
            jacobDllVersionToUse = "jacob-1.18-x64.dll";
        }
        File file = new File("Lib", jacobDllVersionToUse);
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
    }
    public String jvmBitVersion(){
        return System.getProperty("sun.arch.data.model");
    }
}


/**
 * Actual Class which has Code implementation
 * */
import java.text.DateFormat;
import java.util.Calendar;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
//import com.sun.jna.platform.win32.COM.TypeInfoUtil.Invoke;

import frameworkComponents.Core;

public class ALMTesting extends Core{

    /**
     * @param args
     */
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Core cfw = new Core();
        cfw.initializeJacobs();

        uploadFileToTestSetTest("http://win-2008server:8080/qcbin/", "DEFAULT", "Testing", "Baba", "baba", "Root\\Testing", "TestingForJACOBS", "Sample-SeleniumTest", "C:\\Users\\Baba\\Desktop\\SQL_SA_pwd.txt");
    }
  
    @SuppressWarnings("deprecation")
    static void uploadFileToTestSetTest(String almURL, String domain, String project, String almUserName, String almEncryptedPassword, String testFolder, String testSetName, String testName, String uploadFilePath){
        ActiveXComponent almConnection=new ActiveXComponent("TDAPIOLE80.TDConnection");
        Dispatch.call(almConnection, "InitConnectionEx", almURL);      
        Dispatch.call(almConnection, "login", almUserName,almEncryptedPassword);
        Dispatch.call(almConnection, "connect",domain,project);

        /**By me on Test set factory*/
        //        Dispatch testLab = Dispatch.get(almConnection, "TestSetFactory").toDispatch();
        //        Dispatch testSetFilter = Dispatch.get(testLab, "Filter").toDispatch();

//        String testFolder = testFolder;
//        String testSetName = testSetName;
//        String testName = testName;
//        String uploadFilePath = uploadFilePath;

        Dispatch testSetTreeManager = Dispatch.get(almConnection, "TestSetTreeManager").toDispatch();
        Dispatch tsFolder = Dispatch.call(testSetTreeManager, "NodeByPath", testFolder).toDispatch();
        //Dispatch subFolderNode = Dispatch.get(tsFolder, "NewList").toDispatch();
        Dispatch testSetNode = Dispatch.get(tsFolder, "TestSetFactory").toDispatch();
        Dispatch testSetList = Dispatch.call(testSetNode, "NewList", "").toDispatch();
        //        Dispatch tsList = Dispatch.call(tsFolder, "FindTestSets", testSetName).toDispatch();

        System.out.println("till here good");
        Variant count = Dispatch.get(testSetList, "Count");
        System.out.println(count);
        int toInt = count.toInt();
        //        String[] testNames = new String[toInt];
        boolean blnFlag = false;
        for (int testSetIndex = 1; testSetIndex <= toInt; testSetIndex++ ) {
            Dispatch testSetItem = Dispatch.call(testSetList, "Item", new Variant(testSetIndex)).toDispatch();
            Variant testSetActName = Dispatch.get(testSetItem, "Name");
            System.out.println(testSetActName);
            if (testSetActName.toString().equalsIgnoreCase(testSetName)){
                Dispatch testSetTestFactory = Dispatch.get(testSetItem, "TSTestFactory").toDispatch();
                Dispatch testList = Dispatch.call(testSetTestFactory, "NewList", "").toDispatch();
                Variant testsCount = Dispatch.get(testList, "Count");
                System.out.println(testsCount);
                int testsCountInt = testsCount.toInt();
                for(int testIndex=1; testIndex<=testsCountInt; testIndex++){
                    Dispatch testItem = Dispatch.call(testList, "Item", new Variant(testIndex)).toDispatch();
                    Variant testActName = Dispatch.get(testItem, "TestName");
                    System.out.println(testActName);
                    if(testActName.toString().equalsIgnoreCase(testName)){
                      
                        try{
                            Dispatch nowTest = testItem;
                            Dispatch attachmentFactory = Dispatch.get(testItem, "Attachments").toDispatch();
                            //                          Dispatch attachList = Dispatch.call(attachmentFactory, "NewList", "").toDispatch();

//                            Dispatch.put(nowTest, "Status", "Passed");
//                            Dispatch.call(nowTest, "Post");

                            //System.DBNull.Value
                            Variant paramVal = new Variant();
                            paramVal.putNull();
                            Dispatch nowAttachment = Dispatch.call(attachmentFactory, "AddItem", paramVal).toDispatch();
                            Dispatch.put(nowAttachment, "Description", Calendar.getInstance().getTime());
                            Dispatch.put(nowAttachment, "Filename", uploadFilePath);
                            Dispatch.put(nowAttachment, "Type", 1);
                            Dispatch.call(nowAttachment, "Post");
                          
                            /** Updating steps in this test*/
                            // Create run factory for this test
                            Dispatch runF = Dispatch.get(testItem, "RunFactory").toDispatch();
                            // Add a name to this runfactory test
                            Dispatch runA = Dispatch.call(runF, "AddItem", "Automated").toDispatch();
                            Dispatch runAA = Dispatch.call(runF, "NewList","").toDispatch();
                          
                            Variant runCount = Dispatch.get(runAA, "Count");
                            System.out.println("Run Count : "+ runCount);
                            int runsCountInt = runCount.toInt();
                          
                            for(int run=1; run<=runsCountInt; run++){
                                Dispatch runItem = Dispatch.call(runAA, "Item", new Variant(run)).toDispatch();
                                Variant runName = Dispatch.get(runItem, "Name");
                                System.out.println(runName);
                              
                                // Create Stepfactory for this Run
                                Dispatch oStep = Dispatch.get(runItem, "StepFactory").toDispatch();
                                //Create a step
                                Dispatch.call(oStep, "AddItem", "Test case result");
                                Dispatch oStepDetails = Dispatch.call(oStep, "NewList", "").toDispatch();
                              
                                Dispatch.put(runItem, "Status", "Passed");
                                Dispatch.call(runItem, "Post");
                                // Create Stepfactory for this Run
//                                Dispatch oStep = Dispatch.get(runA, "StepFactory").toDispatch();
                              
//                                Dispatch oStepDetails = Dispatch.call(oStep, "NewList", "").toDispatch();
                              
                                // Update all steps to pass
                                Variant stepsCount=Dispatch.get(oStepDetails, "Count");
                                System.out.println("No of Step is - "+stepsCount);
                                int stepsCountInt = stepsCount.toInt();
                                for(int stepIndex=1; stepIndex<=stepsCountInt; stepIndex++){
                                    Dispatch stepItem = Dispatch.call(oStepDetails,"Item",new Variant(stepIndex)).toDispatch();
                                    Variant stepActName = Dispatch.get(stepItem, "Name");
                                    System.out.println("Step Name is : "+stepActName);
                                    try{
                                        Dispatch.put(stepItem, "Status", "Passed");
                                        Dispatch.call(stepItem, "Post");
                                    }catch(ComFailException i){
                                        System.out.println("COM Fail Exception occured, Exited without updates in ALM Status or FileName upload or Description or Type");
                                        Dispatch.call(almConnection, "Disconnect");
                                        Dispatch.call(almConnection, "Logout");
                                        Dispatch.call(almConnection, "ReleaseConnection");
                                        System.exit(-1);
                                    }
                                }
                                break;
                            }
                          
                          
                            blnFlag=true;
                            break;
                        }catch(ComFailException i){
                            System.out.println("COM Fail Exception occured, Exited without updates in ALM Status or FileName upload or Description or Type");
                            Dispatch.call(almConnection, "Disconnect");
                            Dispatch.call(almConnection, "Logout");
                            Dispatch.call(almConnection, "ReleaseConnection");
                            System.exit(-1);
                        }
                    }
                }
                if(blnFlag) break;
            }
        }

        Dispatch.call(almConnection, "Disconnect");
        Dispatch.call(almConnection, "Logout");
        Dispatch.call(almConnection, "ReleaseConnection");
        System.out.println("updates are done");
    }
}

"A good threat is worth a thousand tests" - Boris Beizer

No comments:

There was an error in this gadget