簡體   English   中英

用Groovy腳本創建類

[英]Creating Classes in Groovy Script

我試圖使用groovy腳本步驟在soap UI中編寫類並在需要的地方執行。
但是它顯示錯誤為:

“ org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack:方法的無簽名:excel.main()適用於參數類型:([Ljava.lang.String;)值:[[]]可能的解決方案:wait() ,wait(long),find(),any(),wait(long,int),find(groovy.lang.Closure)

 import groovy.json.JsonSlurper
 import java.io.*;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.ss.usermodel.*;
 import java.util.Iterator;

    class excel {
         static void ReadWriteExcel(String a,int b) 
             {            
                 def projectPath = new 
                 com.eviware.soapui.support.GroovyUtils(context).projectPath 
                 //gets the path of the project root
                 FileInputStream fIpStream= new 
                 FileInputStream(projectPath+"\\Bat_File_Data.xls")
                 HSSFWorkbook wb = new HSSFWorkbook(fIpStream);
                 HSSFSheet worksheet = wb.getSheetAt(0);              
                 int noOfRows = worksheet.getLastRowNum();      
                 int noOfColumns = worksheet.getRow(0).getLastCellNum();
                 for (int i=1;i<2;i++)
                     {  
                        //def res = wb.getSheetAt(0).getRow(1).getCell(0);
                        def res = wb.getSheetAt(0).getRow(i).getCell(1);
                        //log.info res         
                         Row row = worksheet.createRow(i);
                         Cell cell = row.createCell(2);
                         Cell cell1 = row.createCell(3);
                         cell.setCellValue(a);             
                         cell1.setCellValue(b);
                     }     
            fIpStream.close(); //Close the InputStream
            FileOutputStream output_file =new FileOutputStream(new 
            File(projectPath+"\\Bat_File_Data.xls"));  
            wb.write(output_file);
            output_file.close();      

    }  
}
        class Groovy 
        {
            static void main(String[] args)
                {
                ReadWriteExcel("Pass",2234);
                } 
        }

在groovy腳本中,您可以這樣聲明和訪問它的靜態方法:

class A{
    static void greet(String name){
        println "hello $name!"
    }
}

//code without class declaration will be executed as part of script.run() method 
A.greet("world")

在Soapui中,無需編譯任何內容,就可以使用這樣的類:

class A{
   def log
   def context
   def testRunner

   def A(logIn,contextIn,testRunnerIn){
      this.log = logIn
      this.context = contextIn
      this.testRunner = testRunnerIn
   }

   def method(){
       //your code
   }
//log, context, testRunner are global variables in Soapui
context.setProperty( "A", new A( log, context, testRunner) )

然后,使用context.A.method()調用您的類

如果您在另一個測試步驟中,則可以使用: context.workspace.getProjectByName("ProjectName").getTestSuiteByName("TESTSUITE").testCases["TESTCASE"].testSteps["TESTSTEP"].run(testRunner, context)
context.A.method()
在常規步驟之外上課

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM