Tuesday, March 27, 2012

test result

import java.awt.*; 
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
import javax.swing.table.*;
import java.io.File;
import com.eviware.soapui.support.*;
import java.util.*;
import jxl.*;
import java.lang.*;
import java.io.*;
import jxl.*
import jxl.write.*
import jxl.write.WritableSheet;
import com.eviware.soapui.model.testsuite.TestRunner.Status;
import com.eviware.soapui.impl.wsdl.WsdlInterface
import com.eviware.soapui.config.TestStepConfig;
import com.eviware.soapui.impl.wsdl.teststeps.*;
import com.eviware.soapui.impl.wsdl.testcase.*;
import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestStepFactory
import com.eviware.soapui.support.types.StringToStringsMap.*;
import java.text.SimpleDateFormat
import java.text.DateFormat
def properties = testRunner.testCase.getTestStepByName("Properties");
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
properties.setPropertyValue( "projectPath", groovyUtils.projectPath);
String projectpath = properties.getPropertyValue("projectPath").trim();
String operationName = properties.getPropertyValue("operationName").trim();;
//properties.setPropertyValue( "operationName", map.get("Operation Name"));
String[] testcaseNameArray;
String[] expectedResultArray;
String[] actualResultArray;
String[] statusArray;
String serviceQualorQualifiedProductRequest = null;
def serviceQualStep
def serviceQualStepRequest;
if(operationName != "ALL")
{
 serviceQualStep = testRunner.testCase.getTestStepByName(operationName);
 serviceQualStepRequest = serviceQualStep.getProperty("Request");
}
def headers = new types.StringToStringsMap()
String tenantId = properties.getPropertyValue("tenantId").trim();
headers.put("tenantId",tenantId)
//headers.put("tenantId","831D8B107026BBEBE0400F0A32207789")
if(operationName != "ALL")
{
testRunner.testCase.getTestStepByName(operationName).testRequest.setRequestHeaders(headers)
}
if( serviceQualStepRequest != null &&  serviceQualStepRequest != "")
{
serviceQualorQualifiedProductRequest  = serviceQualStepRequest.getValue();
}
def pname = testRunner.testCase.testSuite.project.name;
log.info(pname)
//log.info(colcount)
//if("submitApplication".equals(operationName) || "updateStatus".equals(operationName) || "addPerson".equals(operationName)  )
if("ALL" != operationName)
{
Workbook workbook = Workbook.getWorkbook(new File(projectpath + "file://data.xls/"))
Sheet sheet         =  workbook.getSheet(operationName);
rowcount            =  sheet.getRows();
colcount            =  sheet.getColumns();
testcaseNameArray = new String[colcount-1];
expectesResultArray = new String[colcount-1];
actualResultArray = new String[colcount-1];
statusArray = new String[colcount-1];
String  variableName;
String variableValue;
String testcasename;
String removeNode;
def counter=0;
while(colcount>1)
{
for(int i=1;i<rowcount;i++)
{
testcasename = sheet.getCell(counter+1,0).getContents();
variableName = sheet.getCell(0,i).getContents();
variableValue = sheet.getCell(1+counter,i).getContents();
if(variableValue == "" )
{
removeNode = variableName;
//log.info("Remove:"+removeNode)
}
//def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder(operationName+"#Request")
holder.remove("//"+removeNode);
holder.updateProperty()
//log.info(variableName+":"+variableValue+"|")
if( variableValue.contains("/") && variableValue != null )
{
DateFormat formatter = new SimpleDateFormat("yyyy/mm/dd");
Date date = (Date)formatter.parse(variableValue);
variableValue=date.toString();
formatter = new SimpleDateFormat("yyyy-MM-DD");
variableValue=formatter.format(date);
properties.setPropertyValue(variableName,variableValue );
//log.info("DATE::"+variableName+"::"+variableValue);
}
if(variableValue!= null && variableValue!= "")
{
 properties.setPropertyValue(variableName,variableValue );
}
//log.info(s2)
}
//workbook.close()
def testStepName=operationName+"_"+testcasename
//def newTestStep = testRunner.testCase.addTestStep( config );
def checkStep = testRunner.testCase.getTestStepByName(testStepName);
//log.info(checkStep.getLabel());
if(checkStep != null)
{
//testRunner.testCase.remove(serviceQualStep,testStepName );
testRunner.testCase.removeTestStep(testRunner.testCase.getTestStepByName(testStepName))
}
def checkStep1 = testRunner.testCase.getTestStepByName(testStepName);
if(checkStep1 == null)
{
def newTestStepClone = testRunner.testCase.cloneStep(serviceQualStep,testStepName );
//newTestStepClone.setDisabled(false);
}
testRunner.gotoStepByName(testStepName);
testRunner.runTestStepByName(testStepName)
log.info(testcasename);
def aOrderStep = testRunner.testCase.getTestStepByName(testStepName);
String addOrderRequest = aOrderStep.getProperty("Request").getValue();
String addOrderResponse = aOrderStep.getProperty("Response").getValue();
String errorCode = null;
def node = new XmlSlurper().parseText(addOrderResponse);
errorCode = node.Body.Fault.faultcode.text();
//def su = node.Body.updateFinancialAidResponse.status.text();
//log.info(errorCode.toString())
String ActualResult = null;
String message;
String status;
String ExpectedResult = properties.getPropertyValue("ExpectedResult").trim();
if(errorCode != "" && errorCode != null )
{
String errorMessage = node.Body.Fault.faultstring.text();
String detail = node.Body.Fault.detail.serviceFault.errorCode.text();
message = '\nError Code = '+detail+ '\nError Message = '+ errorMessage;
ActualResult = "SOAP FAULT";
//log.info("PPIF");
//status = "FAIL";
//UISupport.showErrorMessage(' Failed! '+'\n'+'\nTestCaseName = '+testStepName+'\n'+message );
}
else
{
 //log.info("PPELSE");
ActualResult = "NOT SOAP FAULT";
}
if(ExpectedResult.equals(ActualResult))
{
status = "PASS";
}
else
{
status = "FAIL";
}

def d1= new File(projectpath+"//"+operationName).mkdir()
new File(projectpath+"//"+operationName+"//" + testStepName + "_request.xml" ).write( addOrderRequest )
new File(projectpath+"//"+operationName+"//" + testStepName + "_response.xml" ).write( addOrderResponse )
testcaseNameArray[counter] = testcasename;
expectesResultArray[counter] = ExpectedResult;
actualResultArray[counter] = ActualResult;
statusArray[counter] = status
colcount--;
counter++;
serviceQualStepRequest.setValue(serviceQualorQualifiedProductRequest);
}
workbook.close()
}

 private JPanel topPanel;
 private JTable table;
 private JScrollPane scrollPane;
 def columnNames = ['Sr.No','TestCaseName','ExpectedResult','ActualResult','Status'].toArray();
 def dataValues = new String[100][18];
 int i = 0;
  for( fAddress in testcaseNameArray )
  {
   dataValues[i][0] = (i+1).toString();
   dataValues[i][1] = fAddress.toString();
   dataValues[i][2] = expectesResultArray[i].toString();
   dataValues[i][3] = actualResultArray[i].toString();
   dataValues[i][4] = statusArray[i].toString();
    
   i = i + 1;
  
  }
 
  // Create a panel to hold all other components
  topPanel = new JPanel();
  topPanel.setLayout( new BorderLayout() );
  topPanel.setBackground( Color.cyan );
  // Create a new table instance
  table = new JTable( dataValues, columnNames );
  // Configure some of JTable's paramters
  table.setShowHorizontalLines( true );
  table.setRowSelectionAllowed( true );
  table.setColumnSelectionAllowed( true );
  table.setPreferredScrollableViewportSize(new Dimension(500,400));
  //table.setFillsViewportHeight(true);
 
  // Disable auto resizing
  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
   
        // Set the first visible column to 100 pixels wide
        int vColIndex = 0;
        TableColumn col = table.getColumnModel().getColumn(vColIndex);
        int width = 50;
        col.setPreferredWidth(width);
  // Change the selection colour
  table.setSelectionForeground( Color.white );
  table.setSelectionBackground( Color.red );
  //table.getColumnModel().getColumn(4).toString()
  // Add the table to a scrolling pane
  scrollPane = table.createScrollPaneForTable( table );
  topPanel.add( scrollPane, BorderLayout.CENTER );
 def dialog = com.eviware.soapui.support.UISupport.createConfigurationDialog( "Result" );
 dialog.setContent(topPanel);
 dialog.show(new HashMap());
   

