Skip to content
This repository has been archived by the owner on May 22, 2018. It is now read-only.

Commit

Permalink
JasonLeyba: Migrate the js test infrastructure to junit4. Delete the …
Browse files Browse the repository at this point in the history
…e2e infrastructure for webdriverjs, which has long since rotted. I'll have to bring it back in a future change.

r17602
  • Loading branch information
jleyba committed Jul 30, 2012
1 parent acb7472 commit 0f51aaf
Show file tree
Hide file tree
Showing 23 changed files with 394 additions and 931 deletions.
3 changes: 1 addition & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,7 @@ desc "Calculate dependencies required for testing the automation atoms"
task :calcdeps => "build/javascript/deps.js"

task :test_webdriverjs => [
"//javascript/webdriver:test:run",
"//javascript/webdriver:test_e2e:run"
"//javascript/webdriver:test:run"
]

desc "Generate a single file with WebDriverJS' public API"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.openqa.selenium.javascript;

import static org.junit.Assert.fail;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;

import com.google.common.base.Function;
import com.google.common.base.Stopwatch;
import com.google.common.base.Supplier;
import org.junit.runners.model.Statement;

import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class ClosureTestStatement extends Statement {

private static final Logger LOG = Logger.getLogger(ClosureTestStatement.class.getName());

private final Supplier<WebDriver> driverSupplier;
private final String testPath;
private final Function<String, URL> filePathToUrlFn;
private final long timeoutSeconds;

public ClosureTestStatement(Supplier<WebDriver> driverSupplier,
String testPath, Function<String, URL> filePathToUrlFn, long timeoutSeconds) {
this.driverSupplier = driverSupplier;
this.testPath = testPath;
this.filePathToUrlFn = filePathToUrlFn;
this.timeoutSeconds = Math.max(0, timeoutSeconds);
}

@Override
public void evaluate() throws Throwable {
URL testUrl = filePathToUrlFn.apply(testPath);
LOG.info("Running: " + testUrl);

Stopwatch stopwatch = new Stopwatch();
stopwatch.start();

WebDriver driver = driverSupplier.get();
JavascriptExecutor executor = (JavascriptExecutor) driver;
// Avoid Safari JS leak between tests.
executor.executeScript("if (window && window.top) window.top.G_testRunner = null");

try {
driver.get(testUrl.toString());
} catch (WebDriverException e) {
fail("Test failed to load: " + e.getMessage());
}

while (!getBoolean(executor, Query.IS_FINISHED)) {
long elapsedTime = stopwatch.elapsedTime(TimeUnit.SECONDS);
if (timeoutSeconds > 0 && elapsedTime > timeoutSeconds) {
throw new JavaScriptAssertionError("Tests timed out after " + elapsedTime + " s");
}
TimeUnit.MILLISECONDS.sleep(100);
}

if (!getBoolean(executor, Query.IS_SUCCESS)) {
String report = getString(executor, Query.GET_REPORT);
throw new JavaScriptAssertionError(report);
}
}

private boolean getBoolean(JavascriptExecutor executor, Query query) {
return (Boolean) executor.executeScript(query.script);
}

private String getString(JavascriptExecutor executor, Query query) {
return (String) executor.executeScript(query.script);
}

private static enum Query {
IS_FINISHED("return !!tr && tr.isFinished();"),
IS_SUCCESS("return !!tr && tr.isSuccess();"),
GET_REPORT("return tr.getReport(true);");

private final String script;

private Query(String script) {
this.script = "var tr = window.top.G_testRunner;" + script;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,9 @@

package org.openqa.selenium.javascript;

import junit.framework.Test;

import org.openqa.selenium.EnvironmentStarter;

import com.google.common.base.Function;
import org.junit.runner.RunWith;


@RunWith(JavaScriptTestSuite.class)
public class ClosureTestSuite {

public static Test suite() {
Test suite = new JsTestSuiteBuilder().withTestFactory(new Function<String, Test>() {
public Test apply(String input) {
return new ClosureTestCase(input);
}
}).build();
return new EnvironmentStarter(suite);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.openqa.selenium.javascript;

class JavaScriptAssertionError extends AssertionError {

public JavaScriptAssertionError(String message) {
super(message);
}

@Override
public Throwable fillInStackTrace() {
return this; // No java stack traces.
}

@Override
public void setStackTrace(StackTraceElement[] stackTraceElements) {
// No java stack traces.
}
}
Loading

0 comments on commit 0f51aaf

Please sign in to comment.