View Javadoc

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