-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathAllureSelenide.java
128 lines (113 loc) · 4.58 KB
/
AllureSelenide.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* 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.selenide;
import com.codeborne.selenide.WebDriverRunner;
import com.codeborne.selenide.logevents.LogEvent;
import com.codeborne.selenide.logevents.LogEventListener;
import io.qameta.allure.Allure;
import io.qameta.allure.AllureLifecycle;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StatusDetails;
import io.qameta.allure.model.StepResult;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.UUID;
import static io.qameta.allure.util.ResultsUtils.getStatus;
import static io.qameta.allure.util.ResultsUtils.getStatusDetails;
/**
* @author Artem Eroshenko.
*/
@SuppressWarnings("unused")
public class AllureSelenide implements LogEventListener {
private static final Logger LOGGER = LoggerFactory.getLogger(AllureSelenide.class);
private boolean saveScreenshots = true;
private boolean savePageHtml = true;
private final AllureLifecycle lifecycle;
public AllureSelenide() {
this(Allure.getLifecycle());
}
public AllureSelenide(final AllureLifecycle lifecycle) {
this.lifecycle = lifecycle;
}
public AllureSelenide screenshots(final boolean saveScreenshots) {
this.saveScreenshots = saveScreenshots;
return this;
}
public AllureSelenide savePageSource(final boolean savePageHtml) {
this.savePageHtml = savePageHtml;
return this;
}
private static Optional<byte[]> getScreenshotBytes() {
try {
return Optional.of((TakesScreenshot) WebDriverRunner.getWebDriver())
.map(wd -> wd.getScreenshotAs(OutputType.BYTES));
} catch (WebDriverException e) {
LOGGER.warn("Could not get screen shot", e);
return Optional.empty();
}
}
private static Optional<byte[]> getPageSourceBytes() {
try {
return Optional.of(WebDriverRunner.getWebDriver())
.map(WebDriver::getPageSource)
.map(ps -> ps.getBytes(StandardCharsets.UTF_8));
} catch (WebDriverException e) {
LOGGER.warn("Could not get page source", e);
return Optional.empty();
}
}
@Override
public void beforeEvent(final LogEvent event) {
lifecycle.getCurrentTestCaseOrStep().ifPresent(parentUuid -> {
final String uuid = UUID.randomUUID().toString();
lifecycle.startStep(parentUuid, uuid, new StepResult().setName(event.toString()));
});
}
@Override
public void afterEvent(final LogEvent event) {
lifecycle.getCurrentTestCaseOrStep().ifPresent(parentUuid -> {
switch (event.getStatus()) {
case PASS:
lifecycle.updateStep(step -> step.setStatus(Status.PASSED));
break;
case FAIL:
if (saveScreenshots) {
getScreenshotBytes()
.ifPresent(bytes -> lifecycle.addAttachment("Screenshot", "image/png", "png", bytes));
}
if (savePageHtml) {
getPageSourceBytes()
.ifPresent(bytes -> lifecycle.addAttachment("Page source", "text/html", "html", bytes));
}
lifecycle.updateStep(stepResult -> {
stepResult.setStatus(getStatus(event.getError()).orElse(Status.BROKEN));
stepResult.setStatusDetails(getStatusDetails(event.getError()).orElse(new StatusDetails()));
});
break;
default:
LOGGER.warn("Step finished with unsupported status {}", event.getStatus());
break;
}
lifecycle.stopStep();
});
}
}