Saturday, November 8, 2014

Integration of Selenium Webdriver with Test link

                                        

This Post will describe how to integrate Testlink- Selenium. i.e. executing the test cases and updating the status in the Testlink.

2 Different approaches which we can follow to achieve:
          1)  Using Eclipse-Selenium Web driver.
          2)  Using Jenkins-Selenium Web driver.

Before going to execute the test cases using Jenkins or Eclipse, first thing we need to do some Settings in the TestLink.
Edit config.inc.php file, go to line number 379, and add this line

$tlCfg->api->enabled = TRUE;

Test link:


1) API access Key-Test link :
This key will provide an interface between selenium web driver  test cases to Test link, to generate this key, go to Test link--> Setting--> API interface Section-- >Click on Generate Key.

2) Enable Test Automation(API Keys) :
     Go to Test link interface and Enable the check box for the Project which we have created for automation.

3) Create a Project in Test link.

4) Create test suite under project and write the Manual test cases and Select Execution type as Automated.

5) Create test plan, build and assign the test cases to the plan.

6) Create a Custom filed in Testlink (Java Class) add it to the project.




                                                 Create Test Plan under the Project.


                                  
                                  Create Custom Fields as “Java Class ” and assigned to the Test Plan.



                                               Add the automated test cases to the Test plan



After configuring all the above in the Testlink write the code into the eclipse using below code.

Eclipse-Selenium Web driver Test cases:

1)      Download Test link-api-client libs:

       Download “testlink-api-client.zip “file and add “testlink-api-client-2.0.jar”, “xmlrpc-common-3.1.jar”, “xmlrpc-client-3.1.jar”, and”ws-commons-util-1.0.2.jar” jar file in your web driver java project class path.  


2)       Write the Program in the eclipse for the test case and Click on execute it. After completion of execution, status will be update in the test link.

package com.sample;

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

public class seltestlinkinte {

    private WebDriver driver;  
    // Substitute your Dev Key Here
    public final String DEV_KEY = "5hyg785g4y6287hvg8cjjd6903dg85v7";
 
    // Substitute your Server URL Here
    public final String SERVER_URL = "http://localhost/testlink-1.9.10/lib/api/xmlrpc/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 Searchitem() throws Exception {
         String result = "";
         String exception = null;
         try {
              driver.navigate().to("http://url");
              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("shoes");
              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("shoes"));
    }


    @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);
    }
}


fig: 1

fig:2 (PASS)

fig:3(Fail)

Figure 1,2  & 3 are after executing the test cases




       
Configuration in Jenkins for Testlink automation

No comments:

Post a Comment