0

enter image description hereInitially I have placed couple of questions. This is related to then enhancement of my requirement. I am trying to save the response in excel sheet. However responses are getting written twice. Kindly help me where I am lagging to prevent the duplicate records writing.

import jxl.*
import jxl.write.*

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder("Step2#Response")
def request=groovyUtils.getXmlHolder("Step2#Request")


File fr = new File("C:\\Users\\Documents\\Groovy Scripts\\response\\output.xls")

Workbook wk = Workbook.getWorkbook(fr);
WritableWorkbook wr = Workbook.createWorkbook(fr, wk);

WritableSheet sheet= wr.getSheet(0);


def r = sheet.getRows(); 

log.info "rows: ${r}"

xPath1 = "//*:description/text()"     
xPath2 = "//*:OrderId/text()"
xPath3 = "//*:m/text()"

Label orderid = new Label(0,r ,request.getNodeValue(xPath2)); 
sheet.addCell(orderid);
Label mode = new Label(1,r ,request.getNodeValue(xPath3)); 
sheet.addCell(m);
Label description = new Label(2,r , holder.getNodeValue(xPath1));
sheet.addCell(description);
Label response = new Label(3, r, context.expand('${Step2#Response}')); 
sheet.addCell(response); 

wr.write();

wr.close();

Link to earlier post: Groovy script to Read an xml file and update next step request with file contents

Community
  • 1
  • 1
user3212324
  • 163
  • 1
  • 6
  • 23
  • can some one kindly help me how to resolve the duplicate issue – user3212324 Nov 10 '16 at 14:06
  • You added the code which is ok. But is not clear what are you exactly trying to achieve and what is issue? – Rao Nov 10 '16 at 15:51
  • @Rao, Please find in the screenshot, the records are written twice in the sheet. Each record is repeating two times. The above code is written inside step 2 – user3212324 Nov 10 '16 at 16:31
  • I remember mentioning. You may better save data in file opposed to excel. And have the file name reference in excel if you still favour of it. By the way it is way easy to use `.csv` (which can be opened in excel) than to excel format. – Rao Nov 10 '16 at 16:34
  • If I am trying for CSV, I am getting error like unable to recognize OLE stream – user3212324 Nov 10 '16 at 16:38
  • Please check this git hub, it is so easy to ready and write csv files in groovy - https://github.com/xlson/groovycsv – Rao Nov 10 '16 at 16:40
  • I have imported the jars of git hub still I am getting same error unable to recognize OLE stream – user3212324 Nov 10 '16 at 17:12
  • Please check the dependency section. Opencsv is need to copy under SOAPUI_HOME/bin/ext and restart soapui. – Rao Nov 10 '16 at 17:16
  • @ Rao, sorry no luck. Copied opencsv3.8.jar under the folder.. same error – user3212324 Nov 10 '16 at 17:32
  • Kindly help me to identify why the records are being written twice using jxl.jar – user3212324 Nov 11 '16 at 10:43
  • Not sure if I understand question, what you are trying to do, what is the data format and what is the test case structure etc., Do you only need to fill the data in excel or csv also will do? Please edit the question to update ht einformation. – Rao Nov 11 '16 at 11:06
  • @Rao, I am trying to save the response of step2 in a excel sheet. That is the requirement. Data format is first column of the row contains the order id used in the request, second column the xml response of the order. PFB logs for the assertion statement inside step 2. for each response it is executing twice due to which each response is written twice. – user3212324 Nov 11 '16 at 11:22
  • Fri Nov 11 16:43:38 IST 2016:INFO:after file before workbook Fri Nov 11 16:43:39 IST 2016:INFO:after workbook before writable workbook Fri Nov 11 16:43:39 IST 2016:INFO:after writable workbook before sheet Fri Nov 11 16:43:39 IST 2016:INFO:rows: 1 Fri Nov 11 16:43:39 IST 2016:INFO:Mode: Add Fri Nov 11 16:43:40 IST 2016:INFO:after file before workbook Fri Nov 11 16:43:40 IST 2016:INFO:after workbook before writable workbook Fri Nov 11 16:43:40 IST 2016:INFO:after writable workbook before sheet Fri Nov 11 16:43:40 IST 2016:INFO:rows: 2 Fri Nov 11 16:43:40 IST 2016:INFO:Mode: Add – user3212324 Nov 11 '16 at 11:23
  • Now I need to know how to restrict the assertion to execute only once per response. That will solve the issue – user3212324 Nov 11 '16 at 11:28

2 Answers2

1
import java.io.*
import jxl.*
import jxl.write.*
import java.text.SimpleDateFormat

def f=new File("D:\\Testing\\SOAP\\GoogleMapAPI_SoapAutoFile.xls");
def wk= Workbook.getWorkbook(f)
def ws=wk.getSheet("Sheet1")
r=ws.getRows()

def estatus 
def date = new Date()
FileDate = new SimpleDateFormat("ddMMMyyyy_HHmmss")
log.info FileDate.format(date)

def f1 = new File("D:\\Testing\\SOAP\\Report\\GoogleAPIReport\\Report_"+FileDate.format(date)+".xls")
    def wk2 = Workbook.getWorkbook(f)
    def wk1 = Workbook.createWorkbook(f1, wk2)
    def ws1=wk1.getSheet("Sheet1")


