View Javadoc

1   package com.atlassian.webdriver.testing.rule;
2   
3   import com.atlassian.webdriver.WebDriverFactory;
4   import com.atlassian.webdriver.testing.annotation.TestBrowser;
5   import org.junit.Before;
6   import org.junit.rules.TestRule;
7   import org.junit.runner.Description;
8   import org.junit.runners.model.Statement;
9   
10  /**
11   * If a test method, class or package has been annotated
12   * with the {@link TestBrowser} annotation then the
13   * webdriver.browser system property will be set to use to the
14   * value of the annotation otherwise it will use the default value.
15   * In order for this to be used in tests it requires that the test
16   * creates the TestedProduct each test method execution using {@link Before}
17   * or uses the {@link TestedProductRule}
18   *
19   * If using the {@link TestedProductRule} ensure that it is defined
20   * before this rule.
21   *
22   * eg.
23   * <code>
24   *     @Rule public TestedProductRule product = new TestedProductRule()
25   *     @Rule public TestBrowserRule testBrowserRule = new TestBrowserRule();
26   * </code>
27   *
28   * @since 2.1.0
29   * @deprecated Use {@link com.atlassian.webdriver.testing.rule.IgnoreBrowserRule} instead, which handles all browser-related
30   * annotations.
31   */
32  public class TestBrowserRule implements TestRule
33  {
34      private static String originalBrowserValue = WebDriverFactory.getBrowserProperty();
35  
36      public Statement apply(final Statement base, final Description description)
37      {
38          return new Statement()
39          {
40              @Override
41              public void evaluate() throws Throwable
42              {
43                  TestBrowser testBrowser = getTestBrowserAnnotation(description);
44                  if (testBrowser != null)
45                  {
46                      System.setProperty("webdriver.browser", testBrowser.value());
47                  }
48                  else
49                  {
50                      System.setProperty("webdriver.browser", originalBrowserValue);
51                  }
52                  base.evaluate();
53              }
54          };
55      }
56  
57      /**
58       * Checks if there is a {@link TestBrowser} annotation on the test method, class or package
59       * @return the {@link TestBrowser} annotation or null if there isn't one defined.
60       */
61      private TestBrowser getTestBrowserAnnotation(Description description)
62      {
63          TestBrowser methodTestBrowser = description.isTest() ? description.getAnnotation(TestBrowser.class) : null;
64          TestBrowser classTestBrowser = description.getTestClass().getAnnotation(TestBrowser.class);
65          TestBrowser packageTestBrowser = description.getTestClass().getPackage().getAnnotation(TestBrowser.class);
66  
67          return methodTestBrowser != null ? methodTestBrowser :
68              (classTestBrowser != null ? classTestBrowser : packageTestBrowser);
69  
70      }
71  
72  }