View Javadoc

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  * Utility for opening new window sessions.
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       * Opens a new window with the given name
35       *
36       * @param newWindow the name of the new window
37       * @return a window that you can switch to when you want focus
38       */
39      public BrowserWindow openNewWindow(final String newWindow)
40      {
41          return new BrowserWindow(newWindow).open();
42      }
43  
44      /**
45       * Get window by given name, the window must already be open
46       *
47       * @param windowName name of the window
48       * @return browser window
49       * @throws IllegalStateException if window with <tt>windowName</tt> is currently not open
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       * <p/>
74       * Timed condition to check and wait for a given window to be open.
75       *
76       * <p/>
77       * NOTE: this does NOT mean that this window has focus, it only means that it is one of the driver windows that
78       * the underlying driver is aware of. Use {@link com.atlassian.pageobjects.elements.WindowSession.BrowserWindow#switchTo()}
79       * to switch driver's focus to given window.
80       *
81       * @param windowName name of the window
82       * @param timeoutType timeout for the returned condition
83       * @return timed condition checking whether the window is open
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      * Timed condition to check and wait for a given window to be open. The condition will wait with a timeout
107      * of {@link com.atlassian.pageobjects.elements.timeout.TimeoutType#PAGE_LOAD}.
108      *
109      * @param windowName name of the window
110      * @return timed condition checking whether the window is open
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          * Switch to this window. Future webdriver commands
144          * will be done in this window
145          *
146          * @return the new active window
147          */
148         public BrowserWindow switchTo()
149         {
150             checkIsOpen();
151             driver.switchTo().window(windowName);
152             return this;
153         }
154 
155         /**
156          * Switch back to default window and return parent window session
157          *
158          * @return the session this window was associated with
159          */
160         public WindowSession switchBack()
161         {
162             return WindowSession.this.switchToDefault();
163         }
164 
165         /**
166          * Closes this window. Remember to {@link com.atlassian.pageobjects.elements.WindowSession#switchToDefault()}
167          * after this.
168          *
169          * @return parent window session
170          */
171         public WindowSession close()
172         {
173             driver.switchTo().window(windowName);
174             driver.executeScript("self.close()");
175             return switchBack();
176         }
177 
178 
179         /**
180          * Execute some operations within this window and switch back to the default window.
181          *
182          * @param runnable operations to execute
183          * @return parent window session
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 }