使用soapui免费版本中的groovy脚本从excel进行数据驱动测试

时间:2016-09-21 06:04:12

标签: soap groovy soapui

当我尝试使用groovy使用以下代码进行参数化时,我收到以下错误日志:

package org.codehaus.groovy.antlr.parser; 
import java.io.*; 
import jxl.*; 
import org.codehaus.groovy.antlr.parser.GroovyRecognizer; 

Workbook workbook = Workbook.getWorkbook(new File("D:\\SOAPUI\\ConversionRate.xls") 
Sheet sheet1 = workbook.getSheet("DataDriven"); 
row = sheet1.getRow(); col = sheet1.getColumns(); 
log.info "Row Count =" +row 
log.info "Column Count =" +col 
for(i=1;i<row;i++) { 
    for(j=0;;j<col;j++) { 
       def celsius = testRunner.tetCase.getStepByName("Properties") 
       cell var = sheet1.getCell(i,j) 
       log.info var.getContents() 
    } 
} 

注意:我已将所有jar和jxl一起放在bin / ext文件夹中。

org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed: Script7.groovy: 1: unable to resolve class com.company.ui.test.SoapUI_T11001_StockConsSecurityCurBusiDate @ line 1, column    
                             ^
org.codehaus.groovy.syntax.SyntaxException: unable to resolve class com.company.ui.test.SoapUI_T11001_StockConsSecurityCurBusiDate @ line 1, column 1.
 at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:148)  
 at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1206)
 at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:148)
 at org.codehaus.groovy.control.CompilationUnit$6.call(CompilationUnit.java:585)
 at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:832)
 at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:519)
 at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:495)
 at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:472)
 at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:292)
 at groovy.lang.GroovyShell.parseClass(GroovyShell.java:727)
 at groovy.lang.GroovyShell.parse(GroovyShell.ja`enter code here`va:739)
 at groovy.lang.GroovyShell.parse(GroovyShell.java:766)
 at groovy.lang.GroovyShell.parse(GroovyShell.java:757)
 at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:141)
 at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:90)
 at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:148)
 at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:274)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
 at java.lang.Thread.run(Thread.java:619)
 1 error

1 个答案:

答案 0 :(得分:1)

Groovy testStep没有自动完成功能,所以有时处理类方法和其他拼写错误有点痛苦...这就是为什么你可能无法使你的代码工作。所以,我们试着列举错误:

  • 在SOAPUI Groovy testStep package中没有必要...... 此外,在您的情况下,这个肯定不是正确的: package org.codehaus.groovy.antlr.parser;
  • 您的代码中没有必需import

    // groovy add java.io.* by default
    import java.io.*
    // this is not used in your code
    import org.codehaus.groovy.antlr.parser.GroovyRecognizer
    
  • 还有一些错别字;在第一行)缺少关闭getWorkbook(;您没有为rowcol变量定义任何类型;您的循环;中有一个额外的for(j=0;;j<cols;j++)

  • 要使用 jxl 获取行和列的总数,请改用Sheet.getColumns()Sheet.getRows()
  • jxl.Cell课程以大写字母(Java convention for class name is CamelCase
  • 开头
  • Sheet.getCell(int column, int row)期望column位置作为第一个参数,row作为第二个参数,而不是相反。

因此,您的代码更正可能是:

import jxl.*

Workbook workbook = Workbook.getWorkbook(new File("D:/SOAPUI/ConversionRate.xls"))
Sheet sheet1 = workbook.getSheet("DataDriven")
def rows = sheet1.getRows()
def cols = sheet1.getColumns()
log.info "Row Count =" + rows 
log.info "Column Count =" + cols 
for(i=1;i<rows;i++) { 
    for(j=0;j<cols;j++) { 
       Cell cell = sheet1.getCell(j,i) 
       log.info cell.getContents() 
    } 
} 

请注意,我没有对它进行测试,但我认为一旦纠正必须有效,请继续尝试。