Tuesday, February 6, 2018


TestLink Setup for Automation:
1. First you need to enable Automation (API keys) of your test link project.
Open your testlink project, click on check box of “Enable Test Automation (API keys)” and click on save button.

2. Generate Key: This key provide interface between webdriver test scripts and testlink. You must generate this  key. To generate this click on >> My Settings  menu and click on “Generate a new key” button under “API interface” section.


3. Create test link project.
4. Create test link plan and add test plan to created test link project.
5. Create test suite and test cases under created test link project.

Scripts Creation:
1. Download “testlink-api-client.zip “ file and add “testlink-api-client-2.0.jar” , “xmlrpc-common-3.1.jar”, “xmlrpc-client-3.1.jar”,” ws-commons-util-1.0.2.jar” jar file in your webdriver java project class path.
2. Below I have created a sample code for integration of test link with webdriver java script.
package com.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import testlink.api.java.client.TestLinkAPIClient;
import testlink.api.java.client.TestLinkAPIException;
import testlink.api.java.client.TestLinkAPIResults;

public class SeleniumTestLinkIntegration {

    private WebDriver driver;
   
    // Substitute your Dev Key Here
    public final String DEV_KEY = "4dec862f7e8045afc9cffd8630fe45c4";

    // Substitute your Server URL Here
    public final String SERVER_URL = "http://localhost/testlink-1.9.7/lib/api/xmlrpc/v1/xmlrpc.php";

   // Substitute your project name Here
    public final String PROJECT_NAME = "SampleProject";

    // Substitute your test plan Here
    public final String PLAN_NAME = "SampleTestPlan";

    // Substitute your build name
    public final String BUILD_NAME = "SampleBuild";

    @BeforeSuite
    public void setUp() throws Exception {
         driver = new FirefoxDriver();              
    }

    @Test
    public void testSearchCountry() throws Exception {
         String result = "";
         String exception = null;
         try {
              driver.navigate().to("http://www.wikipedia.org/wiki/Main_Page");
              result = TestLinkAPIResults.TEST_PASSED;
              updateTestLinkResult("TC001-1", null, result);
         } catch (Exception ex) {
              result = TestLinkAPIResults.TEST_FAILED;
              exception = ex.getMessage();
              updateTestLinkResult("TC001-1", exception, result);
         }
         try {
              driver.findElement(By.id("searchInput")).clear();
              driver.findElement(By.id("searchInput")).sendKeys("India");
              result = TestLinkAPIResults.TEST_PASSED;
              updateTestLinkResult("TC001-2", null, result);
         } catch (Exception ex) {
              result = TestLinkAPIResults.TEST_FAILED;
              exception = ex.getMessage();
              updateTestLinkResult("TC001-2", exception, result);
         }
         try {
             driver.findElement(By.id("searchButton")).click();
             result = TestLinkAPIResults.TEST_PASSED;
             exception = null;
             updateTestLinkResult("TC001-3", null, result);
         }
         catch (Exception ex) {
             result = TestLinkAPIResults.TEST_FAILED;
             exception = ex.getMessage();
             updateTestLinkResult("TC001-3", exception, result);
         }
         String str = driver.findElement(
         By.xpath("//h1[@id='firstHeading']/span")).getText();                               
         Assert.assertTrue(str.contains("India"));
    }


    @AfterSuite
    public void tearDown() throws Exception {
         driver.quit();                                                      
    }
   
    public void updateTestLinkResult(String testCase, String exception, String result)    throws TestLinkAPIException {
         TestLinkAPIClient testlinkAPIClient = new TestLinkAPIClient(DEV_KEY,
                                SERVER_URL);
         testlinkAPIClient.reportTestCaseResult(PROJECT_NAME, PLAN_NAME,
                                testCase, BUILD_NAME, exception, result);
    }
}