-->

我如何可以使用相对路径来进行的soapUI MockService外部响应文件(How can I

2019-07-31 08:27发布

我做了什么

我使用的soapUI(3.6.1免费版)模拟服务,具体数据为多达我测试2个客户端应用程序。 随着一些简单的Groovy脚本我已经建立了一些模拟操作来从基于客户端应用程序发出的请求特定文件的响应。

模拟响应的静态内容是:

${responsefile}

在操作调度脚本窗格中的常规是:

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 /

我已经试过到目前为止

所以,这是行不通的。 我试过相对路径的变化:

  • ./Test_Files/ID_List_CategoryA.xml
  • /Test_Files/ID_List_CategoryA.xml
  • Test_Files / ID_List_CategoryA.xml

一个职位表明的soapUI可能会考虑项目文件的父目录的根目录的相对路径的目的,所以尝试了以下变化太:

  • ./soapProject/Test_Files/ID_List_CategoryA.xml
  • /soapProject/Test_Files/ID_List_CategoryA.xml
  • soapProject / Test_Files / ID_List_CategoryA.xml

当这些方法都工作,我试图使利用$ {} PROJECTDIR财产的Groovy脚本中,但所有这些努力都失败了“没有这样的属性:mockService类:脚本[N]”的错误。 Admittefly,试图这样做,当我真的摸索周围。

我尝试使用这个职位和其他信息: 怎样使相对的soapUI附件路径?

......没有任何的运气。 更换“试验”和“模拟”,(其它变化之中),从该职位的解决方案代码导致更多属性的错误,例如,

testFile = new File(mockRunner.project.getPath())

.. 导致...

No such property: mockRunner for class: Script3

我想我需要

在帖子中,我已经找到了与此相关的问题都集中在soapUI的测试套件。 我真的需要一个解决方案,MockService中心或至少揭示了它如何为不同MockServices进行处理,而不是测试套件的一些情况。

任何帮助是极大的赞赏。 谢谢。 标记。

解决方案-通过提供GargantuChet

以下包括由建议的修改GargantuChet解决试图通过Groovy脚本的范围内定义一个新PROJECTDIR对象来访问$ {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
}

Answer 1:

我不熟悉使用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$ 


Answer 2:

你可能也做了以下获得PROJECTDIR的价值:

    def projectDir = context.expand('${projectDir}');


文章来源: How can I use relative paths to external response files for soapUI MockService