简体   繁体   English

如何使用Groovy脚本获取断言值

[英]How to get assertion value using groovy script

I have one test step which contains two assertion. 我有一个包含两个断言的测试步骤。

  • Not SOAP Fault 不是SOAP错误
  • Contains. 包含。 The Condition is that response should contain "Message Sent Successfully" 条件是响应应包含“消息发送成功”

Now I have one groovy script, from where I am executing this test step. 现在,我有一个普通的脚本,可以在其中执行此测试步骤。 Using this groovy script I need to print assertion name, Value and Status. 使用此常规脚本,我需要打印断言名称,值和状态。 Below is the code I have written: 以下是我编写的代码:

testStepSrc = testCase.getTestStepByName(testName)
Assertioncounter = testStepSrc.getAssertionList().size()
for (AssertionCount in 0..Assertioncounter-1)
{
log.info("Assertion :" + testStepSrc.getAssertionAt(AssertionCount).getName() + " :: " + testStepSrc.getAssertionAt(AssertionCount).getStatus())

error = testStepSrc.getAssertionAt(AssertionCount).getErrors()
if (error != null)
   {
    log.error(error[0].getMessage())
   }
 }

but in output it is displaying like: 但在输出中显示为:

Wed Sep 04 17:21:11 IST 2013:INFO:Assertion :Not SOAP Fault :: VALID
Wed Sep 04 17:21:11 IST 2013:INFO:Assertion :Contains :: VALID

As you can see, I am able to print assertion name and status but not the value of 'Contains' assertion. 如您所见,我能够打印断言的名称和状态,但不能打印“包含”断言的值。 Please help me how to get the value of a particular assertion. 请帮助我如何获取特定断言的值。

Thanks in advance. 提前致谢。

So here is some things for you to read 所以这里有一些东西供您阅读

and what i tried 和我尝试过的

def assertionsList = testRunner.getTestCase().getTestStepByName("Test Step Name").getAssertionList()
for( e in assertionsList){
    log.info e.getToken() //gives the value of the content to search for
    log.info e.DESCRIPTION
    log.info e.ID
    log.info e.LABEL
    log.info e.toString()
}

This gives the following output 这给出了以下输出

Wed Sep 04 15:12:19 ADT 2013:INFO:Abhishek //the contains assertion was checking for the word "Abhishek" in the response of my test step where the assertion was applied.
Wed Sep 04 15:12:19 ADT 2013:INFO:Searches for the existence of a string token in the property value, supports regular expressions. Applicable to any property. 
Wed Sep 04 15:12:19 ADT 2013:INFO:Simple Contains
Wed Sep 04 15:12:19 ADT 2013:INFO:Contains
Wed Sep 04 15:12:19 ADT 2013:INFO:com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SimpleContainsAssertion@c4115f0

Abhishek's response did contain you answer I believe but just not in the format you were looking for. 我相信Abhishek的回答确实包含您的答案,但并非以您想要的格式。

I was looking for the same info for custom reporting and after digging through The SoapUI forms I stumbled upon this. 我一直在寻找用于自定义报告的相同信息,并且在深入研究SoapUI表单之后,我偶然发现了这一点。

The piece of code that I believe you are looking for is: 我相信您正在寻找的代码段是:

log.info e.getToken()

however this is an example of how to retrieve it only when an error occurs but you can get it in a valid scenario using something similar to: 但是,这是一个仅在发生错误时如何检索它的示例,但您可以在有效的情况下使用类似于以下内容的方法获取它:

def iAssertionName = assertionNameList[j]
def iAssertionStatus = testStep.getAssertionAt(j).getStatus().toString()
def tstep = testStep.getName()
def gStatus =  testStep.getAssertionAt(j).status
def expect = testStep.getAssertionAt(j).getToken()
log.info "Expected Content: " + expect

This is a subset of my code but produces the log message: 这是我的代码的一个子集,但会生成日志消息:

Fri Sep 20 11:04:09 CDT 2013:INFO:Expected Content: success

My SoapUI script assertion was checking to see if my response contained the string "success". 我的SoapUI脚本断言正在检查我的响应是否包含字符串“成功”。

Thanks Abhishek for your response! 感谢Abhishek的回复!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM