View Javadoc

1   package com.atlassian.selenium;
2   
3   import junit.framework.TestCase;
4   
5   /**
6    * A base class for selenium tests
7    *
8    * @since v3.12
9    */
10  public abstract class SeleniumTest extends TestCase
11  {
12      protected SeleniumAssertions assertThat;
13      protected SeleniumClient client;
14      protected SeleniumConfiguration config;
15  
16      public abstract SeleniumConfiguration getSeleniumConfiguration();
17  
18      /**
19       * Calls overridden onSetup method before starting
20       * the selenium client and possibly server and initiating
21       * assertThat and interaction variables
22       */
23      public final void setUp() throws Exception
24      {
25          super.setUp();
26          config = getSeleniumConfiguration();
27  
28          if (SeleniumStarter.getInstance().isManual())
29          {
30              SeleniumStarter.getInstance().start(config);
31          }
32  
33          client = getSeleniumClient();
34  
35          assertThat = new SeleniumAssertions(client, config);
36          // find user agent by running somehting on sel browser
37          String js = "var win = this.browserbot.getCurrentWindow();\n" +
38                      "(function() { " +
39                      "   if (win.opera) { return 'opera'; } \n" +
40                      "   if (document.all) { return 'ie'; } \n" +
41                      "   if (win.navigator.userAgent.indexOf('Firefox') != -1) { return 'firefox'; } \n" +
42                      "   if (win.navigator.vendor.indexOf('Apple') != -1) { return 'safari'; } \n" +
43                      "   return 'unknown' \n" +
44                      "})()";
45          
46          SeleniumStarter.getInstance().setUserAgent(client.getEval(js));
47  
48          onSetUp();
49      }
50  
51      /**
52       * Gets the SeleniumClient. Override this method if you would like to return your
53       * own implementation of {@link SeleniumClient}.
54       * @return
55       */
56      protected SeleniumClient getSeleniumClient()
57      {
58          return SeleniumStarter.getInstance().getSeleniumClient(getSeleniumConfiguration());
59      }
60  
61      /**
62       * To be overridden in the case of test-specific setup activities
63       */
64      protected void onSetUp()
65      {
66      }
67  
68      /**
69       * Calls overridden onTearDown method before shutting down
70       * the selenium client and possibly server
71       */
72      public final void tearDown() throws Exception
73      {
74          super.tearDown();
75          onTearDown();
76          if (SeleniumStarter.getInstance().isManual())
77          {
78              SeleniumStarter.getInstance().stop();
79          }
80      }
81  
82      /**
83       * To be overridden in the case of test-specific tear-down activities
84       */
85      protected  void onTearDown()
86      {
87      }
88  }