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
 * */

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

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

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

import frameworkComponents.Core;

public class ALMTesting extends Core{

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

        uploadFileToTestSetTest("http://win-2008server:8080/qcbin/", "DEFAULT", "Testing", "Baba", "baba", "Root\\Testing", "TestingForJACOBS", "Sample-SeleniumTest", "C:\\Users\\Baba\\Desktop\\SQL_SA_pwd.txt");
    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");, "InitConnectionEx", almURL);     , "login", almUserName,almEncryptedPassword);, "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 =, "NodeByPath", testFolder).toDispatch();
        //Dispatch subFolderNode = Dispatch.get(tsFolder, "NewList").toDispatch();
        Dispatch testSetNode = Dispatch.get(tsFolder, "TestSetFactory").toDispatch();
        Dispatch testSetList =, "NewList", "").toDispatch();
        //        Dispatch tsList =, "FindTestSets", testSetName).toDispatch();

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

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

                            Variant paramVal = new Variant();
                            Dispatch nowAttachment =, "AddItem", paramVal).toDispatch();
                            Dispatch.put(nowAttachment, "Description", Calendar.getInstance().getTime());
                            Dispatch.put(nowAttachment, "Filename", uploadFilePath);
                            Dispatch.put(nowAttachment, "Type", 1);
                  , "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 =, "AddItem", "Automated").toDispatch();
                            Dispatch runAA =, "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 =, "Item", new Variant(run)).toDispatch();
                                Variant runName = Dispatch.get(runItem, "Name");
                                // Create Stepfactory for this Run
                                Dispatch oStep = Dispatch.get(runItem, "StepFactory").toDispatch();
                                //Create a step
                      , "AddItem", "Test case result");
                                Dispatch oStepDetails =, "NewList", "").toDispatch();
                                Dispatch.put(runItem, "Status", "Passed");
                      , "Post");
                                // Create Stepfactory for this Run
//                                Dispatch oStep = Dispatch.get(runA, "StepFactory").toDispatch();
//                                Dispatch oStepDetails =, "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 =,"Item",new Variant(stepIndex)).toDispatch();
                                    Variant stepActName = Dispatch.get(stepItem, "Name");
                                    System.out.println("Step Name is : "+stepActName);
                                        Dispatch.put(stepItem, "Status", "Passed");
                              , "Post");
                                    }catch(ComFailException i){
                                        System.out.println("COM Fail Exception occured, Exited without updates in ALM Status or FileName upload or Description or Type");
                              , "Disconnect");
                              , "Logout");
                              , "ReleaseConnection");
                        }catch(ComFailException i){
                            System.out.println("COM Fail Exception occured, Exited without updates in ALM Status or FileName upload or Description or Type");
                  , "Disconnect");
                  , "Logout");
                  , "ReleaseConnection");
                if(blnFlag) break;
        }, "Disconnect");, "Logout");, "ReleaseConnection");
        System.out.println("updates are done");

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

No comments: