Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Saturday, September 3, 2016

How to get Test Case Detail from testlink using TestLink Java API?

Getting test case detail from testlink using API is quite needful when displaying results in Reports.

To get test case related detail you will need testlink-java-api.

Prerequisite:


  1. TestLink-Java-API
  2. Java 1.7 or higher
  3. Maven

Since, TestLink-Java-API depends on mulitple other jars, it ia adviceable to create a Maven project and add the below dependencies in POM.XML


<dependencies>
  <!-- Commons APIs -->
  <dependency>
   <groupId>commons-lang</groupId>
   <artifactId>commons-lang</artifactId>
   <version>2.6</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.4</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <!-- Logging APIs -->
  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-simple</artifactId>
   <version>1.7.21</version>
  </dependency>
  <!-- Configuration APIs -->
  <dependency>
   <groupId>commons-configuration</groupId>
   <artifactId>commons-configuration</artifactId>
   <version>1.10</version>
  </dependency>
  <!-- XML-RPC related APIs -->
  <dependency>
   <groupId>org.apache.xmlrpc</groupId>
   <artifactId>xmlrpc-common</artifactId>
   <version>3.1.3</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>xml-apis</groupId>
   <artifactId>xml-apis</artifactId>
   <version>2.0.2</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>commons-httpclient</groupId>
   <artifactId>commons-httpclient</artifactId>
   <version>3.1</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>br.eti.kinoshita</groupId>
   <artifactId>testlink-java-api</artifactId>
   <version>1.9.14-0</version>
  </dependency>
<dependencies>


Use the below code to retrieve Test case description, Steps to execute, Expected results etc. using External test case id.


public void getTestDetailFromTestLink() {
  String testCaseDescription = "", expectedResults = "", steps = "";
  URL url;
  try {
   url = new URL("http://localhost/testlink/lib/api/xmlrpc/v1/xmlrpc.php");

   String devKey = "5062c3fertf9b8886e0edd0ffdf6b14";
   TestLinkAPI api = new TestLinkAPI(url, devKey);
   TestCase tCase = api.getTestCaseByExternalId("P1-46039", 1);

   testCaseDescription = tCase.getSummary();

   List<TestCaseStep> list = tCase.getSteps();
   Iterator<TestCaseStep> itr = list.iterator();
   while (itr.hasNext()) {
    TestCaseStep tcs = itr.next();
    expectedResults = tcs.getExpectedResults();
    steps = tcs.getActions();

   }
  } catch (Exception e) {
   e.printStackTrace();
  }

  System.out.println("Description :"+testCaseDescription);
  System.out.println("Expected Result :"expectedResults);
  System.out.println("Steps to Execute :"+steps);
  
 }


Above code produces the below output.

Description :Verify that on selecting Die Hard Techy from Side menu takes user to blog screen.

Expected Result :User should be navigated to Screen.

Steps to Execute :1.Open DieHardTechy website. 2.Select Die Hard Techy.