View Javadoc

1   package com.atlassian.webdriver.testing.rule;
2   
3   import com.atlassian.pageobjects.Page;
4   import com.atlassian.pageobjects.PageBinder;
5   import com.atlassian.pageobjects.ProductInstance;
6   import com.atlassian.pageobjects.TestedProduct;
7   import com.atlassian.pageobjects.TestedProductFactory;
8   import com.atlassian.webdriver.pageobjects.WebDriverTester;
9   import org.junit.rules.TestRule;
10  import org.junit.runner.Description;
11  import org.junit.runners.model.Statement;
12  
13  /**
14   * Creates a TestedProductRule which acts just like a TestedProduct.
15   * Allows setting the rule up in a test instead of having to define a TestedProduct in
16   * an @BeforeClass or @Before rule.
17   *
18   * <code>
19   * &#64;Rule public TestedProductRule product = new TestedProductRule(YourTestedProduct.class);
20   * </code>
21   *
22   * @since 2.1.0
23   * @see InjectionRules
24   * @see com.atlassian.webdriver.testing.runner.ProductContextRunner
25   */
26  public class TestedProductRule<T extends TestedProduct<WebDriverTester>> implements TestRule, TestedProduct<WebDriverTester>
27  {
28      private Class<T> testedProductClass;
29      private T product;
30  
31      public TestedProductRule(Class<T> testedProductClass)
32      {
33          this.testedProductClass = testedProductClass;
34      }
35  
36      public Statement apply(final Statement base, final Description description)
37      {
38          return new Statement() {
39              @Override
40              public void evaluate() throws Throwable
41              {
42                  product = TestedProductFactory.create(testedProductClass);
43                  base.evaluate();
44              }
45          };
46      }
47  
48      public <P extends Page> P visit(Class<P> pageClass, Object... args)
49      {
50          return product.visit(pageClass, args);
51      }
52  
53      public PageBinder getPageBinder()
54      {
55          return product.getPageBinder();
56      }
57  
58      public ProductInstance getProductInstance()
59      {
60          return product.getProductInstance();
61      }
62  
63      public WebDriverTester getTester()
64      {
65          return product.getTester();
66      }
67  
68      public T getTestedProduct()
69      {
70          return product;
71      }
72  
73  }