to read excel sheet groovy

import com.eviware.soapui.support.*;
import java.io.File;
import java.util.*;
import jxl.*;
import java.lang.*;
import java.io.*;
import jxl.*
import jxl.write.*
import com.eviware.soapui.model.testsuite.TestRunner.Status;
import com.eviware.soapui.impl.wsdl.WsdlInterface
def properties = testRunner.testCase.getTestStepByName("Properties");
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
properties.setPropertyValue( "projectPath", groovyUtils.projectPath);
String projectpath = properties.getPropertyValue("projectPath").trim();
Workbook workbook = Workbook.getWorkbook(new File(projectpath + "file://data.xls/"))
Sheet sheet         =  workbook.getSheet("MasterSheet");
rowcount            =  sheet.getRows();
colcount            =  sheet.getColumns();
String  variableName;
String variableValue;
String testcasename;
String removeNode;
def counter=0;
while(colcount>1)
{
for(int i=1;i<rowcount;i++)
{
testcasename = sheet.getCell(counter+1,0).getContents();
variableName = sheet.getCell(0,i).getContents();
variableValue = sheet.getCell(1+counter,i).getContents();

if(variableValue!= null && variableValue!= "")
{
 properties.setPropertyValue(variableName,variableValue );
}
}
counter++;
colcount--;
}
workbook.close()

