Mar*_*ark 6 groovy soapui xmlslurper relative-path
我正在使用soapUI(3.6.1免费版)模拟服务来向我正在测试的2个客户端应用程序提供特定数据.使用一些简单的Groovy脚本,我设置了一些模拟操作,以根据客户端应用程序发出的请求从特定文件中获取响应.
模拟响应的静态内容是:
${responsefile}
Run Code Online (Sandbox Code Playgroud)
操作调度脚本窗格中的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
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,当客户端应用程序向包含字符串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
}
Run Code Online (Sandbox Code Playgroud)
...请记住,soapUI项目xml文件位于C:/ soapProject /
所以,这不起作用.我尝试过相对路径的变体:
一篇文章指出,soapUI可能会将项目文件父目录视为根目录的相对路径,因此也尝试了以下变体:
如果没有这个工作,我尝试在groovy脚本中使用$ {projectDir}属性,但是所有这些尝试都失败了"没有这样的属性:mockService for class:Script [n]"错误.Admittefly,当我试图这样做时,我真的在摸索.
我尝试使用这篇文章和其他人的信息:我如何使soapUI附件路径相对?
......没有运气.在该帖子的解决方案代码中用"mock"(以及其他更改)替换"test"导致更多属性错误,例如
testFile = new File(mockRunner.project.getPath())
Run Code Online (Sandbox Code Playgroud)
.. 导致...
No such property: mockRunner for class: Script3
Run Code Online (Sandbox Code Playgroud)
我发现与此问题相关的帖子都集中在soapUI TestSuites上.我真的需要一个以MockService为中心的解决方案,或者至少可以说明MockServices如何以不同的方式处理它而不是TestSuite.
任何帮助是极大的赞赏.谢谢.标记.
以下内容包括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
}
Run Code Online (Sandbox Code Playgroud)
我不熟悉Groovy,但我认为这File是一个正常的java.io.File例子.
相对路径被解释为相对于应用程序的当前目录.尝试以下内容来验证:
def defaultPathBase = new File( "." ).getCanonicalPath()
println "Current dir:" + defaultPathBase
Run Code Online (Sandbox Code Playgroud)
如果是这种情况,那么您可能希望使用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
Run Code Online (Sandbox Code Playgroud)
这是执行脚本的结果:
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$
Run Code Online (Sandbox Code Playgroud)
小智 6
您还可以完成以下操作以获取projectDir的值:
def projectDir = context.expand('${projectDir}');
Run Code Online (Sandbox Code Playgroud)