将Selenium测试结果与TestRail 4.0集成

时间:2015-07-10 15:20:04

标签: selenium testrail

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

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

2 个答案:

答案 0 :(得分:6)

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

如果要在每次测试通过/失败后将结果推送到TestRail,您可以创建一个TestNG listener来监听测试结果,然后调用API将结果提交给TestRail。这种方法比为每个测试添加一个函数要清晰得多。

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

就您需要调用的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

记录在案here

您通过以下方式在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);

答案 1 :(得分:0)

这应该有效。我们拥有运行Maven风格项目的Jenkins的测试基础结构,其中TestNg为测试框架,Java为脚本语言。一旦将其部署在Jenkins上,就应该按照TestRail的API将其参数化,即参数PROJECT_ID [必选],MILESTONE_ID [可选]。

              ***********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();

    }

}