18
18
package org .openqa .selenium .server .htmlrunner ;
19
19
20
20
21
+ import com .google .common .base .Joiner ;
21
22
import com .google .common .base .Preconditions ;
22
23
import com .google .common .collect .ImmutableList ;
23
24
import com .google .common .collect .ImmutableMap ;
@@ -54,8 +55,11 @@ public void run(Results results, WebDriver driver, Selenium selenium) {
54
55
driver .get (url );
55
56
}
56
57
57
- String rawSource = driver . getPageSource ();
58
+ // Grabbing the steps modifies the underlying HTML...
58
59
List <LoggableStep > steps = findCommands (driver );
60
+ // ... which we now grab so we can process it later.
61
+ String rawSource = getLoggableTests (driver );
62
+
59
63
TestState state = new TestState ();
60
64
List <StepResult > stepResults = new ArrayList <>(steps .size ());
61
65
NextStepDecorator decorator = NextStepDecorator .IDENTITY ;
@@ -65,15 +69,30 @@ public void run(Results results, WebDriver driver, Selenium selenium) {
65
69
stepResults .add (new StepResult (step , decorator .getCause ()));
66
70
if (!decorator .isOkayToContinueTest ()) {
67
71
break ;
68
- } else {
69
- stepResults .add (new StepResult (step , null ));
70
72
}
71
73
state .sleepTight ();
72
74
}
73
75
74
76
results .addTest (rawSource , stepResults );
75
77
}
76
78
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
+
77
96
private List <LoggableStep > findCommands (WebDriver driver ) {
78
97
// Let's just run and hide in the horror that is JS for the sake of speed.
79
98
List <List <String >> rawSteps = (List <List <String >>) ((JavascriptExecutor ) driver ).executeScript (
@@ -85,7 +104,9 @@ private List<LoggableStep> findCommands(WebDriver driver) {
85
104
" continue;\n " +
86
105
" }\n " +
87
106
" 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 " +
89
110
" }\n " +
90
111
"}\n " +
91
112
"return toReturn;" );
@@ -133,10 +154,26 @@ public String toString() {
133
154
static class StepResult {
134
155
private final LoggableStep step ;
135
156
private final Throwable cause ;
157
+ private final String renderableClass ;
136
158
137
159
public StepResult (LoggableStep step , Throwable cause ) {
138
160
this .step = Preconditions .checkNotNull (step );
139
161
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 ;
140
177
}
141
178
142
179
public boolean isSuccessful () {
0 commit comments