【问题标题】:Creating Classes in Groovy Script在 Groovy 脚本中创建类
【发布时间】:2017-08-23 13:58:10
【问题描述】:

我正在尝试使用 groovy 脚本步骤在肥皂 UI 中编写类并在任何需要的地方执行。
但它显示错误为:

“org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: 没有方法签名: excel.main() 适用于参数类型: ([Ljava.lang.String;) 值:[[]] 可能的解决方案:wait(), 等待(long),find(),any(),等待(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);
                } 
        }

【问题讨论】:

  • 你在使用readyapi吗?还是开源版软件?如果您在 groovy 脚本测试步骤中编写上述脚本并尝试在另一个步骤中调用?那么它不适用于上述方法。您需要编译类并创建jar文件并将其放在SOAPUI_HOME/bin/ext目录下,在开始使用之前重启soapui工具。
  • 请查看此线程以了解如何实现 - rupertanderson.com/blog/…

标签: groovy apache-poi soapui


【解决方案1】:

在 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")

【讨论】:

  • 嗨 Rao,我正在使用开源 Soap UI,我按照您所说的进行了操作,但无法解决。你能建议我任何其他答案吗
【解决方案2】:

在 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()
在你的常规步骤之外运行你的课程

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多