如何使用 groovy 在 testSuite TearDown 中捕获请求和响应

How to Capture Request and Response in testSuite TearDown using groovy

我有一个测试套件,假设有 5 个案例,我 运行 来自我的测试套件的案例。

在我的拆卸脚本中,我想捕获所有测试用例所有测试步骤的所有请求和响应。

下面是我在 tearDown TestSuite 中编写的代码,context.expand 中的问题是返回空。我假设需要 testCase 上下文,或者不确定我哪里出错了。

tc_list = testSuite.getTestCaseList()
tc_count = tc_list.size()
for(i=0;i<tc_list.size();i++){
    if(!tc_list[i].isDisabled()){

        ts_list =  tc_list[i].getTestStepList()

        for(j=0;j<ts_list.size();j++){
            req = testSuite.getPropertyValue("reportpath")+'/'+testSuite.getName()+'/'+tc_list[i].getName()+'/'+ts_list[j].getName()+'_RequestData.txt'
            res = testSuite.getPropertyValue("reportpath")+'/'+testSuite.getName()+'/'+tc_list[i].getName()+'/'+ts_list[j].getName()+'_ResponseData.txt'

            def request_expand = context.expand('${'+ts_list[j].getName()+'#Request}')
            log.info '${'+ts_list[j].getName()+'#Response}'+tc_list[i].getName()
               def response_expand = context.expand('${'+ts_list[j].getName()+'#Response}')
              log.info response_expand



             /* def req_file = new File(req)
               req_file.write(request_expand,"UTF-8")
               def res_file = new File(res)
               res_file.write(response_expand,"UTF-8") */



            }
        }


    }

下面的代码对我有用。但是我还在尝试抓取rawRequest和rawResponse,还是没有实现

tc_list = testSuite.getTestCaseList()
tc_count = tc_list.size()
for(i=0;i<tc_list.size();i++){
    if(!tc_list[i].isDisabled()){

        ts_list =  tc_list[i].getTestStepList()

        for(j=0;j<ts_list.size();j++){

            req = testSuite.getPropertyValue("reportpath")+'/'+testSuite.getName()+'/'+tc_list[i].getName()+'/'+ts_list[j].getName()+'_RequestData.txt'
            res = testSuite.getPropertyValue("reportpath")+'/'+testSuite.getName()+'/'+tc_list[i].getName()+'/'+ts_list[j].getName()+'_ResponseData.txt'

            def request_expand = testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getPropertyValue("Request")
            def response_expand = testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getPropertyValue("Response")
            def req_file = new File(req)
            def res_file = new File(res)
            log.info testSuite.getTestCaseByName(tc_list[i].getName()).getName()+' '+testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getName()+' '+response_expand
            if(request_expand!=null && response_expand!=null){

                 log.info testSuite.getTestCaseByName(tc_list[i].getName()).getName()+' '+testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getName()+' '+response_expand

               req_file.write(request_expand,"UTF-8")

               res_file.write(response_expand,"UTF-8")
                }

            }
        }


    }

@Ragesh kr

无论何时在 Soap ui 或 Ready API 中,您需要 RawRequestRawResponse

您可以将 Request 替换为 RawRequest 和响应 RawResponse

我刚刚在你的代码中做了并且它有效

        def request_expand = testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getPropertyValue("RawRequest")
        def response_expand = testSuite.getTestCaseByName(tc_list[i].getName()).getTestStepByName(ts_list[j].getName()).getPropertyValue("RawResponse")

其他一些例子对大家有帮助

当我们只需要在soap中请求和响应ui我们可以使用下面

当我们只需要 soapui/ReadyaPI 中的 RawRequest 和 RawResponse 通过 groovy 我们可以使用下面的

 req=context.expand('${RequestStepName#RawRequest}')
 log.info req
 res=context.expand('${RequestStepName#RawResponse}')
 log.info res