View Javadoc

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   * It is a base class for all Selenium-based test ca se.
15   * It performa initialization of Selenium client part and
16   * retrieves some generally-used parameters like hose application
17   * is running at, port, context.
18   *
19   * @author Andrey Sergievskiy <seas@andreysergievskiy.com>
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  //    @Override
45  //    public void onTearDown() throws Exception {
46  //
47  //            log.info("onTearDown: stopping smtp");
48  //            stopSMTP();
49  //            log.info("onTearDown: stopped smtp");
50  //
51  //            super.onTearDown();
52  //
53  //    }
54  //
55  //    @Override
56  //    public void onSetUp() throws Exception {
57  //        super.onSetUp();
58  //        log.info("onSetUp: starting smtp");
59  //        startSMTP();
60  //        log.info("onSetUp: started smtp");
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  //        assertNotNull("smtp is null", smtp);
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 //        assertNotNull("null smtp", smtpServer);
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 //            fail("could not stop smtp: " + smtpServer + ": " + e.getMessage());
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      * This will initialize a new selenium session for this test scope.
135      */
136     protected void closeSession() {
137         SeleniumManager.closeSession(selenium);
138         assertElementPresent("login");
139     }
140 }