Skip to content

Commit b668406

Browse files
committed
Colourise the output log of the new core runner.
1 parent 3a378b7 commit b668406

File tree

5 files changed

+335
-120
lines changed

5 files changed

+335
-120
lines changed

java/server/src/org/openqa/selenium/server/htmlrunner/CoreTestCase.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.server.htmlrunner;
1919

2020

21+
import com.google.common.base.Joiner;
2122
import com.google.common.base.Preconditions;
2223
import com.google.common.collect.ImmutableList;
2324
import com.google.common.collect.ImmutableMap;
@@ -54,8 +55,11 @@ public void run(Results results, WebDriver driver, Selenium selenium) {
5455
driver.get(url);
5556
}
5657

57-
String rawSource = driver.getPageSource();
58+
// Grabbing the steps modifies the underlying HTML...
5859
List<LoggableStep> steps = findCommands(driver);
60+
// ... which we now grab so we can process it later.
61+
String rawSource = getLoggableTests(driver);
62+
5963
TestState state = new TestState();
6064
List<StepResult> stepResults = new ArrayList<>(steps.size());
6165
NextStepDecorator decorator = NextStepDecorator.IDENTITY;
@@ -65,15 +69,30 @@ public void run(Results results, WebDriver driver, Selenium selenium) {
6569
stepResults.add(new StepResult(step, decorator.getCause()));
6670
if (!decorator.isOkayToContinueTest()) {
6771
break;
68-
} else {
69-
stepResults.add(new StepResult(step, null));
7072
}
7173
state.sleepTight();
7274
}
7375

7476
results.addTest(rawSource, stepResults);
7577
}
7678

79+
private String getLoggableTests(WebDriver driver) {
80+
return (String) ((JavascriptExecutor) driver).executeScript(Joiner.on("\n").join(
81+
"var resultHTML = document.body.innerHTML;",
82+
"if (!resultHTML) { return ''; }",
83+
84+
"var trElement = document.createElement('tr');",
85+
"var divElement = document.createElement('div');",
86+
"divElement.innerHTML = resultHTML;",
87+
88+
"var cell = document.createElement('td');",
89+
"cell.appendChild(divElement);",
90+
91+
"trElement.appendChild(cell);",
92+
93+
"return trElement.outerHTML;"));
94+
}
95+
7796
private List<LoggableStep> findCommands(WebDriver driver) {
7897
// Let's just run and hide in the horror that is JS for the sake of speed.
7998
List<List<String>> rawSteps = (List<List<String>>) ((JavascriptExecutor) driver).executeScript(
@@ -85,7 +104,9 @@ private List<LoggableStep> findCommands(WebDriver driver) {
85104
" continue;\n" +
86105
" }\n" +
87106
" var cells = tables[i].rows[rowCount].cells;\n" +
88-
" toReturn.push([cells[0].textContent.trim(), cells[1].textContent, cells[2].textContent]);\n" +
107+
" toReturn.push([cells[0].textContent.trim(), cells[1].textContent.trim(), cells[2].textContent.trim()]);\n" +
108+
// Now modify the row so we know we should add a result later
109+
" tables[i].rows[rowCount].className += 'insert-core-result';\n" +
89110
" }\n" +
90111
"}\n" +
91112
"return toReturn;");
@@ -133,10 +154,26 @@ public String toString() {
133154
static class StepResult {
134155
private final LoggableStep step;
135156
private final Throwable cause;
157+
private final String renderableClass;
136158

137159
public StepResult(LoggableStep step, Throwable cause) {
138160
this.step = Preconditions.checkNotNull(step);
139161
this.cause = cause;
162+
163+
if (cause == null) {
164+
// I think we can all agree this is shameful
165+
if (step.command.startsWith("verify") || step.command.startsWith("assert")) {
166+
this.renderableClass = "status_passed";
167+
} else {
168+
this.renderableClass = "status_done";
169+
}
170+
} else {
171+
this.renderableClass = "status_failed";
172+
}
173+
}
174+
175+
public String getRenderableClass() {
176+
return renderableClass;
140177
}
141178

142179
public boolean isSuccessful() {

java/server/src/org/openqa/selenium/server/htmlrunner/NextStepDecorator.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,6 @@ public boolean isOkayToContinueTest() {
3030
}
3131
};
3232

33-
static NextStepDecorator ASSERTION_FAILED = new NextStepDecorator(null) {
34-
35-
@Override
36-
public boolean isOkayToContinueTest() {
37-
return false;
38-
}
39-
};
40-
41-
static NextStepDecorator VERIFICATION_FAILED = new NextStepDecorator(null) {
42-
43-
@Override
44-
public boolean isOkayToContinueTest() {
45-
return true;
46-
}
47-
};
48-
4933
private final Throwable cause;
5034

5135
public NextStepDecorator() {
@@ -74,4 +58,24 @@ public boolean isOkayToContinueTest() {
7458
}
7559
};
7660
}
61+
62+
public static NextStepDecorator ASSERTION_FAILED(String message) {
63+
return new NextStepDecorator(new AssertionError(message)) {
64+
@Override
65+
public boolean isOkayToContinueTest() {
66+
return false;
67+
}
68+
};
69+
}
70+
71+
public static NextStepDecorator VERIFICATION_FAILED(String message) {
72+
return new NextStepDecorator(new AssertionError(message)) {
73+
@Override
74+
public boolean isOkayToContinueTest() {
75+
return true;
76+
}
77+
};
78+
}
79+
80+
7781
}

java/server/src/org/openqa/selenium/server/htmlrunner/NonReflectiveSteps.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ public NextStepDecorator execute(Selenium selenium, TestState state) {
8888

8989
steps.put(
9090
"assertSelected",
91-
((locator, value) -> new SelectedOption(locator, value, NextStepDecorator.ASSERTION_FAILED)));
91+
((locator, value) -> new SelectedOption(
92+
locator,
93+
value,
94+
NextStepDecorator.ASSERTION_FAILED(value + " not selected"))));
9295
steps.put(
9396
"verifySelected",
94-
((locator, value) ->
95-
new SelectedOption(locator, value, NextStepDecorator.VERIFICATION_FAILED)));
97+
((locator, value) -> new SelectedOption(
98+
locator,
99+
value,
100+
NextStepDecorator.VERIFICATION_FAILED(value + " not selected"))));
96101

97102
steps.put("echo", ((locator, value) -> (selenium, state) -> {
98103
LOG.info(locator);
@@ -129,7 +134,7 @@ public NextStepDecorator evaluate(CoreStep nextStep, Selenium selenium, TestStat
129134

130135
// This is kind of fragile. Oh well.
131136
if (actualResult.equals(NextStepDecorator.IDENTITY)) {
132-
return NextStepDecorator.ASSERTION_FAILED;
137+
return NextStepDecorator.ASSERTION_FAILED("Expected command to fail");
133138
}
134139
return NextStepDecorator.IDENTITY;
135140
}
@@ -148,7 +153,7 @@ public NextStepDecorator evaluate(CoreStep nextStep, Selenium selenium, TestStat
148153

149154
// This is kind of fragile. Oh well.
150155
if (actualResult.equals(NextStepDecorator.IDENTITY)) {
151-
return NextStepDecorator.VERIFICATION_FAILED;
156+
return NextStepDecorator.VERIFICATION_FAILED("Expected command to fail");
152157
}
153158
return NextStepDecorator.IDENTITY;
154159
}

0 commit comments

Comments
 (0)