Search code examples
javahp-quality-center

How to add a test step in HP QC from Java


I am trying to access and modify test cases in HP QC by JAVA. The code is running successfully but the Step, status, Exec dates are not being updated. Here is my code

for (Com4jObject obj : testInstances)  
{  
  ITSTest testInstance = obj.queryInterface(ITSTest.class);  
  ITSTest tstest = obj.queryInterface(ITSTest.class);

  IRunFactory runfactory = tstest.runFactory().queryInterface(IRunFactory.class);
  IRun run=runfactory.addItem("RunNew").queryInterface(IRun.class);
  Com4jObject step = run.stepFactory();
  // run.field("Step #", "Step1");
  run.status("Passed");
  // Com4jObject steps = run.stepFactory();
  // System.out.println(run.field("Actual Result"));
  // run.field("Actual Result", "As Expected. Please find attachment with TC001");
  if(tstest.name().contains("[1]TC001"))
  {
    try {
      String fileName = new File(files.get(i)).getName();
      String folderName = new File(files.get(i)).getParent();
      System.out.println("FILE: "+fileName);
      System.out.println("FOLDER: "+folderName);

      IAttachmentFactory attachfac = tstest.attachments().queryInterface(IAttachmentFactory.class);
      IAttachment attach = attachfac.addItem(fileName).queryInterface(IAttachment.class);
      IExtendedStorage extAttach = attach.attachmentStorage().queryInterface(IExtendedStorage.class);
      extAttach.clientPath(folderName);  
      extAttach.save(fileName, true);
      //attach.description(Actual);
      attach.post();
      attach.refresh();
    } catch(Exception e) {
      System.out.println("QC Exceptione : "+e.getMessage());
    }
  }

  run.post();
  //AppLog.info("  Test Instance: %s", testInstance.name());
  System.out.println(("Test Instance: %s"+ testInstance.name()));
} 

Solution

  • You need to call post() on each item separately: Test run and each created step. Example in C# starting from the point where you retrieve the step factory.

    // Create test run
    var oRunInstance = (QcClient.RunFactory)oTsTest.RunFactory;
    var oRun = (QcClient.Run)oRunInstance.AddItem("Performance Test");
    oRun.Status = "Passed";
    oRun.Post();
    oRun.Refresh();
    
    // Create test run steps
    var oTest = (QcClient.Test)oTsTest.Test;
    var tsDesignStepList = oTest.DesignStepFactory.NewList("");
    var oStepFactory = (QcClient.StepFactory)oRun.StepFactory;
    foreach (QcClient.DesignStep oDesignStep in tsDesignStepList)
    {
      var oStep = (QcClient.Step)oStepFactory.AddItem(oDesignStep.StepName);
      oStep.Status = "Passed";
      oStep.Post();
    }