Wednesday, 26 March 2014

TestLink API integration with Selenium (WebDriver) script


Summary: By the end of this article you will be able to integrate WebDriver with TestLink. This integration will update the results in TestLink.This article will cover the following:
  •         TestLink: Download necessary JAR files
  •          TestLink: How to enable Test Automation API’s
  •          TestLink: Generate Personal API access key
  •          WebDriver: Creating and implementing script
  •      Testlink reports before and after executing the script

TestLink:

Pre-requisites: You should have TestLink setup up and running with a PROJECT that should have a TEST PLAN, BUILD/RELEASE and some TEST CASES assigned to it.

Note: We will use Project Name, Test Plan Name, Build /Release Name and Test cases ID later while writing selenium script.

Download JAR files: Download testlink-api-client-2.0.zip from Google Code and add the following jar files in your WebDriver java project class path.
‘testlink-api-client-2.0.jar’, ‘xmlrpc-common-3.1.jar’, ’xmlrpc-client-3.1.jar’, ‘ws-commons-util-1.0.2.jar’

How to enable Test Automation (API keys):
  •          Log In to TestLink using admin user
  •          Navigate to Desktop > under ‘Test Project’ > select ‘Test Project Management’
  •          Open appropriate Project > click on check box “Enable Test Automation (API keys)” > click on ‘Save’ button.

Generate Personal API access key:
  •          Click on link ‘My Settings’ > opens Account Settings
  •          API Interface section: Click on ‘Generate a new key’ button > you will get an access key.


      Creating and Implementing Script: Use the below mentioned code but make sure that you make the necessary changes to the file:

Example: Just change Test case ID's as per your test cases.
package com.selenium.integration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import testlink.api.java.client.TestLinkAPIResults;
public class Example {
private WebDriver driver;
@BeforeSuite
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
@Test(priority = 1)
public void accessApplicationURL() throws Exception {
try {
driver.get("https://mail.google.com");
TestLinkUtils.reportResult("TC-1", null, TestLinkAPIResults.TEST_PASSED);
} catch (Exception e) {
TestLinkUtils.reportResult("TC-1", e.getMessage(), TestLinkAPIResults.TEST_FAILED);
//Below line will fail Selenium test case as well, comment it if you don't want it to fail
throw new Exception(e);
}
}
@Test(priority = 2)
public void enterLoginDetails() throws Exception {
try {
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("your username or email");
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Passwd")).sendKeys("your password");
TestLinkUtils.reportResult("TC-2", null, TestLinkAPIResults.TEST_PASSED);
} catch (Exception e) {
TestLinkUtils.reportResult("TC-2", e.getMessage(), TestLinkAPIResults.TEST_FAILED);
//Below line will fail Selenium test case as well, comment it if you don't want it to fail
throw new Exception(e);
}
}
}
view raw Example.java hosted with ❤ by GitHub

TestLinkUtils: replace TestLink Server URL, Access key, Project Name, Test Plan Name, Build Name as per your TestLink setup.
package com.selenium.integration;
import testlink.api.java.client.TestLinkAPIClient;
import testlink.api.java.client.TestLinkAPIException;
public class TestLinkUtils {
// Replace Testlink API Interface access key here
public final static String ACCESS_KEY = "xxxxxxxxxx";
// Replace your TestLink Server URL here
public final static String TESTLINK_SERVER_URL = "http://xxxx/lib/api/xmlrpc/v1/xmlrpc.php";
// Replace Project Name here
public final static String TESTLINK_PROJECT_NAME = "Test Project";
// Replace your Test Plan here
public final static String TESTLINK_TESTPLAN_NAME = "Test Plan";
// Replace your Build Name here
public final static String BUILD_RELEASE_NAME = "Test build";
public static void reportResult(String testCaseID, String executionNotes, String result) throws TestLinkAPIException {
TestLinkAPIClient apiClient = new TestLinkAPIClient(ACCESS_KEY,TESTLINK_SERVER_URL);
apiClient.reportTestCaseResult(TESTLINK_PROJECT_NAME, TESTLINK_TESTPLAN_NAME,testCaseID, BUILD_RELEASE_NAME, executionNotes, result);
}
}

TestLink Report before and after executing the script

Before:

After:



9 comments:

  1. Nice post and it's helpful also......

    ReplyDelete
  2. Nice stuff.. helped me a lot in my Imation project.

    ReplyDelete
  3. Nicely explained step by step. Very useful for fresher like me.
    Thanks for post. :)

    ReplyDelete
    Replies
    1. Ajeet,

      Yes, it works. Please provide your exact steps and error if it is not working for you.

      Thanks.
      -

      Delete
  4. Are you integrate your test with jenkins?

    ReplyDelete
  5. Hi Shivraj,

    Great! But when I run the above script I get following error stacktrace. Any idea?


    testlink.api.java.client.TestLinkAPIException: The call to the xml-rpc client failed.
    at testlink.api.java.client.TestLinkAPIClient.executeXmlRpcMethod(TestLinkAPIClient.java:1266)
    at testlink.api.java.client.TestLinkAPIClient.execXmlRpcMethodWithCache(TestLinkAPIClient.java:1195)
    at testlink.api.java.client.TestLinkAPIClient.getProjects(TestLinkAPIClient.java:726)
    at testlink.api.java.client.TestLinkAPIHelper.getProjectInfo(TestLinkAPIHelper.java:64)
    at testlink.api.java.client.TestLinkAPIHelper.getProjectID(TestLinkAPIHelper.java:48)
    at testlink.api.java.client.TestLinkAPIClient.reportTestCaseResult(TestLinkAPIClient.java:184)
    at com.visualiq.testlink.Seltestlinkinte.updateTestLinkResult(Seltestlinkinte.java:80)
    at com.visualiq.testlink.Seltestlinkinte.Searchitem(Seltestlinkinte.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1137)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:753)
    at org.testng.TestRunner.run(TestRunner.java:607)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
    at org.testng.SuiteRunner.run(SuiteRunner.java:270)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
    at org.testng.TestNG.runSuites(TestNG.java:1124)
    at org.testng.TestNG.run(TestNG.java:1096)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
    Caused by: org.apache.xmlrpc.XmlRpcException: Failed to read servers response: Connection refused: connect
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:150)
    at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:94)
    at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:44)
    at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:53)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:166)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:157)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:146)
    at testlink.api.java.client.TestLinkAPIClient.executeXmlRpcMethod(TestLinkAPIClient.java:1232)
    ... 32 more

    I would appreciate your help

    ReplyDelete
  6. How do I run automation script from TestLink? Above code shows updating result in TestLink when script is run using Selenium.

    Amar

    ReplyDelete
  7. Hello,

    Can anyone know how to call testlink URL with HTTPS ?

    ReplyDelete