My previous post Just a simple Groovy test flow - Update, Execute and Assert states a simple flow of a simple test case, but in that scenario everything was hard-coded in the code, means we have provided value in code only but what if we have to execute the same test for different set of values, then we can easily use data driven technique i.e., we can use excel file - store our test data over there and then execute the groovy code.
Yes, I know this might have already made you jumped out of your seat, so without delaying further let me also jump straight to the point.
For accessing data driven using Excel file we will be using JExcelApi. You need to download the jxl.jar file and place it in you /SOAPUI-Installation folder/lib folder and then make sure you also need to restart your SOAPUI.
Modifying the code we have created for the previous post, our new code will look like this.
There is one point which you need to take care of creating test data in Excel file, which is, JExcelAPI only works till Excel 2003 and not there after, so you need to create Excel file in Excel 2003 or below.
Yes, I know this might have already made you jumped out of your seat, so without delaying further let me also jump straight to the point.
For accessing data driven using Excel file we will be using JExcelApi. You need to download the jxl.jar file and place it in you /SOAPUI-Installation folder/lib folder and then make sure you also need to restart your SOAPUI.
Modifying the code we have created for the previous post, our new code will look like this.
import jxl.*
// Step 1: Update the Request in the SOAP webservice
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def request = groovyUtils.getXmlHolder("<TestStepName>#Request")
request.namespaces["ns1"] = "http://www.webserviceX.NET/"
// Opening the Excel file
Workbook testData = Workbook.getWorkbook(new File("D:\\soapui practise\\test.xls"))
// Opening the first sheet
Sheet sheet1 = testData.getSheet(0)
// Retrieving the data
Cell a1 = sheet1.getCell(0,0) // Retrieve the fromCurrency
Cell a2 = sheet1.getCell(1,0) // Retrieve the toCurrency
def fromCurrency = a1.getContents()
def toCurrency = a2.getContents()
request.setNodeValue("//ns1:ConversionRate/ns1:FromCurrency",fromCurrency)
request.updateProperty()
request.setNodeValue("//ns1:ConversionRate/ns1:ToCurrency",toCurrency)
request.updateProperty()
// Step 2: Execute the SOAP webservice.
testRunner.runTestStepByName("<TestStepName>")
// Retrieve Response
def response = groovyUtils.getXmlHolder("<TestStepName>#Response")
response.namespaces["ns2"] = "http://www.w3.org/2001/XMLSchema"
// Step 3: Add xPath assertion
def testst = testRunner.testCase.getTestStepByName("<TestStepName>")
// Following pseudo code will just get if any assertion of name xPath exists or not, if no assertion of xPath exists then it will procede to create a new xPath assertion else it will remove pre-existing before and then will create a new one.
def asserting = testst.getAssertionByName("XPath Match")
if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
{
testst.removeAssertion(asserting)
}
def assertion = testst.addAssertion("XPath Match")
assertion.path = "//ConversionRateResponse/ConversionRateResult"
assertion.expectedContent = <type in here whatever the content you want to match with>
// Step 1: Update the Request in the SOAP webservice
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def request = groovyUtils.getXmlHolder("<TestStepName>#Request")
request.namespaces["ns1"] = "http://www.webserviceX.NET/"
// Opening the Excel file
Workbook testData = Workbook.getWorkbook(new File("D:\\soapui practise\\test.xls"))
// Opening the first sheet
Sheet sheet1 = testData.getSheet(0)
// Retrieving the data
Cell a1 = sheet1.getCell(0,0) // Retrieve the fromCurrency
Cell a2 = sheet1.getCell(1,0) // Retrieve the toCurrency
def fromCurrency = a1.getContents()
def toCurrency = a2.getContents()
request.setNodeValue("//ns1:ConversionRate/ns1:FromCurrency",fromCurrency)
request.updateProperty()
request.setNodeValue("//ns1:ConversionRate/ns1:ToCurrency",toCurrency)
request.updateProperty()
// Step 2: Execute the SOAP webservice.
testRunner.runTestStepByName("<TestStepName>")
// Retrieve Response
def response = groovyUtils.getXmlHolder("<TestStepName>#Response")
response.namespaces["ns2"] = "http://www.w3.org/2001/XMLSchema"
// Step 3: Add xPath assertion
def testst = testRunner.testCase.getTestStepByName("<TestStepName>")
// Following pseudo code will just get if any assertion of name xPath exists or not, if no assertion of xPath exists then it will procede to create a new xPath assertion else it will remove pre-existing before and then will create a new one.
def asserting = testst.getAssertionByName("XPath Match")
if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
{
testst.removeAssertion(asserting)
}
def assertion = testst.addAssertion("XPath Match")
assertion.path = "//ConversionRateResponse/ConversionRateResult"
assertion.expectedContent = <type in here whatever the content you want to match with>
There is one point which you need to take care of creating test data in Excel file, which is, JExcelAPI only works till Excel 2003 and not there after, so you need to create Excel file in Excel 2003 or below.
No comments:
Post a Comment