首页 > 解决方案 > 通过groovy脚本将测试结果从soapui传递给testlink

问题描述

我想使用 SoapUI 5.0 用 groovy 脚本连接到 testlink 服务器,然后将测试结果传递给测试链接

在我尝试这样做之前,我安装了从 GitHub ( https://github.com/kinow/testlink-java-api ) 下载的 TestLink Java API 库作为 jar 文件。

我将 testlink-java-api-1.9.17-1 archive.jar 复制到带有 SoapUI 的目录中,路径如下:\SoapUI\lib 和 \SoapUI\bin\ext

//here is my code from the groovy script test step

    import testlink.api.java.client.TestLinkAPIResults.*;
    import testlink.api.java.client.TestLinkAPIClient.*;

     def DEVKEY="2f404203b306bd8dd811a7f824c194d0";
     def  URL="http://172.29.0.73/testlink/lib/api/xmlrpc/v1/xmlrpc.php";

    TestLinkAPIClient api = new TestLinkAPIClient(DEVKEY, URL);

运行此脚本时,出现以下无法解析类错误

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script74.groovy: 7: unable to resolve class TestLinkAPIClient @ line 7, column 19. TestLinkAPIClient api = new TestLinkAPIClient(DEVKEY, URL); ^ org.codehaus.groovy.syntax.SyntaxException: unable to resolve class TestLinkAPIClient @ line 7, column 19. at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:146) at ....... 

在此处输入图像描述

在我的情况下,是否可以使用 SoapUI 中的 groovy 脚本连接到 testlink?谁能举例说明如何正确地做到这一点?

标签: groovysoapuitestlink

解决方案


也许有人会有用。事实证明,可以通过两种方式解决这个问题:

  1. 分析了测试链接手册中的示例后,我将以下代码添加到 Groovy 脚本测试步骤:

    import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.Hashtable;
    import java.util.Map;
    
    import org.apache.xmlrpc.XmlRpcException;
    import org.apache.xmlrpc.client.XmlRpcClient;
    import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
    def TranId= "TranId : " + testRunner.testCase.getPropertyValue( "TranId" )
    def  Response= "Response: "+ testRunner.testCase.getPropertyValue( "Response" )
    def  TL_extid = testRunner.testCase.getPropertyValue( "TL_extid" )
    def add_info = TranId +" " + Response;
    
    public class TestlinkAPIXMLRPCClient 
    {   
        // Substitute your Dev Key Here
        public static final String DEV_KEY =  "fcd38512b5c3e44befbc8b862e678894";
        // Substitute your Server URL Here
        public static final String SERVER_URL = "http://172.29.0.78/testlink/lib/api/xmlrpc/v1/xmlrpc.php"; 
    
        /**
         * report test execution to TestLink API
         * 
         * @param int tcid
         * @param int tpid
         * @param String status
         */
        public static void testLinkReport(String tcid, int tpid,int bid,String plname, String status, String notes)
        {
            try 
            {
                XmlRpcClient rpcClient;
                XmlRpcClientConfigImpl config;
    
                config = new XmlRpcClientConfigImpl();
                config.setServerURL(new URL(SERVER_URL));
                rpcClient = new XmlRpcClient();
                rpcClient.setConfig(config);        
    
                ArrayList<Object> params = new ArrayList<Object>();
                Hashtable<String, Object> executionData = new Hashtable<String, Object>();              
                executionData.put("devKey", DEV_KEY);
                executionData.put("testcaseexternalid", tcid);
                executionData.put("testplanid", tpid);
                executionData.put("buildid", bid);
                executionData.put("platformname", plname);
                executionData.put("status", status);
                executionData.put("notes", notes);
                params.add(executionData);
    
                Object[] result = (Object[]) rpcClient.execute("tl.reportTCResult", params);
    
                // Typically you'd want to validate the result here and probably do something more useful with it
                System.out.println("Result was:\n");                
                for (int i=0; i< result.length; i++)
                {
                    Map item = (Map)result[i];
                    System.out.println("Keys: " + item.keySet().toString() + " values: " + item.values().toString());
                }
            }
            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch (XmlRpcException e)
            {
                e.printStackTrace();
            }
        }
    }
        //Send to testlink:
        //TestCaseID tcid, TestLpanID  tpid, BuildId bid, PlatformName plname,  status  ,notes
    
        TestlinkAPIXMLRPCClient.testLinkReport("$TL_extid", 7367,238 ,"FLORAWARE", "p",  
        "Result from  GROOVY SOAPUI $add_info");
    
  2. 我认为最优雅的方式。

在soapui测试项目中,我通过在具有testlink服务器地址的资源路径中指定testlink api的路径来创建一个新的rest服务。像这样: TestLink 休息服务

然后添加以下用于将参数传递到测试链接的 REST 请求:

<methodCall>
   <methodName>tl.reportTCResult</methodName>
   <params>
      <param>
         <value>
            <struct>
               <member>
                  <name>devKey</name>
                  <value>${#TestSuite#DEV_KEY}</value>
               </member>
               <member>
                  <name>status</name>
                  <value>${TL_Properties#TestResult}</value>
               </member>
               <member>
                  <name>buildid</name>
                  <value>
                     <i4>${TL_Properties#LatestBuildID}</i4>
                  </value>
               </member>
               <member>
                  <name>platformname</name>
                  <value>FLORAWARE</value>
               </member>
               <member>
                  <name>notes</name>
                  <value>TEST RUN FROM SOAPUI Transaction ID: ${TL_Properties#TranID} Response: ${TL_Properties#Response}</value>
               </member>
               <member>
                  <name>testplanid</name>
                  <value>
                     <i4>7367</i4>
                  </value>
               </member>
               <member>
                  <name>testcaseexternalid</name>
                  <value>${#TestCase#TL_extid}</value>
               </member>
                   <member>
                  <name>execduration</name>
                  <value>1</value>
               </member>
            </struct>
         </value>
      </param>
   </params>
</methodCall>

推荐阅读