Forum Discussion

mchelikani's avatar
mchelikani
Contributor
12 years ago

dynamically adding assertions to TestStep in Event Handler

Hi,
I am adding dynamically adding assertions to teststep by reading from excel sheet in TestRunLister.beforeStep. I am getting the below exception. Please help.

Could you please let me know what is wrong in below code? The issue is when I set the Path for Assertion which is highlighted below in bold.


Code:


/* Grrovy Script to Read Assertions for Folder and Validate them.
*/

import groovy.io.*
import org.apache.poi.hssf.usermodel.*
import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;



def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def DEFAULT_ENV = "qap"
//TestCaseName
def testCase = testRunner.testCase
def testStep = context.testCase.getTestStepAt(context.getCurrentStepIndex())

def testCaseName = testCase.name
def testStepName = testStep.getLabel()

//Get TestStep Type
def testStepType = testStep.config.type

// Assertions will be done only for HttpRequests
if(testStepType =='httprequest'){

def xPathFilePath = getXpathFilePath(getTestCaseSourceFolder(), getEnvironment(DEFAULT_ENV) , testCaseName)
def xPathFileName = getXpathFileName( testCaseName , testStepName , "_XPath.xls")

def xPathFileAbsPath = xPathFilePath + xPathFileName

def xpathFile = new File(xPathFileAbsPath)

if(xpathFile.exists()){
log.info("Checking if Xpaths Assertions Present In File : "+xPathFileAbsPath)
//Get All the Spreadsheet Rows
def excelDataSheet = getExcelDataSheetFromFile(xpathFile)
def excelDataIter = excelDataSheet.rowIterator()
def excelDataSizeIter = excelDataSheet.iterator()
log.info("Excel Data: "+excelDataIter)

if(excelDataSizeIter.size() > 1){ //Check if Spread Sheet Contains More than One Row
excelDataIter.each{ row->
//Skip the First Row as It Contains the Headings
if (row.getRowNum() > 0) {
buildAssertion(row, testStep)
}
}
}else{
log.info("There are NO XPath Assertions Present in File : "+xPathFileAbsPath)
}

}else{
log.info("XPath File "+ xPathFileAbsPath +" DOES NOT Exists.")
}

}

/*
* Read Excel File and Return Rows
*/
def getExcelDataSheetFromFile(file){

//Create a new workbook using POI API
def workbook = new HSSFWorkbook(new FileInputStream(file))

//Get first sheet of the workbook (assumes data is on first sheet)
def sheet = workbook.getSheetAt(0)

return sheet
}

/*
* Get TestCaseSource Folder Path
*/
def getTestCaseSourceFolder(){
return context.expand( '${#Project#testcase_src}' )
}
/*
* Get Environment, If No Environment Return Default.
*/
def getEnvironment(DEFAULT_ENV){
def environment = DEFAULT_ENV
//If Environment is Passed from Maven, Use that
if(context.expand( '${#Project#environment}' ) != ''){
environment = context.expand( '${#Project#environment}' )
}
log.info("Environment : "+environment)

return environment;
}

/*
* Get Xpath File Path
*/
def getXpathFilePath(testcaseSrcFolder , environment , testCaseName){
def xPathFilePath = new StringBuilder()
xPathFilePath.append(testcaseSrcFolder)
xPathFilePath.append(environment)
xPathFilePath.append("/")
xPathFilePath.append(testCaseName)
xPathFilePath.append("/")

return xPathFilePath.toString()
}

/*
* Get XPathFile Name
*/
def getXpathFileName(testCaseName , testStepName , xPathFileSuffix){
def xPathFileName = new StringBuilder()
xPathFileName.append(testCaseName)
xPathFileName.append("_")
xPathFileName.append(testStepName)
xPathFileName.append(xPathFileSuffix)

return xPathFileName.toString()
}
/*
* Create Assertion
*/
def buildAssertion(row, testStep){
log.info("\n Xpath Assertion, Row Number :"+ row.getRowNum())
def xpath = row.getCell(0)
def xpathValue = row.getCell(1)
def xpathValidationType = row.getCell(2)

//Print the Row Values
log.info("XPATH :"+row.getCell(0))
log.info("XPATH_VALUE :"+row.getCell(1))
log.info("XPATH_VALIDATION_TYPE :"+row.getCell(2))


if(xpathValidationType.getStringCellValue() == "1"){
log.info("Adding Check for existance of Assertion :"+xpath + " to TestStep " + testStep.getLabel())
def assertion = testStep.addAssertion("Check for existance of")
assertion.path = xpath
assertion.expectedContent = true

} else if (xpathValidationType.getStringCellValue() == "2"){
log.info("Adding XPath Match Assertion :"+xpath + " to TestStep " + testStep.getLabel())
//assert nodeValue == xpathValue
def assertion = testStep.addAssertion("XPath Match")
assertion.path = xpath
assertion.expectedContent = xpathValue
log.info("Added Assertion)))))))))))))))))))))))))))))))))))))")
} else if (xpathValidationType.getStringCellValue() == "0"){
log.info("XPATH_VALIDATION_TYPE =" +xpathValidationType +".Hence Not Adding Assertion.")
}
}







