1 package com.atlassian.webdriver.testing.rule;
2
3 import com.atlassian.webdriver.testing.annotation.WindowSize;
4 import com.google.common.base.Supplier;
5 import org.junit.rules.TestWatcher;
6 import org.junit.runner.Description;
7 import org.openqa.selenium.Dimension;
8 import org.openqa.selenium.Point;
9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.WebDriverException;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import javax.inject.Inject;
15
16 import static com.google.common.base.Preconditions.checkState;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public class WindowSizeRule extends TestWatcher
69 {
70 private final static Logger log = LoggerFactory.getLogger(WindowSizeRule.class);
71
72 private final WebDriverSupport<? extends WebDriver> support;
73
74 @Inject
75 public WindowSizeRule(WebDriver webDriver)
76 {
77 this.support = WebDriverSupport.forInstance(webDriver);
78 }
79
80 public WindowSizeRule(Supplier<? extends WebDriver> driverSupplier)
81 {
82 this.support = WebDriverSupport.forSupplier(driverSupplier);
83 }
84
85 public WindowSizeRule()
86 {
87 this.support = WebDriverSupport.fromAutoInstall();
88 }
89
90 @Override
91 protected void starting(Description description)
92 {
93 try
94 {
95 WindowSize windowSize = findAnnotation(description);
96 if (windowSize != null)
97 {
98 handleWindowSize(windowSize);
99 }
100 else
101 {
102 maximizeWindow();
103 }
104 }
105 catch (WebDriverException e)
106 {
107
108 log.warn("Caught exception while trying to adjust window size.");
109 log.debug("Exception while trying to adjust window size", e);
110 }
111 }
112
113 private WindowSize findAnnotation(Description description)
114 {
115 if (description.getAnnotation(WindowSize.class) != null)
116 {
117 return description.getAnnotation(WindowSize.class);
118 }
119 else if (description.getTestClass() != null && description.getTestClass().isAnnotationPresent(WindowSize.class))
120 {
121 return description.getTestClass().getAnnotation(WindowSize.class);
122 }
123 else
124 {
125 return null;
126 }
127 }
128
129 private void handleWindowSize(WindowSize windowSize)
130 {
131 if (windowSize.maximize())
132 {
133 maximizeWindow();
134 }
135 else
136 {
137 setSize(computeDimension(windowSize));
138 }
139 }
140
141 private void maximizeWindow()
142 {
143 support.getDriver().manage().window().maximize();
144 }
145
146 private void setSize(Dimension dimension)
147 {
148 support.getDriver().manage().window().setPosition(new Point(0,0));
149 support.getDriver().manage().window().setSize(dimension);
150
151 support.getDriver().manage().window().setSize(dimension);
152 }
153
154 private Dimension computeDimension(WindowSize windowSize)
155 {
156 checkState(windowSize.width() > 0, "@WindowSize width must be greater than 0, was: " + windowSize.width());
157 checkState(windowSize.height() > 0, "@WindowSize height must be greater than 0, was: " + windowSize.height());
158 return new Dimension(windowSize.width(), windowSize.height());
159 }
160 }