我所做的
我正在使用soapUI (3.6.1免费版本)模拟服务向我正在测试的2个客户端应用程序提供特定数据。使用一些简单的Groovy脚本,我设置了一些模拟操作,以便根据客户端应用程序提出的请求从特定文件中获取响应。
模拟响应的静态内容是:
${responsefile}操作分派脚本窗格中的groovy是:
def req = new XmlSlurper().parseText(mockRequest.requestContent)
if (req =~ "CategoryA")
{
context.responsefile = new File("C:/soapProject/Test_Files/ID_List_CategoryA.xml").text
}
else
{
context.responsefile = new File("C:/soapProject/Test_Files/ID_List_CategoryB.xml").text
}在本例中,当客户端应用程序向包含字符串CategoryA的模拟服务发出请求时,soapUI返回的响应是文件ID_List_CategoryA.xml的内容。
我想要实现的
这对于groovy中的绝对路径都很好。现在,我想将soapUI项目文件和外部文件的整个集合提取到一个包中,以便于重新部署。从我对soapUI的阅读中,我希望这将像将项目资源根值设置为${projectDir}一样简单,并将路径更改为:
def req = new XmlSlurper().parseText(mockRequest.requestContent)
if (req =~ "CategoryA")
{
context.responsefile = new File("Test_Files/ID_List_CategoryA.xml").text
}
else
{
context.responsefile = new File("Test_Files/ID_List_CategoryB.xml").text
}..。请记住,soapUI项目xml文件驻留在C:/soapProject/
到目前为止我尝试过的
所以,这不管用。我试过不同的相对路径:
有一篇文章指出,为了相对路径的目的,soapUI可能会将项目文件父目录视为根目录,因此也尝试了以下变体:
当所有这些都不起作用时,我尝试使用groovy脚本中的${projectDir}属性,但是所有这样的尝试都失败了,出现了"No : mockService for class: Scriptn“错误。Admittefly,我在尝试做这件事时,真的是在摸索。
我尝试使用这篇文章和其他文章中的信息:How do I make soapUI attachment paths relative?
..。没有任何运气。在该帖子中的解决方案代码中,将"test“替换为”more“(以及其他更改)会导致更多的属性错误。
testFile = new File(mockRunner.project.getPath())。。导致..。
No such property: mockRunner for class: Script3我认为我需要的
我发现的有关这个问题的文章都集中在soapUI TestSuites上。我真的需要一个以MockService为中心的解决方案,或者至少能说明如何在MockServices中以不同的方式处理它,而不是TestSuites。
任何帮助都是非常感谢的。谢谢。马克。
GargantuChet提供的解决方案
以下是GargantuChet建议的更改,以解决尝试访问${ projectDir }属性并通过在groovy脚本范围内定义新的projectDir对象来启用相对路径的问题:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectDir = groovyUtils.projectPath
def req = new XmlSlurper().parseText(mockRequest.requestContent)
if (req =~ "CategoryA")
{
context.responsefile = new File(projectDir, "Test_Files/ID_List_CategoryA.xml").text
}
else
{
context.responsefile = new File(projectDir, "Test_Files/ID_List_CategoryB.xml").text
}发布于 2012-09-04 15:37:57
我不熟悉Groovy,但我假设File是一个普通的java.io.File实例。
相对路径被解释为相对于应用程序的当前目录。尝试下面这样的方法来验证:
def defaultPathBase = new File( "." ).getCanonicalPath()
println "Current dir:" + defaultPathBase如果这里是这样的话,那么您可能需要使用new File(String parent, String child)构造函数,将资源目录作为第一个参数传递,而相对路径作为第二个参数。
例如:
// hardcoded for demonstration purposes
def pathbase = "/Users/chet"
def content = new File(pathbase, "Desktop/sample.txt").text
println content下面是执行脚本的结果:
Chets-MacBook-Pro:Desktop chet$ groovy sample.groovy
This is a sample text file.
It will be displayed by a Groovy script.
Chets-MacBook-Pro:Desktop chet$ groovy sample.groovy
This is a sample text file.
It will be displayed by a Groovy script.
Chets-MacBook-Pro:Desktop chet$ 发布于 2013-03-07 20:05:30
您还可以执行以下操作以获得projectDir的值:
def projectDir = context.expand('${projectDir}');https://stackoverflow.com/questions/12266525
复制相似问题