Exception:

2013-11-28 02:12:15,342 INFO [log] Checking if Xpaths Assertions Present In File : D:\svn_b2b\B2B\Projects\TestHarness-SoapUI/b2b/src/test/soap-ui/qap/Amadeus/Amadeus_RateRange_XPath.xls
2013-11-28 02:12:15,489 INFO [log] Excel Data: java.util.TreeMap$ValueIterator@4aa3d0bd
2013-11-28 02:12:15,504 INFO [log]
Xpath Assertion, Row Number :1
2013-11-28 02:12:15,505 INFO [log] XPATH ://*:OTA_HotelAvailRS/*:RoomStays/*:RoomStay/@AvailabilityStatus
2013-11-28 02:12:15,505 INFO [log] XPATH_VALUE :AvailableForSale1
2013-11-28 02:12:15,505 INFO [log] XPATH_VALIDATION_TYPE :2
2013-11-28 02:12:15,510 INFO [log] Adding XPath Match Assertion ://*:OTA_HotelAvailRS/*:RoomStays/*:RoomStay/@AvailabilityStatus to TestStep RateRange
2013-11-28 02:12:15,515 ERROR [EventHandlersRequestFilter] java.lang.NullPointerException: Cannot set property 'path' on null object
2013-11-28 02:12:15,515 ERROR [SoapUI] An error occured [Cannot set property 'path' on null object], see error log for details
2013-11-28 02:12:15,516 ERROR [errorlog] java.lang.NullPointerException: Cannot set property 'path' on null object
java.lang.NullPointerException: Cannot set property 'path' on null object
at org.codehaus.groovy.runtime.NullObject.setProperty(NullObject.java:66)
at org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:179)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:480)
at Script1.buildAssertion(Script1.groovy:142)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:361)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:885)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:46)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:145)
at Script1$_run_closure1.doCall(Script1.groovy:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:885)
at groovy.lang.Closure.call(Closure.java:405)
at groovy.lang.Closure.call(Closure.java:418)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1240)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1216)
at org.codehaus.groovy.runtime.dgm$124.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:271)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at Script1.run(Script1.groovy:41)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:96)
at com.eviware.soapui.support.scripting.groovy.SoapUIProGroovyScriptEngineFactory$SoapUIProGroovyScriptEngine.run(SourceFile:89)
at com.eviware.soapui.eventhandlers.support.DefaultSoapUIScript.invoke(SourceFile:47)
at com.eviware.soapui.eventhandlers.support.DefaultSoapUIEventHandler.invoke(SourceFile:29)
at com.eviware.soapui.eventhandlers.support.AbstractEventHandlerMetaData.invokeHandlers(SourceFile:73)
at com.eviware.soapui.eventhandlers.support.AbstractEventHandlerMetaData.invokeHandlers(SourceFile:49)
at com.eviware.soapui.eventhandlers.impl.EventHandlersTestRunListener.beforeStep(SourceFile:64)
at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.runTestStep(AbstractTestCaseRunner.java:232)
at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.runCurrentTestStep(WsdlTestCaseRunner.java:48)
at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:147)
at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:42)
at com.eviware.soapui.impl.wsdl.support.AbstractTestRunner.run(AbstractTestRunner.java:135)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

1 Reply

  • Hi,
    I tried to reproduce it with similar code. I was able to execute it successfully once and then it asked for a unique name for the assertion when I executed again through GUI. I saw the same exception when I clicked the "Cancel" in the name dialog. If I enter a unique name then it adds the assertion and sets the path correctly. So, I think the assertion was added in first run and it stays there once the project is saved - you don't have to add it in every run/execution - rather you just want to update the path and expectedContent values if the assertion already exists.
    Do you also see the error in same scenario?

    Can you please change:
    mchelikani wrote:
    def assertion = testStep.addAssertion("Check for existance of")

    To:

    def assertion = testStep.getAssertionByName("Check for existance of")
    if(assertion == null){
    assertion = testStep.addAssertion("Check for existance of")
    }


    and

    mchelikani wrote:
    def assertion = testStep.addAssertion("XPath Match")

    To:

    def assertion = testStep.getAssertionByName("XPath Match")
    if(assertion == null){
    assertion = testStep.addAssertion("XPath Match")
    }

    and try again.
    I hope it helps.

    Best Regards,
    Prakash
    SmartBear Sweden