黄瓜放心:如何将请求信息放入HTML报告中

时间:2019-06-19 12:17:54

标签: cucumber rest-assured cucumber-java

我想在我的HTML报告中显示我的请求和响应详细信息。

功能文件示例:

Feature: Rest Assured under Cucumber POC

  Scenario: Azure Login Scenario
    Given Request specifications are set with base uri "https://login.microsoftonline.com/"
    When Azure Login Request Executed
    Then Verify Status Code is 200

Runner类是:

@RunWith(Cucumber.class)

@CucumberOptions(
        features = "src/main/resources/features",
        glue = {""},
        tags = "@tests",
        plugin = {      "pretty",
                        "json:target/cucumber-reports/Cucumber.json",
                        "html:target/cucumber-reports"}//reporting plugin
)
public class CucumberRunner {}

步骤是:

@Given("Request specifications are set with base uri {string}")
public void setRequestsSpec(String baseUri){

    RequestSpecification spec = new RequestSpecBuilder()
            .setBaseUri(baseUri)
            .addFilter(new ResponseLoggingFilter())//log request and response for better debugging. You can also only log if a requests fails.
            .addFilter(new RequestLoggingFilter())
            .build();

    testContext().setRequestSpec(spec);
}

@When("^Azure Login Request Executed$")
public void azureLoginExecuted() {

    response =
    given()  //Add x-www-form-urlencoded body params:
        .formParam(GRANT_TYPE_KEY, GRANT_TYPE_VALUE)
        .formParam(AUTO_TEAM_CLIENT_ID_KEY, AUTO_TEAM_CLIENT_ID_VALUE)
        .formParam(AUTO_TEAM_CLIENT_SECRET_KEY, AUTO_TEAM_CLIENT_SECRET_VALUE)
        .formParam(RESOURCE_KEY, RESOURCE_VALUE)
    .when()
        .post(AUTO_TEAM_TENANT_ID + RESOURCE); //Send the request along with the resource

    testContext().setResponse(response);

    setAuthorizationToken();
}

@Then("Verify Status Code is {int}")
public void verifyStatusCode(int expected_repsonse_code) {
    testContext().getResponse().then().statusCode(expected_repsonse_code);
}

当前,我发现了如何仅在IntelliJ控制台中显示这些详细信息:

例如:

@tests
Feature: Rest Assured under Cucumber POC

  @tests
  Scenario: Azure Login Scenario                                                            # src/main/resources/features/poc.feature:5
    Given Request specifications are set with base uri "https://login.microsoftonline.com/" # CommonStepsDefinitions.setRequestsSpec(String)
Request method: POST
Request URI:    https://login.microsoftonline.com/6ae4e000-b5d0-4f48-a766-402d46119b76/oauth2/token
Proxy:          <none>
Request params: <none>
Query params:   <none>

还有更多..

但是HTML报告仅显示:

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:1)

我可以为您提供一些可能无法完全回答您问题的细节。

要向Cucumber HTML报表添加数据,可以使用:

@After
public void addDataToReport(Scenario scenario) { //scenario is provided from Cucumber
    scenario.write(string with the information about scenario);
}

它不会被格式化,我也不知道如何更改报告的显示方式。每个消息都将在每个测试用例下。

您必须以某种方式将信息传递到@After挂钩。

我希望其他人会提供更多详细信息。

编辑:

为了存储有关当前或什至并行运行场景的信息,我们可以基于Thread创建一个类来存储必要的信息,因此它将是线程安全的。

让我们创建一个存储Scenario的类。我们称之为Storage

public class Storage {
    private static final HashMap<Thread, Scenario> map = new HashMap<>();

    public static void putScenario(Scenario scenario) {
        map.put(Thread.currentThread(), scenario);
    }

    public static Scenario getScenario() {
        return map.get(Thread.currentThread());
    }
}

现在,我们必须以某种方式获取Scenario。可以使用@Before这样的钩子来实现:

public class BeforeHook {

    @Before(order = 1)
    public void getScenario(Scenario scenario) {
        Storage.putScenario(scenario);
    }
}

@Before挂钩在每种情况之前运行。我们获得了有关方案的信息,并将其放入存储中,因此我们知道什么方案在什么线程上运行。 请记住,必须通过Cucumber Runner中的glue参数可以访问钩子!

现在,如果我们想向报告中写入其他信息:

    @Then("Data is saved to the report")
    public void data_is_saved_to_the_report() {
        System.out.println("Saving data to report");
        Storage.getScenario().write("Test data and stuff");
    }

我们只是从存储中获取当前方案,并使用Scenario.write()方法将信息添加到报告中。

在报告中将如下所示: enter image description here