View Javadoc

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   * <p/>
20   * Implements support for the {@link com.atlassian.webdriver.testing.annotation.WindowSize} annotation.
21   *
22   * <p/>
23   * If this rule is used in a test and the {@link com.atlassian.webdriver.testing.annotation.WindowSize} annotation is
24   * not present on either the test method, or the test class, the window will be maximized by default.
25   *
26   * <p/>
27   * Otherwise the directives present on the annotation found (with method-level annotation superseding the class-level
28   * one) will be applied to the current driver's window in a following manner:
29   * <ul>
30   *     <li>if the <tt>maximize</tt> flag is set to <code>true</code>, the window will be maximized</li>
31   *     <li>otherwise the properties <tt>height</tt> and <tt>width</tt> will be used to set the window size. In such
32   *     case those properties must have positive integer value, or an exception will be raised</li>
33   * </ul>
34   *
35   * <p/>
36   * Example:
37   *
38   *
39   * <pre>
40   *      public class MyWebDriverTest
41   *      {
42   *          &#064;Rule public WindowSizeRule windowSizeRule = //...
43   *
44   *          &#064;Test
45   *          public void myTest()
46   *          {
47   *              // at this stage the window will be maximized
48   *          }
49   *      }
50   * </pre>
51   *
52   * <pre>
53   *      public class MyWebDriverTest
54   *      {
55   *          &#064;Rule public WindowSizeRule windowSizeRule = //...
56   *
57   *          &#064;Test
58   *          &#064;WindowSize(width=1024,height=768)
59   *          public void myTest()
60   *          {
61   *              // at this stage the window will be set to 1024x768
62   *          }
63   *      }
64   * </pre>
65   *
66   * @since 2.1
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             // half of the drivers does not support it, let's not break because of this
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         // _not_ a mistake... don't ask
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 }