简体   繁体   English

如何使用Groovy脚本在现有的Excel工作表中编写响应

[英]How to write the response in existing excel sheet using Groovy script

在此处输入图片说明 Initially 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. 我正在尝试将响应保存在Excel工作表中。 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 链接到先前的文章: Groovy脚本以读取xml文件并使用文件内容更新下一步请求

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. 它运作良好...您要做的就是将Excel工作表保存为.xls格式。

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. 让我知道它是否有效。 :) :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM