通过 Groovy 脚本将测试结果从 Soapui 传递给 TestLink



我想使用 SoapUI 5.0 用一个时髦的脚本连接到测试链接服务器,然后将测试结果传递给测试链接

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

我将testlink-java-api-1.9.17-1存档.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 中的时髦脚本连接到测试链接?谁能举个例子来说明如何正确做到这一点?

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

  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 服务。喜欢这个:测试链接休息服务

然后添加了以下 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>

您必须像这样动态地导入库:

this.getClass().classLoader.addURL(new File(context.expand(project.resourceRoot) + "/libs/testlink.jar").toURL());

或者更好的是,将所有依赖项放在一个 jar 文件中并使用该脚本行。您可以使用类的修饰名称:

testlink.api.java.client.TestLinkAPIClient api = new testlink.api.java.client.TestLinkAPIClient(DEVKEY, URL);

这大约是90%的正确时间。

由于Groovy和SoapUI不一致,您可能需要使用两个步骤:

  1. 动态导入库
  2. 使用 TestLinkAPIClient。

加载类后,该错误将不再显示。

最新更新