Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cucumber-jvm] Add attachments to allure reports using cucumber api #339

Merged
merged 7 commits into from
Apr 15, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
Expand Down Expand Up @@ -66,6 +68,9 @@ public class AllureCucumberJvm implements Reporter, Formatter {
private static final String SKIPPED = "skipped";
private static final String PENDING = "pending";

private static final String TXT_EXTENSION = ".txt";
private static final String TEXT_PLAIN = "text/plain";

private final Map<Scenario, String> scenarioUuids = new ConcurrentHashMap<>();
private final Deque<Step> gherkinSteps = new LinkedList<>();
private final AllureLifecycle lifecycle;
Expand Down Expand Up @@ -250,12 +255,17 @@ private void createDataTableAttachment(final List<DataTableRow> dataTableRows) {

@Override
public void embedding(final String string, final byte[] bytes) {
//Nothing to do with Allure
lifecycle.addAttachment("Screenshot", null, null, new ByteArrayInputStream(bytes));
}

@Override
public void write(final String string) {
//Nothing to do with Allure
lifecycle.addAttachment(
"Text output",
TEXT_PLAIN,
TXT_EXTENSION,
Objects.toString(string).getBytes(StandardCharsets.UTF_8)
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,33 @@ void shouldAddDataTableAttachment() {

}

@AllureFeatures.Attachments
@Test
void shouldAddAttachments() {
final AllureResults results = runFeature("features/attachments.feature");

final List<Attachment> attachments = results.getTestResults().stream()
.map(TestResult::getSteps)
.flatMap(Collection::stream)
.map(StepResult::getAttachments)
.flatMap(Collection::stream)
.collect(Collectors.toList());

assertThat(attachments)
.extracting(Attachment::getName, Attachment::getType)
.containsExactlyInAnyOrder(
tuple("Text output", "text/plain"),
tuple("Screenshot", null)
);

final List<String> attachmentContents = results.getAttachments().values().stream()
.map(bytes -> new String(bytes, StandardCharsets.UTF_8))
.collect(Collectors.toList());

assertThat(attachmentContents)
.containsExactlyInAnyOrder("text attachment", "image attachment");
}

@AllureFeatures.Steps
@Disabled("unsupported")
@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2019 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.cucumberjvm.samples;

import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;

public class AttachmentSteps
{
private Scenario scenario;

@Before("@attachments")
public void setup(Scenario scenario)
{
this.scenario = scenario;
}

@Given("step with scenario write")
public void stepWithScenarioWrite()
{
scenario.write("text attachment");
}

@Given("step with scenario embed")
public void stepWithScenarioEmbed()
{
scenario.embed("image attachment".getBytes(), "image/png");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@attachments
Feature: Feature with attachments

Scenario: Simple scenario with a text attachment
Given step with scenario write

Scenario: Simple scenario with an image attachment
Given step with scenario embed
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import cucumber.api.event.TestSourceRead;
import cucumber.api.event.TestStepFinished;
import cucumber.api.event.TestStepStarted;
import cucumber.api.event.WriteEvent;
import cucumber.api.event.EmbedEvent;
import cucumber.api.formatter.Formatter;
import cucumber.runner.UnskipableStep;
import gherkin.ast.Examples;
Expand All @@ -49,6 +51,8 @@

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
Expand Down Expand Up @@ -89,6 +93,11 @@ public class AllureCucumber2Jvm implements Formatter {
private final EventHandler<TestCaseFinished> caseFinishedHandler = this::handleTestCaseFinished;
private final EventHandler<TestStepStarted> stepStartedHandler = this::handleTestStepStarted;
private final EventHandler<TestStepFinished> stepFinishedHandler = this::handleTestStepFinished;
private final EventHandler<WriteEvent> writeEventHandler = this::handleWriteEvent;
private final EventHandler<EmbedEvent> embedEventHandler = this::handleEmbedEvent;

private static final String TXT_EXTENSION = ".txt";
private static final String TEXT_PLAIN = "text/plain";

@SuppressWarnings("unused")
public AllureCucumber2Jvm() {
Expand All @@ -108,6 +117,9 @@ public void setEventPublisher(final EventPublisher publisher) {

publisher.registerHandlerFor(TestStepStarted.class, stepStartedHandler);
publisher.registerHandlerFor(TestStepFinished.class, stepFinishedHandler);

publisher.registerHandlerFor(WriteEvent.class, writeEventHandler);
publisher.registerHandlerFor(EmbedEvent.class, embedEventHandler);
}

/*
Expand Down Expand Up @@ -220,6 +232,19 @@ private void handleTestStepFinished(final TestStepFinished event) {
}
}

private void handleWriteEvent(final WriteEvent event) {
lifecycle.addAttachment(
"Text output",
TEXT_PLAIN,
TXT_EXTENSION,
Objects.toString(event.text).getBytes(StandardCharsets.UTF_8)
);
}

private void handleEmbedEvent(final EmbedEvent event) {
lifecycle.addAttachment("Screenshot", null, null, new ByteArrayInputStream(event.data));
}

/*
Utility Methods
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,33 @@ void shouldAddDataTableAttachment() {

}

@AllureFeatures.Attachments
@Test
void shouldAddAttachments() {
final AllureResults results = runFeature("features/attachments.feature");

final List<Attachment> attachments = results.getTestResults().stream()
.map(TestResult::getSteps)
.flatMap(Collection::stream)
.map(StepResult::getAttachments)
.flatMap(Collection::stream)
.collect(Collectors.toList());

assertThat(attachments)
.extracting(Attachment::getName, Attachment::getType)
.containsExactlyInAnyOrder(
tuple("Text output", "text/plain"),
tuple("Screenshot", null)
);

final List<String> attachmentContents = results.getAttachments().values().stream()
.map(bytes -> new String(bytes, StandardCharsets.UTF_8))
.collect(Collectors.toList());

assertThat(attachmentContents)
.containsExactlyInAnyOrder("text attachment", "image attachment");
}

@AllureFeatures.Steps
@Test
void shouldAddBackgroundSteps() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2019 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.cucumber2jvm.samples;

import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;

public class AttachmentSteps
{
private Scenario scenario;

@Before("@attachments")
public void setup(Scenario scenario)
{
this.scenario = scenario;
}

@Given("step with scenario write")
public void stepWithScenarioWrite()
{
scenario.write("text attachment");
}

@Given("step with scenario embed")
public void stepWithScenarioEmbed()
{
scenario.embed("image attachment".getBytes(), "image/png");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@attachments
Feature: Feature with attachments

Scenario: Simple scenario with a text attachment
Given step with scenario write

Scenario: Simple scenario with an image attachment
Given step with scenario embed
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import cucumber.api.event.TestSourceRead;
import cucumber.api.event.TestStepFinished;
import cucumber.api.event.TestStepStarted;
import cucumber.api.event.WriteEvent;
import cucumber.api.event.EmbedEvent;
import cucumber.api.formatter.Formatter;
import gherkin.ast.Examples;
import gherkin.ast.Feature;
Expand All @@ -50,6 +52,8 @@

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
Expand Down Expand Up @@ -91,6 +95,11 @@ public class AllureCucumber3Jvm implements Formatter {
private final EventHandler<TestCaseFinished> caseFinishedHandler = this::handleTestCaseFinished;
private final EventHandler<TestStepStarted> stepStartedHandler = this::handleTestStepStarted;
private final EventHandler<TestStepFinished> stepFinishedHandler = this::handleTestStepFinished;
private final EventHandler<WriteEvent> writeEventHandler = this::handleWriteEvent;
private final EventHandler<EmbedEvent> embedEventHandler = this::handleEmbedEvent;

private static final String TXT_EXTENSION = ".txt";
private static final String TEXT_PLAIN = "text/plain";

@SuppressWarnings("unused")
public AllureCucumber3Jvm() {
Expand All @@ -110,6 +119,9 @@ public void setEventPublisher(final EventPublisher publisher) {

publisher.registerHandlerFor(TestStepStarted.class, stepStartedHandler);
publisher.registerHandlerFor(TestStepFinished.class, stepFinishedHandler);

publisher.registerHandlerFor(WriteEvent.class, writeEventHandler);
publisher.registerHandlerFor(EmbedEvent.class, embedEventHandler);
}

/*
Expand Down Expand Up @@ -223,6 +235,19 @@ private void handleTestStepFinished(final TestStepFinished event) {
}
}

private void handleWriteEvent(final WriteEvent event) {
lifecycle.addAttachment(
"Text output",
TEXT_PLAIN,
TXT_EXTENSION,
Objects.toString(event.text).getBytes(StandardCharsets.UTF_8)
);
}

private void handleEmbedEvent(final EmbedEvent event) {
lifecycle.addAttachment("Screenshot", null, null, new ByteArrayInputStream(event.data));
}

/*
Utility Methods
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,33 @@ void shouldAddDataTableAttachment() {

}

@AllureFeatures.Attachments
@Test
void shouldAddAttachments() {
final AllureResults results = runFeature("features/attachments.feature");

final List<Attachment> attachments = results.getTestResults().stream()
.map(TestResult::getSteps)
.flatMap(Collection::stream)
.map(StepResult::getAttachments)
.flatMap(Collection::stream)
.collect(Collectors.toList());

assertThat(attachments)
.extracting(Attachment::getName, Attachment::getType)
.containsExactlyInAnyOrder(
tuple("Text output", "text/plain"),
tuple("Screenshot", null)
);

final List<String> attachmentContents = results.getAttachments().values().stream()
.map(bytes -> new String(bytes, StandardCharsets.UTF_8))
.collect(Collectors.toList());

assertThat(attachmentContents)
.containsExactlyInAnyOrder("text attachment", "image attachment");
}

@AllureFeatures.Steps
@Test
void shouldAddBackgroundSteps() {
Expand Down
Loading