1 package com.atlassian.pageobjects.elements;
2
3 import com.atlassian.pageobjects.elements.query.AbstractTimedCondition;
4 import com.atlassian.pageobjects.elements.query.TimedCondition;
5 import com.atlassian.pageobjects.elements.timeout.TimeoutType;
6 import com.atlassian.pageobjects.elements.timeout.Timeouts;
7 import com.atlassian.webdriver.AtlassianWebDriver;
8 import com.google.common.base.Function;
9 import org.openqa.selenium.NoSuchWindowException;
10
11 import javax.annotation.Nullable;
12 import javax.inject.Inject;
13
14 import static com.google.common.base.Preconditions.checkState;
15
16
17
18
19 public class WindowSession
20 {
21 private final String defaultWindow;
22 private final AtlassianWebDriver driver;
23 private final Timeouts timeouts;
24
25 @Inject
26 WindowSession(AtlassianWebDriver driver, Timeouts timeouts)
27 {
28 this.driver = driver;
29 this.timeouts = timeouts;
30 this.defaultWindow = driver.getWindowHandle();
31 }
32
33
34
35
36
37
38
39 public BrowserWindow openNewWindow(final String newWindow)
40 {
41 return new BrowserWindow(newWindow).open();
42 }
43
44
45
46
47
48
49
50
51 public BrowserWindow getWindow(String windowName)
52 {
53 return new BrowserWindow(windowName);
54 }
55
56 private void checkIsOpen(String windowName)
57 {
58 checkState(isWindowOpen(windowName).now(), "Window '" + windowName + "' is not open");
59 }
60
61 public BrowserWindow defaultWindow()
62 {
63 return new BrowserWindow(defaultWindow);
64 }
65
66 public WindowSession switchToDefault()
67 {
68 driver.switchTo().window(defaultWindow);
69 return this;
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 public TimedCondition isWindowOpen(final String windowName, TimeoutType timeoutType)
86 {
87 return new AbstractTimedCondition(timeouts.timeoutFor(timeoutType), timeouts.timeoutFor(TimeoutType.EVALUATION_INTERVAL))
88 {
89 @Override
90 protected Boolean currentValue()
91 {
92 try
93 {
94 driver.switchTo().window(windowName);
95 return true;
96 }
97 catch (NoSuchWindowException e)
98 {
99 return false;
100 }
101 }
102 };
103 }
104
105
106
107
108
109
110
111
112 public TimedCondition isWindowOpen(final String windowName)
113 {
114 return isWindowOpen(windowName, TimeoutType.PAGE_LOAD);
115 }
116
117 public class BrowserWindow
118 {
119 private final String windowName;
120
121 private BrowserWindow(final String windowName)
122 {
123 this.windowName = windowName;
124 }
125
126 public TimedCondition isOpen()
127 {
128 return isWindowOpen(windowName);
129 }
130
131 private void checkIsOpen()
132 {
133 WindowSession.this.checkIsOpen(windowName);
134 }
135
136 public BrowserWindow open()
137 {
138 driver.executeScript("window.open('', '" + windowName + "')");
139 return this;
140 }
141
142
143
144
145
146
147
148 public BrowserWindow switchTo()
149 {
150 checkIsOpen();
151 driver.switchTo().window(windowName);
152 return this;
153 }
154
155
156
157
158
159
160 public WindowSession switchBack()
161 {
162 return WindowSession.this.switchToDefault();
163 }
164
165
166
167
168
169
170
171 public WindowSession close()
172 {
173 driver.switchTo().window(windowName);
174 driver.executeScript("self.close()");
175 return switchBack();
176 }
177
178
179
180
181
182
183
184
185 public WindowSession doInWindow(Runnable runnable)
186 {
187 switchTo();
188 try
189 {
190 runnable.run();
191 return WindowSession.this;
192 }
193 finally
194 {
195 switchBack();
196 }
197 }
198
199 public <I,O> O doInWindow(@Nullable final I input, Function<I,O> function)
200 {
201 switchTo();
202 try
203 {
204 return function.apply(input);
205 }
206 finally
207 {
208 switchBack();
209 }
210 }
211 }
212 }