1 package org.itracker.selenium;
2
3 import com.dumbster.smtp.SimpleSmtpServer;
4 import com.thoughtworks.selenium.Selenium;
5 import org.apache.commons.logging.Log;
6 import org.apache.log4j.Logger;
7 import org.itracker.AbstractDependencyInjectionTest;
8 import sun.java2d.pipe.SpanShapeRenderer;
9
10 import java.io.IOException;
11 import java.net.ServerSocket;
12
13
14
15
16
17
18
19
20
21 public abstract class AbstractSeleniumTestCase extends AbstractDependencyInjectionTest {
22 public final static String SE_TIMEOUT = "20000";
23 public final static int SMTP_PORT = 2525;
24 protected SimpleSmtpServer smtpServer;
25 protected Selenium selenium;
26 protected String applicationHost;
27 protected int applicationPort;
28 protected String applicationPath;
29 Logger log = Logger.getLogger(getClass());
30
31 public AbstractSeleniumTestCase() {
32 try {
33 selenium = SeleniumManager.getSelenium();
34 assertNotNull(selenium);
35 applicationHost = SeleniumManager.getApplicationHost();
36 applicationPort = SeleniumManager.getApplicationPort();
37 applicationPath = SeleniumManager.getApplicationPath();
38 } catch (final IOException e) {
39 log.error(e);
40 fail(e.getMessage());
41 }
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 SimpleSmtpServer startSMTP() throws InterruptedException {
64 log.info("startSMTP: skip smtp until working");
65 if (true) return null;
66
67
68 log.info("Starting smtp");
69 if (null != smtpServer && !smtpServer.isStopped()) {
70 log.warn("Already running smtp");
71 stopSMTP();
72 log.info("Already running smtp stopped");
73 }
74 smtpServer = null;
75 try {
76 ServerSocket s = new ServerSocket(SMTP_PORT);
77 log.info("sleep 100");
78 Thread.currentThread().sleep(100);
79 log.info("close socket");
80 s.close();
81 log.info("sleep 100");
82 Thread.currentThread().sleep(100);
83 } catch (IOException ioe) {
84 fail("Socket " + SMTP_PORT + " is open: " + ioe.getMessage());
85 }
86 log.info("SimpleSmtpServer.start("+SMTP_PORT+")");
87 SimpleSmtpServer smtp = SimpleSmtpServer.start(SMTP_PORT);
88 log.info("sleep 300");
89 Thread.currentThread().sleep(300);
90
91 log.info("checking running smtp");
92
93 if (smtp.isStopped()) {
94 throw new RuntimeException("Could not Start smtpServer");
95 }
96
97 log.info("got running smtp " + smtp);
98 smtpServer = smtp;
99 log.info("Started smtp");
100 return smtp;
101 }
102
103 void stopSMTP() {
104 log.info("stopSMTP: skip smtp until working");
105 if (true) return;
106
107 log.info("Stopping smtp");
108
109 try {
110 smtpServer.stop();
111 Thread.currentThread().sleep(300);
112 if (!smtpServer.isStopped()) {
113 throw new RuntimeException("Could not Stop smtpServer");
114 }
115 } catch (Exception e) {
116 log.warn("failed to close smtp", e);
117
118 } finally {
119 smtpServer = null;
120 }
121
122 log.info("Stopped smtp");
123 }
124
125 final void assertElementPresent(String q) {
126 assertTrue(selenium.getLocation() + "#" + q + " expected present", selenium.isElementPresent(q));
127 }
128
129 final void assertTextEquals(String expected, String q) {
130 assertEquals(selenium.getLocation() + "#" + q, expected, selenium.getText(q));
131 }
132
133
134
135
136 protected void closeSession() {
137 SeleniumManager.closeSession(selenium);
138 assertElementPresent("login");
139 }
140 }