Jacobvu84 / serenity-pageobject-junit-webdriver

4 stars 1 forks source link

Cách đẩy kết quả test lên testrail #62

Open Jacobvu84 opened 4 years ago

Jacobvu84 commented 4 years ago

http://docs.gurock.com/testrail-api2/start

Jacobvu84 commented 4 years ago

add_result_for_case

http://docs.gurock.com/testrail-api2/reference-results#add_result

Jacobvu84 commented 4 years ago
private void initBuilder() {
        if (this.apiUrl == "") {
            throw new RuntimeException("Service url is empty");
        }
        Client client = JerseyClientBuilder.newBuilder().build();
        client.register(new LoggingFeature(Logger.getGlobal(), Level.INFO, null, null));
        client.property(ClientProperties.FOLLOW_REDIRECTS, Boolean.FALSE);
        client.property(LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT, LoggingFeature.Verbosity.PAYLOAD_ANY);
        client.register(JacksonFeature.class);
        client.register(MultiPartFeature.class);
        client.register(authenticate);
        WebTarget resource = client.target(apiUrl);
        this.request = resource.request();
        this.request.accept(MediaType.APPLICATION_JSON);
    }
Jacobvu84 commented 4 years ago

Authentication

public HttpAuthenticationFeature setBasicHttpAuthenticationFeature(String strUsername, String strPassword) {
        authenticate = HttpAuthenticationFeature.basic(strUsername, strPassword);
        return authenticate;
    }
Jacobvu84 commented 4 years ago

AddResultForCaseRequest

public AddResultForCase(String caseID) {
        this.setServiceUrl("https://" + this.endpoint + "/" + addResultForCaseApi + "/" + runID + "/" + caseID);
        this.setBasicHttpAuthenticationFeature(username, password);
    }
Jacobvu84 commented 4 years ago

SetTestCaseResultOnTestRail.send()

try {
            ObjectMapper mapper = new ObjectMapper();
            Response response = requestForCase
                    .post(Entity.entity(mapper.writeValueAsString(jsonBody), MediaType.APPLICATION_JSON));
            if (response.getStatus() == 200) {
                System.out.println("Success: {" + response.readEntity(String.class) + "}");
            } else {
                System.out.println("Failed: {" + response.readEntity(String.class) + "}");
            }
        } catch (JsonProcessingException e) {
            System.out.println("Cannot execute service api " + requestForCase.getServiceUrl());
            e.printStackTrace();
    }
Jacobvu84 commented 4 years ago

SetTestCaseResultOnTestRail.perform()

String testCaseStatus = report.getResult();
        switch (testCaseStatus) {
            case "SUCCESS":
                jsonBody.put("status_id", TestRailResult.PASS.getStatus());
                jsonBody.put("comment", "Functions work well");
                break;

            case "FAILURE":
                StringBuilder commentBuiderFailedCase = new StringBuilder("This test case is FAILED\n\n\n");
                List<ReportStep> lstReportSteps = report.getSteps();
                for (ReportStep step : lstReportSteps) {
                    if (step.getResult().equals("FAILURE")) {
                        commentBuiderFailedCase.append("At step \"" + step.getDescription() + "\", By reason:");
                        commentBuiderFailedCase.append("" + step.getExceptionObject().getMessage() + "\n");
                        List<ReportStepScreenshot> listScreenshots = step.getScreenshots();
                        String screenshotBefore = System.getProperty("user.dir") + "/target/site/serenity/"
                                + listScreenshots.get(0).getScreenshot();
                        String screenshotAfter = System.getProperty("user.dir") + "/target/site/serenity/"
                                + listScreenshots.get(1).getScreenshot();
                        commentBuiderFailedCase.append(
                                "\nScreenshot captured before this step \"" + screenshotBefore + "\"\n");
                        commentBuiderFailedCase
                                .append("\nScreenshot captured after this step \"" + screenshotAfter + "\"\n");
                        commentBuiderFailedCase.append("\n------------------------------------------------\n");
                    }
                }
                commentBuiderFailedCase.append("\n\nend of comment");
                jsonBody.put("status_id", TestRailResult.FAILED.getStatus());
                jsonBody.put("comment", commentBuiderFailedCase.toString());
                break;

            case "ERROR":

                StringBuilder commentBuiderBlockedCase = new StringBuilder("This test case is BLOCKED (cannot finish execution)\n\n\n");
                commentBuiderBlockedCase.append("Because of this error " + report.getTestFailureSummary());
                commentBuiderBlockedCase.append("\n\nend of comment");
                jsonBody.put("status_id", TestRailResult.BLOCKED.getStatus());
                jsonBody.put("comment", commentBuiderBlockedCase.toString());
                break;