View Javadoc

1   package com.atlassian.webdriver.rule;
2   
3   import com.atlassian.pageobjects.browser.Browser;
4   import com.atlassian.webdriver.testing.annotation.IgnoreBrowser;
5   import com.atlassian.webdriver.testing.annotation.TestBrowser;
6   import com.atlassian.webdriver.testing.rule.IgnoreBrowserRule;
7   import org.junit.Test;
8   import org.junit.internal.AssumptionViolatedException;
9   import org.junit.runner.RunWith;
10  import org.junit.runners.model.Statement;
11  import org.mockito.Mock;
12  import org.mockito.runners.MockitoJUnitRunner;
13  
14  import static org.hamcrest.Matchers.instanceOf;
15  import static org.hamcrest.Matchers.is;
16  import static org.junit.Assert.assertThat;
17  
18  /**
19   * <p/>
20   * Test case for {@link com.atlassian.webdriver.testing.rule.IgnoreBrowserRule}.
21   *
22   * <p/>
23   * This tests for the legacy annotations support and must be removed once
24   * {@link com.atlassian.webdriver.testing.annotation.IgnoreBrowser} and
25   * {@link com.atlassian.webdriver.testing.annotation.TestBrowser} are removed.
26   *
27   * @since 2.1
28   */
29  @RunWith(MockitoJUnitRunner.class)
30  public class TestIgnoreBrowserRuleLegacyAnnotations
31  {
32      @Mock private Statement statement;
33  
34      @Test
35      public void shouldIgnoreForAnnotatedTestMethod()
36      {
37          final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.HTMLUNIT);
38          final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
39                  Descriptions.forTest(MockClassWithIgnoreMethods.class, "methodAnnotatedWithIgnoreBrowserHtmlUnit")));
40          invoker.invokeSafely();
41          assertFailedAssumption(invoker);
42      }
43  
44      @Test
45      public void shouldIgnoreForAnnotatedTestMethodWithMultipleValues()
46      {
47          final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.ANDROID);
48          final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
49                  Descriptions.forTest(MockClassWithIgnoreMethods.class, "methodAnnotatedWithIgnoreBrowserMultipleValues")));
50          invoker.invokeSafely();
51          assertFailedAssumption(invoker);
52      }
53  
54      @Test
55      public void shouldNotIgnoreForAnnotatedTestMethodIfNotMatching()
56      {
57          final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.FIREFOX);
58          final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
59                  Descriptions.forTest(MockClassWithIgnoreMethods.class, "methodAnnotatedWithIgnoreBrowserHtmlUnit")));
60          invoker.invokeSafely();
61          assertThat(invoker.isSuccess(), is(true));
62      }
63  
64      @Test
65      public void shouldIgnoreForAnnotatedMethodWithIgnoreAll()
66      {
67          final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.CHROME);
68          final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
69                  Descriptions.forTest(MockClassWithIgnoreMethods.class, "methodAnnotatedWithIgnoreAll")));
70          invoker.invokeSafely();
71          assertFailedAssumption(invoker);
72      }
73  
74      @Test
75      public void shouldIgnoreForIgnoreBrowserOnClass()
76      {
77          final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.FIREFOX);
78          final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
79                  Descriptions.forTest(MockIgnoreBrowserClass.class, "someMethod")));
80          invoker.invokeSafely();
81          assertFailedAssumption(invoker);
82      }
83  
84      @Test
85      public void shouldNotIgnoreForAnnotatedClassIfBrowserNotMatching()
86      {
87          final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.CHROME);
88          final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
89                  Descriptions.forTest(MockIgnoreBrowserClass.class, "someMethod")));
90          invoker.invokeSafely();
91          assertThat(invoker.isSuccess(), is(true));
92      }
93  
94  
95      @Test
96      public void shouldSkipForAnnotatedTestMethod()
97      {
98          final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.CHROME);
99          final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
100                 Descriptions.forTest(MockClassWithRequireMethods.class, "methodAnnotatedWithRequireBrowserHtmlUnit")));
101         invoker.invokeSafely();
102         assertFailedAssumption(invoker);
103     }
104 
105 
106     @Test
107     public void shouldNotSkipForAnnotatedTestMethodIfMatching()
108     {
109         final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.HTMLUNIT);
110         final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
111                 Descriptions.forTest(MockClassWithRequireMethods.class, "methodAnnotatedWithRequireBrowserHtmlUnit")));
112         invoker.invokeSafely();
113         assertThat(invoker.isSuccess(), is(true));
114     }
115 
116     @Test
117     public void shouldNeverSkipForAnnotatedMethodWithRequireAll()
118     {
119         final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.CHROME);
120         final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
121                 Descriptions.forTest(MockClassWithRequireMethods.class, "methodAnnotatedWithRequireAll")));
122         invoker.invokeSafely();
123         assertThat(invoker.isSuccess(), is(true));
124     }
125 
126     @Test
127     public void shouldSkipForRequireBrowserOnClass()
128     {
129         final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.CHROME);
130         final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
131                 Descriptions.forTest(MockRequireBrowserClass.class, "someMethod")));
132         invoker.invokeSafely();
133         assertFailedAssumption(invoker);
134     }
135 
136     @Test
137     public void shouldNotSkipForRequireBrowserOnClassIfBrowserMatching()
138     {
139         final IgnoreBrowserRule rule = new IgnoreBrowserRule(Browser.HTMLUNIT);
140         final SafeStatementInvoker invoker = new SafeStatementInvoker(rule.apply(statement,
141                 Descriptions.forTest(MockRequireBrowserClass.class, "someMethod")));
142         invoker.invokeSafely();
143         assertThat(invoker.isSuccess(), is(true));
144     }
145 
146     private void assertFailedAssumption(SafeStatementInvoker invoker)
147     {
148         assertThat(invoker.isSuccess(), is(false));
149         assertThat(invoker.getError(), instanceOf(AssumptionViolatedException.class));
150     }
151 
152 
153     public static class MockClassWithIgnoreMethods
154     {
155         @Test
156         @IgnoreBrowser(value = Browser.HTMLUNIT, reason = "For some reason")
157         public void methodAnnotatedWithIgnoreBrowserHtmlUnit() {}
158 
159         @Test
160         @IgnoreBrowser(value = { Browser.HTMLUNIT, Browser.ANDROID, Browser.FIREFOX }, reason = "For some reason")
161         public void methodAnnotatedWithIgnoreBrowserMultipleValues() {}
162 
163         @Test
164         @IgnoreBrowser(value = Browser.ALL, reason = "For some reason")
165         public void methodAnnotatedWithIgnoreAll() {}
166     }
167 
168     @IgnoreBrowser(value = {Browser.HTMLUNIT, Browser.FIREFOX}, reason = "For some reason")
169     public static class MockIgnoreBrowserClass
170     {
171         @Test
172         public void someMethod() {}
173     }
174 
175 
176     public static class MockClassWithRequireMethods
177     {
178         @Test
179         @TestBrowser("htmlunit")
180         public void methodAnnotatedWithRequireBrowserHtmlUnit() {}
181 
182         @Test
183         @TestBrowser("all")
184         public void methodAnnotatedWithRequireAll() {}
185     }
186 
187     @TestBrowser("htmlunit")
188     public static class MockRequireBrowserClass
189     {
190         @Test
191         public void someMethod() {}
192     }
193 }