Monday, 5 March 2012

Automating SOAP API tests with SOAPUI


SOAP UI is a powerful and easy to use open source tool for automating different types of web services like SOAP, REST etc. It provides a UI for creating test cases, assertions making it easier to develop test cases rapidly.
This article covers basic steps and information on how to start with SOAP UI, create basic test cases and assertions and execute the test cases.
Install SOAP UI 
Install the latest free version of SOAP UI from http://www.soapui.org/
Create a project
  1. Open SOAP UI.
  2. Click on file->new SOAP UI project














  1. Click on ok button.
  2. Add WSDL to the Project. Right click on Project (WebservicesTesting) ->Add WSDL.
  3. Provide the URL to the WSDL in the Add WSDL window and click OK. A service binding for the WSDL will be generated with all operations.





















When the WSDL for a services changes, it can be updated without affecting the test cases by right clicking on the service binding and choosing update definition.


















Generating test suite/test case
  1. Right click on the service binding and select “Generate Test Suite”. Select the operations for which sample test case template has to be generated and click on ok button. A test suite will be generated for the selected operations.


























  1. Right click on the test suite to generate a new test case
Adding steps in a test case
When a new test case is added, right click on the test case and select add step -> test request. Select the operation for which the request needs to be sent.
Once test case is added, different steps to the test case can be added. These steps form the blocks of the test case which are executed in order.
How to pass parameters to tags
Values for the tags in the SOAP request can be hard coded or parameterized. There are two ways of doing passing parameters/values 1) Creating properties to transfer values 2) Data driven by external files
1) Creating properties to transfer values: Properties can be used to store data. These can act as variables which can be passed to the SOAP requests.
1.1) Properties at a project: Properties can be defined at project and these are available for all the test suites and all test cases.
a) Double click on project and click on properties tab at the bottom to create properties.



b) Enter Property name in Add Property and Click on Ok button.










c) Enter Value to created property name at the bottom.
















d) Pass created property value to the test suites/test cases in the Project.
e) Open the SOAP Request of Test Case (countryInfoByIana) ->Right click on ianacode value->Get Data->Project: [WebserviceTesting]->Property:[cntry]




















f) Ianacode value is replaced by ${#Project#cntry} automatically.








g) Run the Script.
1.2) Properties at the test suite level: Properties which are defined at the test suite level are available for all the test cases and test steps inside the suite. Double click on test suite and click on properties tab at the bottom to create properties.
Follow the same steps as in step 1.1.
1.3) Properties at the test case: Properties at the test case are available for only that test case and all the test steps inside the test case.
a) Right click on test case and select add step -> properties to create properties for a test case.























b) Declaring properties using properties test step.





c) Transfer the property value to the Test Case. Right Click on ianacode value->Get Data->Property name (Ex: country)->Property(Ex:countryvalue)

















d) Ianacode value change to ${country#countryvalue}








2) Data driven by external files: Is available only in the commercial version
Creating assertions
Assertions are at the heart of any test case where the actual testing happens
SOAP UI provides different types of assertions. Double click on a test request inside a test case. You will find an assertions tab at the bottom and different kinds of assertions can be added.
XPath Match:
Step1: Select XPath Match from Select Assertion window.

















Step2: Click on ok button.
Step3: Enter the Xpath Expression of the test case Response in Declare box.
Step4: Click on Select from current.













Step5: Click on Test to verify whether we are giving valid Xpath Expression or not.










Running the test cases
1) Running from UI:
Double click on a project or test suite or test case and click on run button to execute tests at that level. Passed test steps in each of the test suite are shown in green and failed ones in red.










2) Running from command prompt:
SOAP UI test cases can also be run from command prompt by using the following command
com.eviware.soapui.tools.SoapUITestCaseRunner (options)
An easier way is to run the tests is using the test runner batch file which comes along with the SOAP UI installation at /bin
testrunner.bat -sWebserviceTestSuite -j -f C:\SOAPUIProjects\Reports C:\SOAPUIProjects\WebservicesTesting-soapui-project.
-s is for the test suite
-j indicates that junit style HTML report will be generated
-f is for specifying the folder for report generation
Mentioning the location of project XML is mandatory











