4

我正在研究 Selenium 测试自动化。我创建了我的 Selenium 测试套件来自动化我的测试套件。现在我想将 Selenium 结果与 TestRail 集成。我不确定如何将 Selenium 测试运行的结果集成到 TestRail 测试套件。我用java写了所有的测试用例。我现在被困住了。举个例子对我有帮助。

我正在使用 testng 框架,Maven 构建工具。

4

2 回答 2

6

基本思想是,您需要能够在给定用户的上下文中将结果链接回 TestRail 中的唯一测试 ID。这可以在每个测试执行并通过/失败时完成,也可以在整个运行完成后完成。

如果您想在每个测试通过/失败后将结果推送到 TestRail,您可以创建一个TestNG 侦听器来侦听测试结果,然后调用 API 将结果提交到 TestRail。这种方法比在每个测试中添加一个函数要干净得多。

如果您想在运行完成后将结果推送到 TestRail,您可能必须编写一个解析器来读取/处理整个结果文件,然后适当地调用TestRail API

对于您需要调用的 API,您可以使用 API 方法“add_result”或“add_result_for_case”来执行此操作。这两种方法的主要区别在于“add_result_for_case”采用案例 ID 和运行 ID,而“add_result”采用测试 ID。根据您的自动化方法,两者都可能有用。

有一个 Java API 绑定可用:

https://github.com/gurock/testrail-api

在此处记录。

您可以通过以下方式在 Java 中实例化 API 连接:

import com.gurock.testrail.APIClient;
import com.gurock.testrail.APIException;
import java.util.Map;
import java.util.HashMap;
import org.json.simple.JSONObject;

public class Program
{
    public static void main(String[] args) throws Exception
    {
        APIClient client = new APIClient("http://<server>/testrail/");
        client.setUser("..");
        client.setPassword("..");
    }
}

下面是一个 GET 请求的示例:

APIClient client = new APIClient("http://<server>/testrail/");
client.setUser("..");
client.setPassword("..");
JSONObject c = (JSONObject) client.sendGet("get_case/1");
System.out.println(c.get("title"));

这是一个 POST 请求:

Map data = new HashMap();
data.put("status_id", new Integer(1));
data.put("comment", "This test worked fine!");
JSONObject r = (JSONObject) client.sendPost("add_result_for_case/1/1", data);
于 2015-07-12T22:59:20.623 回答
0

这应该有效。我们有 Jenkins 的测试基础设施,使用 Maven Styled Project 运行,TestNg 作为测试框架,Java 作为脚本语言。一旦将其部署在 Jenkins 上,它应该是参数化作业,参数 iePROJECT_ID [强制],MILESTONE_ID [可选] 根据 TestRail 的 API。

              ***********PageBase Class [Generic methods]***********


public static int TEST_RUN_ID;
public static String TESTRAIL_USERNAME = "atul.xxma@xxx.com";
public static String TESTRAIL_PASSWORD = "oXafTubi/wsM7KZhih73-ZZ38v";
public static String RAILS_ENGINE_URL = "https://xxxx.testrail.io/";
public static final int TEST_CASE_PASSED_STATUS = 1;
public static final int TEST_CASE_FAILED_STATUS = 5;
public static APIClient client = new APIClient(RAILS_ENGINE_URL);




 @BeforeSuite()  //TestRail API
 public void connectAndCreateRunTestRail() throws MalformedURLException, IOException, 
       APIException {
    System.out.println("before suite: connectAndCreateRunTestRail");

    String project_id = System.getProperty("PROJECT_ID"); // jenkins parameter
    String milestone_id = System.getProperty("MILESTONE_ID"); // jenkins parameter

    client.setUser(TESTRAIL_USERNAME);
    client.setPassword(TESTRAIL_PASSWORD);

    Map data = new HashMap();
    data.put("suite_id", 1); // default
    data.put("name", "Test Run:");
    data.put("description", "Desc:XXXE");
    data.put("milestone_id", milestone_id);
    data.put("assignedto_id", 6); // User ID
    data.put("include_all", false); // set to false as need to select required TCs.

    int[] arrCaseIds = { 45, 93, 94, 97, 96, 99, 174 };
    List<Object> lstCaseIds = 
               Arrays.stream(arrCaseIds).boxed().collect(Collectors.toList());

    data.put("case_ids", lstCaseIds);
    data.put("refs", "Ref:Regression Suite");

    System.out.println(data.toString());

    // Post with URL and payload in Map format.
    Object response_Post_addRun = client.sendPost("add_run/" + project_id, data);
    System.out.println("Response of response_Post-AddRun-->" + 
      response_Post_addRun.toString());

    JSONObject obj = new JSONObject(response_Post_addRun.toString());
    int run_Id = obj.getInt("id");
    TEST_RUN_ID = run_Id;
    System.out.println("Added Run ID -->" + run_Id);

}


   public static void addResultForTestCase(String testCaseId, int status, String 
        screenshotPath) throws IOException, APIException, ParseException {

    client.setUser(TESTRAIL_USERNAME);
    client.setPassword(TESTRAIL_PASSWORD);

    HashMap data = new HashMap();
    data.put("status_id", status);
    data.put("comment", "Executed Selenium TestFramework.");
    Object response_Post_add_result_for_case = client
            .sendPost("add_result_for_case/" + TEST_RUN_ID + "/" + testCaseId + "", 
   data);

    System.out.println("response_Post-->" + 
    response_Post_add_result_for_case.toString());

    JSONObject obj = new JSONObject(response_Post_add_result_for_case.toString());

    int result_id = obj.getInt("id");
    System.out.println("result_id->" + result_id);
    System.out.println("screenshotPath-->" + screenshotPath);
    client.sendPost("add_attachment_to_result/" + result_id, screenshotPath);

}

                    ***********Reporting***********


@Override
public void onTestSuccess(ITestResult tr) {

    System.out.println("onTestSuccess");

    String screenshotPath = ".//" + screenshotName + ".png";
    File screenshotTRPath = new File(System.getProperty("user.dir") + "/Reports/" + 
         screenshotName + ".png");

    System.out.println("screenshotPath-->" + screenshotPath);
    System.out.println("screenshotTRPath-->" + screenshotTRPath.toString());
    ///TestRail API
    try {
        addResultForTestCase(currentTestCaseId, TEST_CASE_PASSED_STATUS, 
     screenshotTRPath.toString());
    } catch (IOException | APIException | ParseException e) {  e.printStackTrace();

    }

}
于 2020-10-20T20:45:26.397 回答