【问题标题】:Integrate Selenium test results with TestRail 4.0将 Selenium 测试结果与 TestRail 4.0 集成
【发布时间】:2020-11-05 22:33:21
【问题描述】:

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

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

【问题讨论】:

    标签: selenium testrail


    【解决方案1】:

    基本思想是,您需要能够在给定用户的上下文中将您的结果链接回 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);
    

    【讨论】:

    • 嗨@UdayChitturi,你能给我这个代码吗?实际上它对我不起作用
    【解决方案2】:

    这应该可行。我们有 Jenkins 的测试基础架构,使用 Maven Styled Project 运行,TestNg 作为测试框架,Java 作为脚本语言。一旦将其部署在 Jenkins 上,它应该是带有参数的 PARAMETERIZED JOB,即根据 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();
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      相关资源
      最近更新 更多