String targetUrl = properties.getPropertyValue("targetUrl").trim();
java.util.List l1 = testRunner.testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep);
ArrayList al = l1.toArray();
String[] operationName1 = new String[al.size()]
Iterator itr = al.iterator();
int ii = 0;
while(itr.hasNext())
{
    com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep step = (com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep)itr.next();
     step.getTestRequest().setEndpoint(targetUrl);
    operationName1[ii] = step.getOperationName();
   log.info(operationName1[ii]);
   ii++;
}

List<String> list = Arrays.asList(operationName1)
Set<String> set = new HashSet<String>(list)
operationName1 = new String[set.size()]
set.toArray(operationName1);
 //operationName1[0] = "ALL"; 
//String[] operationArray     = ["copyDocument","deleteDocument", "findDocumentsByExample","ALL"].toArray();
HashMap map = new HashMap();
def dialog = com.eviware.soapui.support.UISupport.createConfigurationDialog( "Select Operation to be invoke" );
dialog.addComboBox( "Operation Name", operationName1, null);
map.put( "Operation Name", properties.getPropertyValue("operationName"));

boolean finished = false;
while (!finished)  {
                if (!dialog.show(map))  {
                                testRunner.cancel( "Failed to capture entry!" );
                                return;
                }
                finished = true;
}
String operationName = (String) map.get("Operation Name");
properties.setPropertyValue( "operationName", map.get("Operation Name"));
if("ALL".equals(operationName))
{
 for(int i = 4 ; i<operationName1.length;i++)
 {
  operationName = operationName1[i];
  properties.setPropertyValue( "operationName", operationName);
  //log.info(operationName1.length)
  //log.info("OPERATION NAME:::::"+operationName);
  //log.info("CALLS:::::"+i);
  if(operationName != null && operationName!= "ALL" )
  { //testRunner.gotoStepByName("Delay");
   testRunner.gotoStepByName("ALL");
   testRunner.runTestStepByName("ALL")
  }
 }
}
String serviceQualorQualifiedProductRequest = null;
def serviceQualStep = testRunner.testCase.getTestStepByName(operationName);
def serviceQualStepRequest = serviceQualStep.getProperty("Request");
if( serviceQualStepRequest != null &&  serviceQualStepRequest != "")
{
serviceQualorQualifiedProductRequest  = serviceQualStepRequest.getValue();
}
//def pname = testRunner.testCase.testSuite.project.name;
//log.info(pname)
//log.info(colcount)
//if("submitApplication".equals(operationName) || "updateStatus".equals(operationName) || "addPerson".equals(operationName))
if("ALL" != operationName)
{
   testRunner.gotoStepByName("ALL");
   testRunner.runTestStepByName("ALL")
}