forked from jenkinsci/pipeline-graph-view-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipelineConsole.java
More file actions
132 lines (106 loc) · 4.97 KB
/
PipelineConsole.java
File metadata and controls
132 lines (106 loc) · 4.97 KB
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
129
130
131
132
package io.jenkins.plugins.pipelinegraphview.playwright;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.AriaRole;
import io.jenkins.plugins.pipelinegraphview.utils.PipelineState;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Assertions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class PipelineConsole {
private static final Logger log = LoggerFactory.getLogger(PipelineConsole.class);
private static final String STEP_NAME_CLASS = ".pgv-step-detail-header__content";
private final Locator logs;
private final Page page;
public PipelineConsole(Locator logs) {
this.logs = logs;
this.page = logs.page();
}
public void isVisible() {
assertThat(logs).isVisible();
}
public void stageIsSelected(String name) {
assertThat(selectedStage()).hasText(name);
}
private Locator selectedStage() {
log.info("Getting selected stage from the logs");
return logs.getByRole(AriaRole.HEADING, new Locator.GetByRoleOptions().setLevel(2));
}
private Locator steps() {
return logs.locator(".pgv-step-detail-group");
}
private boolean isOpenStep(Locator step) {
String[] classes =
step.locator(".pgv-step-detail-header").getAttribute("class").split(" ");
for (String clazz : classes) {
if (clazz.equals("jenkins-button--tertiary")) {
return false;
}
}
return true;
}
public void stepContainsText(String stepName, String textToFind) {
log.info("Checking that the step {} contains a log with the text {}", stepName, textToFind);
Locator stepContainer = steps().filter(new Locator.FilterOptions()
.setHas(page.locator(
STEP_NAME_CLASS,
new Page.LocatorOptions().setHasText(Pattern.compile("^" + stepName + "$")))))
.first();
if (!isOpenStep(stepContainer)) {
stepContainer.click();
}
Locator stepLogs = stepContainer.getByRole(AriaRole.LOG);
assertThat(stepLogs).containsText(textToFind);
Locator plainTextLogsLink = stepContainer.getByRole(
AriaRole.LINK, new Locator.GetByRoleOptions().setName("View step as plain text"));
try (Page plainText = page.context().waitForPage(plainTextLogsLink::click)) {
assertThat(plainText.locator("body")).containsText(textToFind);
}
}
public void stepDoesNotContainText(String stepName, String textToNotFind) {
log.info("Checking that the step {} does not contain a log with the text {}", stepName, textToNotFind);
Locator stepContainer = steps().filter(new Locator.FilterOptions()
.setHas(page.locator(
STEP_NAME_CLASS,
new Page.LocatorOptions().setHasText(Pattern.compile("^" + stepName + "$")))))
.first();
if (!isOpenStep(stepContainer)) {
stepContainer.click();
}
Locator stepLogs = stepContainer.getByRole(AriaRole.LOG);
assertThat(stepLogs).not().containsText(textToNotFind);
Locator plainTextLogsLink = stepContainer.getByRole(
AriaRole.LINK, new Locator.GetByRoleOptions().setName("View step as plain text"));
try (Page plainText = page.context().waitForPage(plainTextLogsLink::click)) {
assertThat(plainText.locator("body")).not().containsText(textToNotFind);
}
}
public void stageHasSteps(String step, String... additional) {
List<String> expectedSteps = new ArrayList<>();
expectedSteps.add(step);
Collections.addAll(expectedSteps, additional);
log.info("Checking that the stage has the steps {}", expectedSteps);
List<String> foundSteps = steps().locator(STEP_NAME_CLASS).allTextContents();
expectedSteps.removeAll(foundSteps);
if (!expectedSteps.isEmpty()) {
Assertions.fail("Could not find steps with the names:\n " + String.join("\n ", expectedSteps)
+ "\nFound steps:\n" + String.join("\n ", foundSteps));
}
}
public void stageHasState(String stage, PipelineState state) {
this.stageIsSelected(stage);
log.info("Checking if stage {} has state {} in the logs", stage, state);
Locator stateSVG = selectedStage().locator("..").getByRole(AriaRole.IMG);
assertThat(stateSVG).hasAccessibleName(state.toString());
}
public void scrollToText(String text) {
log.info("Scrolling to log line containing the text {}", text);
Locator logLine = logs.getByText(text);
logLine.scrollIntoViewIfNeeded();
assertThat(logLine).isInViewport();
}
}