3) Running from ANT:
An ANT target can be created which executes this command line task to integrate with the builds.
<target name="RunSOAPUISuite"> <exec executable="C:\Program Files\SmartBear\soapUI-4.0.1\bin\testrunner.bat"> <arg line='-sWebserviceTestSuite -j -f"C:\SOAPUIProjects\Reports" "C:\SOAPUIProjects\WebservicesTesting-soapui-project.xml"'/> </exec> </target>
Build.Xml



























JUnit Report:










Importing the test cases
The test suites and projects can be exported and imported to different machines and thus can be shared and executed by other team members very easily as it provides the same view on import.

SoapUi,Selenium and TestLink Integration


This document will show how to integrate Selenium, Soapui with Test link. This integration is nothing more than sending the execution result of the SOAPUI test script for Test link.
 
Prerequisites:
  • Install Test link version 1.9
  • dbfacade-Testlink-api-client
  • Libraries Selenium RC Java
  • Install SoapUI 3.6.1

I) Configure Test Link
Step1: Enable the project with Automation Testing
















Step2: Edit the configuration file in Test link
1) Add below two lines in file custom_config.inc.php.example
$tlCfg-> api -> enabled
$tlCfg-> exec_cfg-> enable_test_automation
2) Rename custom_config.inc.php.example as custom_config.inc.php
Step3: Generate a new key access
After you enable the API access to XML-RPC and report the activation of automation in the configuration file is necessary to generate the key. This key is created by user and that it will allow the Test link identify who is trying to access the XML-RPC services.
TestLinkHomePage--->My Settings--->Account Settings--->ApiInterface Section--->Click on Generate a new key.









II) Configure SOAPUI

Step1:-Install SOAPUI 3.6.1 Software.
Step2: Create a new Project.
Right Click on Projects--->New soapUI Project.














Step3: Create a Test Suite

Test Suite is a collection of test cases that are intended to be used to test a software program to show that it has some specified set of behaviors.

To Create a test Suite, double click on the Project name, overview of the Project will be displayed. Click on Test Suites Icon as Shown below














Step 4: Create a Test Case.














Step5:Add Groovy Script to test case.












III) Preparing Development Environment (SOAPUI)
Step1:-Add the following jar files in SOAPUI ext folder.
Path: C:\Program Files\eviware\soapUI-3.6.1\bin\ext
  • dbfacade-Testlink:
    • Testlink-client-api-2.0.jar
    • all libs
  • Selenium RC:
    • selenium-server.jar
    • Selenium-java-client-driver. jar
Step2: Create new project (TestLinkSelenium) in Eclipse
Step3: Create new package (test link).
Step4: Create Class ResultadoExecution to package (testlink).
Create ResultadoExecution class, which is responsible for sending the result of the script to Test link.
package testlink;
import testlink.api.java.client.TestLinkAPIClient;
import testlink.api.java.client.TestLinkAPIException;

public class ResultadoExecution implements IConstantes
{
public static void reportTestCaseResult1(String TestProject, String TestPlan, String TestCase, String BuildName, String nota, String resultado) throws TestLinkAPIException
{
TestLinkAPIClient testlinkAPIClient = new TestLinkAPIClient(DEVKEY, URL);
testlinkAPIClient.reportTestCaseResult(TestProject, TestPlan, TestCase, BuildName, nota, resultado);
}
}
Step5: Create Interface Iconstantes to package (testlink).
Create an interface with the data necessary to send the data to the Testlink, which are the parameters that were described above.
package testlink;
public interface IConstantes {
final String DEVKEY = "7c0893a8c63d2599074a308de09ad75b";
final String URL = "http://localhost:82/testlink/lib/api/xmlrpc.php";
final String PROJECT = "SeleniumIntegrationWithTestlink";
final String PLAN = "LCLT001-IntegratingSelenium";
final String BUILD = "integarationbuild1";
final String TestCase1 = "Limsearch";
final String TestCase2 = "Searchbyemail";
}
Here DEVKEY =generated API KEY
PROJECT= Project name in Testlink.
PLAN=TestPlan Name
BUILD=Build Name
TestCase1=TestCaseName (Limsearch)

