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
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
59
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 }