for(def i=1;i<r;i++)
{
    log.info "--RRRRRRRRR-------------------------------------------------"
    log.info r
    log.info "--IIIIIIIIIIIIIII-----------------------------------------"
    log.info i

  Cell c1=ws.getCell(2,i)

  if(c1.getContents().equalsIgnoreCase("Y"))
  {
    Cell c2=ws.getCell(3,i)
    log.info c2.getContents()
    Cell c22=ws.getCell(4,i)
    log.info c22.getContents()
    log.info c2.getContents()
    testRunner.testCase.testSuite.setPropertyValue("testdata",c2.getContents())
    testRunner.testCase.testSuite.setPropertyValue("testdata2",c22.getContents())
    Cell c3=ws.getCell(1,i)
    log.info c3.getContents()
  testRunner.runTestStepByName(c3.getContents())

  def assertionsList = testRunner.getTestCase().getTestStepByName(c3.getContents()).getAssertionList()
  for( e in assertionsList){
    log.info "--ASSERTION NAME---------------------------------------------"
    log.info e.getName()
    log.info e.getToken() //gives the value of the content to search for
    log.info e.DESCRIPTION
    log.info e.ID
    log.info e.LABEL
    log.info "--ASERTION STATUS----------------------------------------------"
    log.info e.status
    log.info e.toString()
    estatus=e.status
  }
  log.info i

    enter code here

log.info "--LABEL---------------------------------------------------------"
 }
    log.info estatus
    ws1.addCell(new Label(5, i, estatus.toString()));
    estatus=null

}
wk1.write()
wk1.close()
log.info "DONE"

And the Excel File have:
[enter image description here][1]


Now add the Testcase in SOUPUI
[enter image description here][2]

  [1]: https://i.stack.imgur.com/dnfgt.jpg
  [2]: https://i.stack.imgur.com/cr9Hw.jpg
Sandeep
  • 21
  • 1
  • Hello and welcome to StackOverflow. It is better to complete your answer with a short description before your code snippet. – Hamid Rouhani Mar 15 '17 at 11:45
1

You can try the below code for both reading and writing. It works pretty well... All you have to do is save your excel sheet in .xls format.

import com.eviware.soapui.support.XmlHolder
import java.io.File
import java.io.IOException
import jxl.*
import jxl.read.biff.BiffException
import jxl.write.*
import jxl.write.Label
log.info("Testing Started")
def reqOperationName = "Operation Name"
def inputDataFileName = "FileLocation/filename.xls"
def inputDataSheetName = "Datasheet name"
Workbook workbook = Workbook.getWorkbook(new File(inputDataFileName))
Sheet  sheet1 = workbook.getSheet(inputDataSheetName)


def myList = new ArrayList<String>();
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
String xmlResponse = reqOperationName+"#Request"
def reqholder = groovyUtils.getXmlHolder(xmlResponse)
try{
    rowcount = sheet1.getRows()
    colcount = sheet1.getColumns()

    for(Row in 1..rowcount-1){

    String reqTagName = sheet1.getCell(0,0).getContents()

    def TagCount = reqholder["count(//*:"+reqTagName+")"]

    if(TagCount!=0){
        String reqTagValue = sheet1.getCell(0,Row).getContents()
        if(reqTagValue!=null && !reqTagValue.isEmpty() && reqTagValue!="")
                {
                    reqholder = groovyUtils.getXmlHolder(xmlResponse)
                    log.info "extracted value : " + reqTagValue
                reqholder.setNodeValue("//*:"+reqTagName, reqTagValue)
                reqholder.updateProperty()        
                log.info "node value : " + reqholder.getNodeValue("//*:"+reqTagName)
                //test the request
                testRunner.runTestStepByName(reqOperationName)
                reqholder = groovyUtils.getXmlHolder(reqOperationName+"#Response")
                myList.add(reqholder.getPrettyXml().toString())
                log.info myList[Row-1]
                }                      
    }

    }
}
catch (Exception e) {log.info(e)}
finally{
    workbook.close()
}
Workbook existingWorkbook = Workbook.getWorkbook(new File(inputDataFileName));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File(inputDataFileName), existingWorkbook);

try
{
    WritableSheet sheetToEdit = workbookCopy.getSheet(inputDataSheetName);
    WritableCell cell;
    for (int i =1;i<myList.size();i++)
    {
    def resholder = groovyUtils.getXmlHolder(myList[i])

    resTagValue1= resholder.getNodeValue("//*:Response Field Element1")
    Label l = new Label(2, i+1, resTagValue1.toString());
    cell = (WritableCell) l;
    sheetToEdit.addCell(cell);

    resTagValue2= resholder.getNodeValue("//*:Response Field Element2")
    Label m = new Label(3, i+1, resTagValue2.toString());
    cell = (WritableCell) m;
    sheetToEdit.addCell(cell);

    resTagValue3= resholder.getNodeValue("//*:Response Field Element3")
    Label n = new Label(4, i+1, resTagValue3.toString());
    cell = (WritableCell) n;
    sheetToEdit.addCell(cell);

    resTagValue4= resholder.getNodeValue("//*:Response Field Element4")
    Label o = new Label(5, i+1, resTagValue4.toString());
    cell = (WritableCell) o;
    sheetToEdit.addCell(cell);

    }
}
catch (Exception e) {log.info(e)}
finally{
     workbookCopy.write();
 workbookCopy.close();
 existingWorkbook.close();
}
log.info("Testing Over")

Let me know if it works. :)

Sam
  • 62
  • 8