Step6: Create jar file (soapui.jar)for class ResultadoExecution and interface Iconstantes .This jar file will be use in Soapui.
Step7: Add created jar file (soapui.jar) to ext folder of soapui.
Path: C:\Program Files\eviware\soapUI-3.6.1\bin\ext
Step8: Add the SourceCode to Groovy Script in Soapui.


import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import testlink.api.java.client.TestLinkAPIResults;
import testlink.api.java.client.TestLinkAPIClient;
import testlink.api.java.client.TestLinkAPIException;
import testlink.*; //import ResultadoExecution and IConstantes

public final class Emailsearch implements IConstantes {
private static Emailsearch instance;
public Selenium selenium;

private Emailsearch (String host, int port, String browserType, String url)
{
this.selenium= new DefaultSelenium(host, port, browserType, url);
this.selenium.start ();
this.selenium.open (url);
String resultado = null;
String nota = null;
try {
selenium.open ("/");
selenium.click ("link=Login");
selenium.waitForPageToLoad ("60000");
selenium.type ("login", "bala");
selenium.type ("password", "krishna");
selenium.click ("Submit");
selenium.waitForPageToLoad ("30000");
selenium.click ("link=Search Member");
selenium.waitForPageToLoad ("30000");
selenium.type ("email", "sample@cil.com");
selenium.click ("//input [@type='submit']");
selenium.waitForPageToLoad ("30000");
selenium.click ("link=Logout");
selenium.waitForPageToLoad ("30000");
selenium.close ();
selenium.stop ();
resultado = TestLinkAPIResults.TEST_PASSED
}

catch (Exception e) {
resultado = TestLinkAPIResults.TEST_FAILED;
nota = e.getMessage ();
e.printStackTrace ();
}

finally {
ResultadoExecution.reportTestCaseResult1 (PROJET, PLAN, TestCase1, BUILD, nota, resultado);
}
}

public static Emailsearch getInstance (String host, int port, String browserType, String url)
{
instance = new Emailsearch (host, port, browserType, url);
return instance;
}
}

Emailsearch.getInstance ("172.16.2.212", 4444,"*iexplore","http://www.lowcostloadtest.com");


Step9: Start SeleniumServer.jar
Step10: Launch TestRunner in SoapUi.



































Step11:Click on Launch. Selenium testcase will be executed in iexplore.
















Step12:After completing the execution test Case Results will be updated in Test Link.











Selenium and TestLink Integration

This article will show how to integrate Selenium with Testlink . This integration is nothing more than sending the execution result of the test script for Testlink .
We will create a Java project with the support of testng for creating the test script with Selenium . Below is a list of items needed for this tutorial.

PreRequisites:
  • Install Testlink version 1.9
  • dbfacade-Testlink-api-client
  • IDE for Java development(Eclipce)
  • Java JDK 1.6.0
  • Libraries Selenium RC Java
Configure TestLink:
Step1:Enable the project with Automation Testing















Step2:Edit the configuration file in Testlink
1)Add below two lines in file custom_config.inc.php.example
$tlCfg-> api-> enabled
$tlCfg-> exec_cfg-> enable_test_automation
2)Rename custom_config.inc.php.example as custom_config.inc.php
Step3:Generate a new key access
After you enable the API access to XML-RPC and report the activation of automation in the configuration file is necessary to generate the key. This key is created by user and that it will allow the Testlink identify who is trying to access the XML-RPC services.
TestLinkHomePage--->MySettings--->AccounSettings--->ApiInterface Section--->Click on Generate a new key.










