我想在我的 HTML 报告中显示我的请求和响应详细信息.
I would like to show my requests and responses details in my HTML report.
功能文件示例:
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 控制台中显示这些详细信息:
Currently I found out how to show those details only in my IntelliJ console:
例如:
@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 报告只显示:
But the HTML report shows only:
谢谢!
我可以给你一些细节,可能无法完全回答你的问题.
I can give you some details which might not answer your question fully.
为了向 Cucumber HTML 报告添加数据,您可以使用:
In order to add data to Cucumber HTML report you can use:
@After
public void addDataToReport(Scenario scenario) { //scenario is provided from Cucumber
scenario.write(string with the information about scenario);
}
它不会被格式化,我不知道如何更改报告的显示方式.每条消息都将位于每个测试用例下.
It won't be formatted and I don't know how to change how the report displays it. Each of the messages will be under each Test Case.
您必须以某种方式将信息传递给 @After
挂钩.
You have to, somehow, pass the information to @After
hook.
希望其他人能详细回答一下.
I hope someone else will answer with more details.
为了存储有关当前正在运行的场景的信息,甚至并行存储,我们可以基于线程创建一个类来存储必要的信息,因此它将是线程安全的.
In order to store the info about what Scenario is running at the moment, or even in parallel, we can create a class to store necessary information, based on the Thread, so it will be Thread-safe.
让我们创建一个类来存储Scenario
.我们称之为Storage
Let's create a class to store Scenario
. Let's call it 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
钩子来实现:
Now, we have to somehow get the Scenario
. It can be achieved by using @Before
hook like this:
public class BeforeHook {
@Before(order = 1)
public void getScenario(Scenario scenario) {
Storage.putScenario(scenario);
}
}
@Before
钩子在每个场景之前运行.我们得到关于 Scenario 的信息并将其放入 Storage 中,这样我们就知道什么 Scenario 在什么 Thread 上运行.请记住,钩子必须可以通过 Cucumber Runner 中的 glue
参数访问!
@Before
hooks are run before each scenario. We get the information about Scenario and put it in Storage so we know what Scenario is run on what Thread.
Remember that hooks have to be reachable by the glue
parameter in Cucumber Runner!
现在,如果我们想在报告中写入更多信息:
And now, if we want to write additional information to the report:
@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");
}
我们只是从 Storage 中获取当前场景并使用 Scenario.write()
方法将信息添加到报告中.
We just get current scenario from the Storage and use Scenario.write()
method to add information to the report.
它在报告中将如下所示:
It will look like this in the report: