Skip to content

Commit

Permalink
AlexeiBarantsev: Taking control over order of execution @Before-block…
Browse files Browse the repository at this point in the history
…s, and disabling some tests that fail locally.

r18280
  • Loading branch information
barancev committed Dec 7, 2012
1 parent 3c9d776 commit c9d6863
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
import com.google.common.io.Resources;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.openqa.selenium.Build;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
Expand Down Expand Up @@ -105,92 +110,112 @@ public static void initializeServer() {
GlobalTestEnvironment.get(SeleniumTestEnvironment.class);
}

@Before
public void addNecessaryJavascriptCommands() {
if (selenium == null || !(selenium instanceof WebDriverBackedSelenium)) {
return;
@Rule
public TestRule traceMethodName = new TestWatcher() {
@Override
protected void starting(Description description) {
super.starting(description);
log.info(">>> Starting " + description);
}

// We need to be a on page where we can execute JS
WebDriver driver = ((WrapsDriver) selenium).getWrappedDriver();
driver.get(whereIs("/selenium-server"));
@Override
protected void finished(Description description) {
super.finished(description);
log.info("<<< Finished " + description);
}
};

public ExternalResource initializeSelenium = new ExternalResource() {
@Override
protected void before() throws Throwable {
selenium = instance.get();
if (selenium != null) {
return;
}

try {
URL scriptUrl =
Resources.getResource(getClass(), "/com/thoughtworks/selenium/testHelpers.js");
String script = Resources.toString(scriptUrl, Charsets.UTF_8);
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(UNEXPECTED_ALERT_BEHAVIOUR, IGNORE);
if (Boolean.getBoolean("singlewindow")) {
caps.setCapability(SINGLE_WINDOW, true);
caps.setCapability(MULTI_WINDOW, "");
}
if (Boolean.getBoolean("webdriver.debug")) {
caps.setCapability("browserSideLog", true);
}

((JavascriptExecutor) driver).executeScript(script);
} catch (IOException e) {
fail("Cannot read script: " + Throwables.getStackTraceAsString(e));
}
}
String baseUrl = whereIs("/selenium-server/tests/");
caps.setCapability("selenium.server.url", baseUrl);

private String whereIs(String location) {
return GlobalTestEnvironment.get().getAppServer().whereIs(location);
}

@Before
public void focusOnMainWindow() {
if (selenium == null) {
return;
}
selenium.windowFocus();
}
WebDriver driver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
if (driver instanceof SeleneseBackedWebDriver) {
selenium = ((SeleneseBackedWebDriver) driver).getWrappedSelenium();
} else {
selenium = new WebDriverBackedSelenium(driver, baseUrl);
}

@Before
public void returnFocusToMainWindow() {
if (selenium == null) {
return;
selenium.setBrowserLogLevel("debug");
instance.set(selenium);
}
};

try {
selenium.selectWindow("");
} catch (SeleniumException e) {
// TODO(simon): Window switching in Opera is picky.
if (Browser.detect() != Browser.opera) {
throw e;
public ExternalResource addNecessaryJavascriptCommands = new ExternalResource() {
@Override
protected void before() throws Throwable {
if (selenium == null || !(selenium instanceof WebDriverBackedSelenium)) {
return;
}
}
}

@Before
public void initializeSelenium() {
selenium = instance.get();
if (selenium != null) {
return;
}
// We need to be a on page where we can execute JS
WebDriver driver = ((WrapsDriver) selenium).getWrappedDriver();
driver.get(whereIs("/selenium-server"));

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(UNEXPECTED_ALERT_BEHAVIOUR, IGNORE);
if (Boolean.getBoolean("singlewindow")) {
caps.setCapability(SINGLE_WINDOW, true);
caps.setCapability(MULTI_WINDOW, "");
}
if (Boolean.getBoolean("webdriver.debug")) {
caps.setCapability("browserSideLog", true);
}
try {
URL scriptUrl =
Resources.getResource(getClass(), "/com/thoughtworks/selenium/testHelpers.js");
String script = Resources.toString(scriptUrl, Charsets.UTF_8);

String baseUrl = whereIs("/selenium-server/tests/");
caps.setCapability("selenium.server.url", baseUrl);
((JavascriptExecutor) driver).executeScript(script);
} catch (IOException e) {
fail("Cannot read script: " + Throwables.getStackTraceAsString(e));
}
}
};

public ExternalResource returnFocusToMainWindow = new ExternalResource() {
@Override
protected void before() throws Throwable {
if (selenium == null) {
return;
}

WebDriver driver = new WebDriverBuilder().setDesiredCapabilities(caps).get();
if (driver instanceof SeleneseBackedWebDriver) {
selenium = ((SeleneseBackedWebDriver) driver).getWrappedSelenium();
} else {
selenium = new WebDriverBackedSelenium(driver, baseUrl);
try {
selenium.selectWindow("");
selenium.windowFocus();
} catch (SeleniumException e) {
// TODO(simon): Window switching in Opera is picky.
if (Browser.detect() != Browser.opera) {
throw e;
}
}
}
};

selenium.setBrowserLogLevel("debug");
instance.set(selenium);
}
@Rule
public TestRule chain =
RuleChain.outerRule(addNecessaryJavascriptCommands)
.around(returnFocusToMainWindow)
.around(initializeSelenium);

@After
public void checkVerifications() {
checkForVerificationErrors();
}

private String whereIs(String location) {
return GlobalTestEnvironment.get().getAppServer().whereIs(location);
}

public static void destroyDriver() {
Selenium selenium = instance.get();
if (selenium != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@
TestCssLocators.class,
// TestCursorPosition.class,
// TestDojoDragAndDrop.class,
TestDomainCookie.class,
// TestDomainCookie.class,
// TestDragAndDrop.class,
TestEditable.class,
TestElementIndex.class,
TestElementOrder.class,
TestElementPresent.class,
TestErrorChecking.class,
TestEval.class,
TestEvilClosingWindow.class,
// TestEvilClosingWindow.class,
TestFailingAssert.class,
TestFailingVerifications.class,
// TestFocusOnBlur.class,
Expand Down Expand Up @@ -136,7 +136,7 @@
// TestRollup.class,
TestSelect.class,
TestSelectMultiLevelFrame.class,
// TestSelectPopUp.class,
TestSelectPopUp.class,
// TestSelectWindow.class,
// TestSelectWindowTitle.class,
// TestSetSpeed.class,
Expand Down

0 comments on commit c9d6863

Please sign in to comment.