Preparing Development Environment
Step1:Add the following JAR files in eclipse.
  • dbfacade-Testlink:
    • Testlink-client-api-2.0.jar
    • all libs
  • Selenium RC:
    • selenium-server.jar
    • selenium-java-client-driver.jar
Step2:Sample Source Code.

import java.util.Date;
import com.thoughtworks.selenium.*;
import org.testng.Reporter;
import org.testng.annotations.*;
import testlink.api.java.client.TestLinkAPIResults;
public class Selenium_Methods implements IConstantes {
Selenium selenium;
@Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url"})
@BeforeMethod(alwaysRun=true)
public void startBrowser(String host,String port,String browser,String url){
this.selenium= new DefaultSelenium(host, Integer.parseInt(port), browser, url);
this.selenium.start("captureNetworkTraffic=true, addCustomRequestHeader=true,captureNetworkTraffic=true");
this.selenium.open(url);
} //startBrowser();
@Test public void limitedSearch() throws Exception {
String resultado = null;
String nota = null;
try
{
selenium.open("/");
selenium.click("link=Login");
selenium.waitForPageToLoad("30000");
selenium.type("login", "bala");
selenium.type("password", "krishna");
selenium.click("Submit");
selenium.waitForPageToLoad("30000");
if(selenium.isTextPresent("Welcome balakrishna"))
Reporter.log("Successfully log in");
else
Reporter.log("Error on page");
selenium.click("link=Limited Search Member");
selenium.waitForPageToLoad("30000");
selenium.type("email", "10");
selenium.click("//input[@type='submit']");
selenium.waitForPageToLoad("30000");
selenium.click("link=Logout");
selenium.waitForPageToLoad("30000");
resultado = TestLinkAPIResults.TEST_PASSED;
}
catch (Exception e) {
resultado = TestLinkAPIResults.TEST_FAILED;
nota = e.getMessage();
e.printStackTrace();
}
finally {
ResultadoExecucao.reportTestCaseResult1(PROJETO, PLANO, CASO_TESTE1, BUILD, nota, resultado);
}
}
@AfterMethod(alwaysRun=true)
public void stopSelenium() {
Date dt;
dt = new Date();
System.out.println("Execution Terminated at "+ dt);
this.selenium.stop();
} //stopSelenium();
}

Step3:Class ResultadoExecucao
Within the project create ResultadoExecucao class, which is responsible for sending the result of the script to Testlink.
import testlink.api.java.client.TestLinkAPIClient;
import testlink.api.java.client.TestLinkAPIException;
public class ResultadoExecucao implements IConstantes {
public static void reportTestCaseResult1(String projetoTeste, String planoTeste, String casoTeste, String nomeBuild, String nota, String resultado) throws TestLinkAPIException {
TestLinkAPIClient testlinkAPIClient = new TestLinkAPIClient(DEVKEY, URL);
testlinkAPIClient.reportTestCaseResult(projetoTeste, planoTeste, casoTeste, nomeBuild, nota, resultado);
}

Step4:Interface iConstantes
Create an interface with the data necessary to send the data to the Testlink, which are the parameters that were described above.
public interface IConstantes {
final String DEVKEY = "7c0893a8c63d2599074a308de09ad75b";
final String URL = "http://localhost:82/testlink/lib/api/xmlrpc.php";
final String PROJETO = "SeleniumIntegrationWithTestlink";
final String PLANO = "LCLT001-IntegratingSelenium";
final String BUILD = "integarationbuild1";
final String CASO_TESTE1 = "LimitedSearch";
}

Here DEVKEY =generated API KEY
PROJETO = Project name in Testlink.
PLANO =TestPlan Name
BUILD=Build Name
CASO_TESTE1=TestCaseName

Step5:After Execution,the Result will be automatically reflected to TestLink as below.

Converting Jmeter JTL File To CSV File

JtlToCsv Converter:-By using this utility we convert Jmeter jtl file to CSV file.


Prerequisites:
1)Python 2.7

