Skip to content

Commit

Permalink
Colourise the output log of the new core runner.
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jul 28, 2016
1 parent 3a378b7 commit b668406
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.server.htmlrunner;


import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -54,8 +55,11 @@ public void run(Results results, WebDriver driver, Selenium selenium) {
driver.get(url);
}

String rawSource = driver.getPageSource();
// Grabbing the steps modifies the underlying HTML...
List<LoggableStep> steps = findCommands(driver);
// ... which we now grab so we can process it later.
String rawSource = getLoggableTests(driver);

TestState state = new TestState();
List<StepResult> stepResults = new ArrayList<>(steps.size());
NextStepDecorator decorator = NextStepDecorator.IDENTITY;
Expand All @@ -65,15 +69,30 @@ public void run(Results results, WebDriver driver, Selenium selenium) {
stepResults.add(new StepResult(step, decorator.getCause()));
if (!decorator.isOkayToContinueTest()) {
break;
} else {
stepResults.add(new StepResult(step, null));
}
state.sleepTight();
}

results.addTest(rawSource, stepResults);
}

private String getLoggableTests(WebDriver driver) {
return (String) ((JavascriptExecutor) driver).executeScript(Joiner.on("\n").join(
"var resultHTML = document.body.innerHTML;",
"if (!resultHTML) { return ''; }",

"var trElement = document.createElement('tr');",
"var divElement = document.createElement('div');",
"divElement.innerHTML = resultHTML;",

"var cell = document.createElement('td');",
"cell.appendChild(divElement);",

"trElement.appendChild(cell);",

"return trElement.outerHTML;"));
}

private List<LoggableStep> findCommands(WebDriver driver) {
// Let's just run and hide in the horror that is JS for the sake of speed.
List<List<String>> rawSteps = (List<List<String>>) ((JavascriptExecutor) driver).executeScript(
Expand All @@ -85,7 +104,9 @@ private List<LoggableStep> findCommands(WebDriver driver) {
" continue;\n" +
" }\n" +
" var cells = tables[i].rows[rowCount].cells;\n" +
" toReturn.push([cells[0].textContent.trim(), cells[1].textContent, cells[2].textContent]);\n" +
" toReturn.push([cells[0].textContent.trim(), cells[1].textContent.trim(), cells[2].textContent.trim()]);\n" +
// Now modify the row so we know we should add a result later
" tables[i].rows[rowCount].className += 'insert-core-result';\n" +
" }\n" +
"}\n" +
"return toReturn;");
Expand Down Expand Up @@ -133,10 +154,26 @@ public String toString() {
static class StepResult {
private final LoggableStep step;
private final Throwable cause;
private final String renderableClass;

public StepResult(LoggableStep step, Throwable cause) {
this.step = Preconditions.checkNotNull(step);
this.cause = cause;

if (cause == null) {
// I think we can all agree this is shameful
if (step.command.startsWith("verify") || step.command.startsWith("assert")) {
this.renderableClass = "status_passed";
} else {
this.renderableClass = "status_done";
}
} else {
this.renderableClass = "status_failed";
}
}

public String getRenderableClass() {
return renderableClass;
}

public boolean isSuccessful() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,6 @@ public boolean isOkayToContinueTest() {
}
};

static NextStepDecorator ASSERTION_FAILED = new NextStepDecorator(null) {

@Override
public boolean isOkayToContinueTest() {
return false;
}
};

static NextStepDecorator VERIFICATION_FAILED = new NextStepDecorator(null) {

@Override
public boolean isOkayToContinueTest() {
return true;
}
};

private final Throwable cause;

public NextStepDecorator() {
Expand Down Expand Up @@ -74,4 +58,24 @@ public boolean isOkayToContinueTest() {
}
};
}

public static NextStepDecorator ASSERTION_FAILED(String message) {
return new NextStepDecorator(new AssertionError(message)) {
@Override
public boolean isOkayToContinueTest() {
return false;
}
};
}

public static NextStepDecorator VERIFICATION_FAILED(String message) {
return new NextStepDecorator(new AssertionError(message)) {
@Override
public boolean isOkayToContinueTest() {
return true;
}
};
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ public NextStepDecorator execute(Selenium selenium, TestState state) {

steps.put(
"assertSelected",
((locator, value) -> new SelectedOption(locator, value, NextStepDecorator.ASSERTION_FAILED)));
((locator, value) -> new SelectedOption(
locator,
value,
NextStepDecorator.ASSERTION_FAILED(value + " not selected"))));
steps.put(
"verifySelected",
((locator, value) ->
new SelectedOption(locator, value, NextStepDecorator.VERIFICATION_FAILED)));
((locator, value) -> new SelectedOption(
locator,
value,
NextStepDecorator.VERIFICATION_FAILED(value + " not selected"))));

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

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

// This is kind of fragile. Oh well.
if (actualResult.equals(NextStepDecorator.IDENTITY)) {
return NextStepDecorator.VERIFICATION_FAILED;
return NextStepDecorator.VERIFICATION_FAILED("Expected command to fail");
}
return NextStepDecorator.IDENTITY;
}
Expand Down
Loading

0 comments on commit b668406

Please sign in to comment.