集成Selenium测试结果与TestRail 4.0



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

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

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

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

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

就需要调用的API而言,您可以使用API方法"add_result"或"add_result_for_case"来完成此操作。这两个方法之间的关键区别在于"add_result_for_case"接受Case ID和Run ID,而"add_result"接受Test 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);

应该可以。我们有Jenkins的测试基础设施运行在Maven风格的项目中,TestNg作为测试框架,Java作为脚本语言。一旦它被部署在Jenkins上,它应该是参数化的JOB,即根据TestRail的API, project_id[必选],里程碑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();
    }
}

相关内容

  • 没有找到相关文章

最新更新