Steps:

Step1:-Create JtlToCsvConverter folder in C drive.
Step2:-Create input,output sub folders in JtlToCsvConverter folder.
Step3:-Copy below code to notepad and save as JtlToCsv_Jmeter.py in JtlToCsvConverter folder.
#!C:\Python27\python.exe -u
import sys
import re
import datetime
import time

startTime = time.time()
cnt = 0
cnt2 = 0
failCnt = 0
reCompile = re.compile("\s([^\s]*?)=\"(.*?)\"")
delimiterCharacterOut = ","

def writeCSVLine(line):

      x = reCompile.findall(line)
      a = dict((row[0], row[1]) for row in x)
     
      try:
          a['ts1'] = str(int(int(a['ts'])/1000))
          x = str(datetime.datetime.fromtimestamp(float(a['ts1'])))[0:19]
          b = a['ts'] + ",\"" + x + "\"," + a['t'] + "," + a['lt'] + ",\"" + a['s'] + "\",\"" + a['lb'] + "\"," + a['rc'] + ",\"" + a['rm'] + "\",\"" + a['tn'] + "\",\"" + a['dt'] + "\"," + a['by'] + ",\"" + a['sc'] + "\"," + a['ec'] + ",\"" + a['ng'] + "\"," + a['na'] + ",\"" + a['hn'] + "\"," + a['in'] + "\n"
      except:
         return -1
      o.write(b)
      return 1

print "Splitting JTL file"

try:

     runArgv = sys.argv            #Save the command line
     jtlInfile = str(sys.argv[1])  #Name of JTL input file
     cvsOutfile = str(sys.argv[2]) # Name of CVS output file
     reFilter = str(sys.argv[3])   # Filter the labels (lb) for the filter

except:
      print "Error: Input format: <input file> <output file> <Filter by regular expression>"
      raise

try:

     f = open(jtlInfile, "r")
     o = open(cvsOutfile, "w")

except:

     raise

print "Filtering on regular expression : " + reFilter
cmpFilter = re.compile(reFilter)
# o.write("timestamp" + ",\""+ "datetime" + "\n")
o.write("timeStamp" + ",\"" + "datetime" + "\"," + "elapsed" + "," + "Latency" + ",\"" + "success" + "\",\"" + "label" + "\"," + "responseCode" + ",\"" + "responseMessage" + "\",\"" + "threadName"+ "\",\"" + "dataType" + "\"," + "bytes" + ",\"" + "SampleCount" + "\"," + "ErrorCount" + ",\"" + "grpThreads" + "\"," + "allThreads" + ",\"" + "Hostname" + "\"," + "IdleTime" + "\n")

for line in f:
    
     try:
           if cmpFilter.search(line):
                 returnVal = writeCSVLine(line)
                 if returnVal<0:
                      failCnt += 1
                 else:
                     cnt2 += 1
     except:
           print 'Error in line : ', cnt, line
           raise
     cnt += 1

endTime = time.time()
print "Time taken : ", str(endTime-startTime)
print "Lines processed : ", cnt
print "Lines that passed the filter : ", cnt2
print "Lines skipped (error?) : ", failCnt
f.close()
o.close()

Step4:-Copy Jtl file to input folder.
            Ex:-sample.jtl
Step5:-Create batch file in JtlToCsvConverter folder to run the python script.
 Runme.bat
cd /d C:\JtlToCsvConverter
JtltoCsv_Jmeter.py C:\JtlToCsvConverter\input\sample.jtl C:\JtlToCsvConverter\output\sample_friday.csv "(.+?)"

Step6:-Click on Runme.bat file,CSV file should be created at output folder.