001: package info.jtrac.selenium;
002:
003: import com.thoughtworks.selenium.DefaultSelenium;
004: import java.lang.reflect.Constructor;
005: import java.lang.reflect.Method;
006: import junit.framework.Test;
007: import junit.framework.TestCase;
008: import junit.framework.TestSuite;
009: import org.openqa.selenium.server.SeleniumServer;
010:
011: /**
012: * base class for Selenium test scripts that hack JUnit so as to be
013: * able to run test methods in the order in which they appear
014: * in the source file. If the class name is "AllTest.java", following
015: * boiler-plate must be included (I said this was a hack :)
016: *
017: * static {
018: * clazz = AllTest.class;
019: * }
020: *
021: * public AllTest(String name) {
022: * super(name);
023: * }
024: *
025: */
026: public abstract class SeleniumTestCase extends TestCase {
027:
028: public SeleniumTestCase(String name) {
029: super (name);
030: }
031:
032: private static ThreadLocalSelenium threadLocalSelenium;
033: protected static Class clazz;
034: protected JtracSelenium selenium;
035: protected static SeleniumServer server;
036:
037: public static Test suite() throws Exception {
038: threadLocalSelenium = new ThreadLocalSelenium();
039: Constructor constructor = clazz.getDeclaredConstructors()[0];
040: Method[] methods = clazz.getMethods();
041: TestSuite s = new TestSuite();
042: for (Method m : methods) {
043: if (m.getName().startsWith("test")) {
044: Test test = (Test) constructor
045: .newInstance(new Object[] { m.getName() });
046: s.addTest(test);
047: }
048: }
049: return s;
050: }
051:
052: private static class ThreadLocalSelenium extends ThreadLocal {
053: @Override
054: public JtracSelenium initialValue() {
055: try {
056: server = new SeleniumServer();
057: server.start();
058: } catch (Exception e) {
059: throw new RuntimeException(e);
060: }
061: JtracSelenium s = new JtracSelenium("localhost",
062: SeleniumServer.getDefaultPort(), "*iexplore",
063: "http://localhost:8080/jtrac");
064: s.start();
065: return s;
066: }
067: }
068:
069: @Override
070: public final void setUp() {
071: selenium = (JtracSelenium) threadLocalSelenium.get();
072: }
073:
074: protected void assertTextPresent(String text) {
075: assertTrue(selenium.isTextPresent(text));
076: }
077:
078: protected void stopSelenium() {
079: selenium.stop();
080: server.stop();
081: }
082:
083: /**
084: * custom extension of Selenium to automatically wait for page to load
085: * after clicking a button or link
086: */
087: public static class JtracSelenium extends DefaultSelenium {
088:
089: public JtracSelenium(String host, int port, String browser,
090: String url) {
091: super (host, port, browser, url);
092: }
093:
094: public void clickAndWait(String locator) {
095: click(locator);
096: waitForPageToLoad("30000");
097: }
098:
099: }
100:
101: }
|