View Javadoc

1   package com.atlassian.pageobjects.elements;
2   
3   import com.atlassian.webdriver.AtlassianWebDriver;
4   import org.openqa.selenium.Keys;
5   import org.openqa.selenium.interactions.Action;
6   import org.openqa.selenium.interactions.Actions;
7   
8   import javax.inject.Inject;
9   
10  import static com.atlassian.pageobjects.elements.WebDriverElement.getWebElement;
11  
12  /**
13   * <p/>
14   * Wrapper around WebDriver's {@link org.openqa.selenium.interactions.Actions} class for convenient use
15   * with {@link com.atlassian.pageobjects.elements.PageElement}s. @Inject it into your page objects, or
16   * bind via {@link com.atlassian.pageobjects.PageBinder} to get instance explicitly.
17   *
18   * <p/>
19   * Build a sequence of actions to execute by using the builder-style methods of this class. After that
20   * {@link #build()} a resulting action to execute, or just {@link #perform()} the actions straight away.
21   *
22   * @see Actions
23   * @since 2.1
24   */
25  public class PageElementActions
26  {
27  
28      @Inject
29      private AtlassianWebDriver webDriver;
30  
31      private Actions actions;
32  
33      private Actions getActions()
34      {
35          if (actions == null)
36          {
37              actions = new Actions(webDriver.getDriver());
38          }
39          return actions;
40      }
41  
42      /**
43       * @see Actions#keyDown(org.openqa.selenium.Keys)
44       */
45      public PageElementActions keyDown(Keys theKey)
46      {
47          getActions().keyDown(theKey);
48          return this;
49      }
50  
51      /**
52       * @see Actions#keyDown(org.openqa.selenium.WebElement, org.openqa.selenium.Keys)
53       */
54      public PageElementActions keyDown(PageElement element, Keys theKey)
55      {
56          getActions().keyDown(getWebElement(element), theKey);
57          return this;
58      }
59  
60      /**
61       * @see Actions#keyUp(org.openqa.selenium.Keys)
62       */
63      public PageElementActions keyUp(Keys theKey)
64      {
65          getActions().keyUp(theKey);
66          return this;
67      }
68  
69      /**
70       * @see Actions#keyUp(org.openqa.selenium.WebElement, org.openqa.selenium.Keys)
71       */
72      public PageElementActions keyUp(PageElement element, Keys theKey)
73      {
74          getActions().keyUp(getWebElement(element), theKey);
75          return this;
76      }
77  
78      /**
79       * @see Actions#sendKeys(CharSequence...)
80       */
81      public PageElementActions sendKeys(CharSequence... keysToSend)
82      {
83          getActions().sendKeys(keysToSend);
84          return this;
85      }
86  
87      /**
88       * @see Actions#sendKeys(org.openqa.selenium.WebElement, CharSequence...)
89       */
90      public PageElementActions sendKeys(PageElement element, CharSequence... keysToSend)
91      {
92          getActions().sendKeys(getWebElement(element), keysToSend);
93          return this;
94      }
95  
96      /**
97       * @see org.openqa.selenium.interactions.Actions#clickAndHold(org.openqa.selenium.WebElement)
98       */
99      public PageElementActions clickAndHold(PageElement onElement)
100     {
101         getActions().clickAndHold(getWebElement(onElement));
102         return this;
103     }
104 
105     /**
106      * @see org.openqa.selenium.interactions.Actions#clickAndHold()
107      */
108     public PageElementActions clickAndHold()
109     {
110         getActions().clickAndHold();
111         return this;
112     }
113 
114     /**
115      * @see org.openqa.selenium.interactions.Actions#release(org.openqa.selenium.WebElement)
116      */
117     public PageElementActions release(PageElement onElement)
118     {
119         getActions().release(getWebElement(onElement));
120         return this;
121     }
122 
123     /**
124      * @see org.openqa.selenium.interactions.Actions#release()
125      */
126     public PageElementActions release()
127     {
128         getActions().release();
129         return this;
130     }
131 
132     /**
133      * @see Actions#click(org.openqa.selenium.WebElement)
134      */
135     public PageElementActions click(PageElement onElement)
136     {
137         getActions().click(getWebElement(onElement));
138         return this;
139     }
140 
141     /**
142      * @see org.openqa.selenium.interactions.Actions#click()
143      */
144     public PageElementActions click() {
145         getActions().click();
146         return this;
147     }
148 
149     /**
150      * @see org.openqa.selenium.interactions.Actions#doubleClick(org.openqa.selenium.WebElement)
151      */
152     public PageElementActions doubleClick(PageElement onElement)
153     {
154         getActions().doubleClick(getWebElement(onElement));
155         return this;
156     }
157 
158     /**
159      * @see org.openqa.selenium.interactions.Actions#doubleClick()
160      */
161     public PageElementActions doubleClick()
162     {
163         getActions().doubleClick();
164         return this;
165     }
166 
167     /**
168      * @see Actions#moveToElement(org.openqa.selenium.WebElement)
169      */
170     public PageElementActions moveToElement(PageElement toElement)
171     {
172         getActions().moveToElement(getWebElement(toElement));
173         return this;
174     }
175 
176     /**
177      * @see Actions#moveToElement(org.openqa.selenium.WebElement, int, int)
178      */
179     public PageElementActions moveToElement(PageElement toElement, int xOffset, int yOffset)
180     {
181         getActions().moveToElement(getWebElement(toElement), xOffset, yOffset);
182         return this;
183     }
184 
185     /**
186      * @see Actions#moveByOffset(int, int)
187      */
188     public PageElementActions moveByOffset(int xOffset, int yOffset)
189     {
190         getActions().moveByOffset(xOffset, yOffset);
191         return this;
192     }
193 
194     /**
195      * @see Actions#contextClick(org.openqa.selenium.WebElement)
196      */
197     public PageElementActions contextClick(PageElement onElement)
198     {
199         getActions().contextClick(getWebElement(onElement));
200         return this;
201     }
202 
203     /**
204      * @see Actions#dragAndDrop(org.openqa.selenium.WebElement, org.openqa.selenium.WebElement)
205      */
206     public PageElementActions dragAndDrop(PageElement source, PageElement target)
207     {
208         getActions().dragAndDrop(getWebElement(source), getWebElement(target));
209         return this;
210     }
211 
212     /**
213      * @see Actions#dragAndDropBy(org.openqa.selenium.WebElement, int, int)
214      */
215     public PageElementActions dragAndDropBy(PageElement source, int xOffset, int yOffset) {
216         getActions().dragAndDropBy(getWebElement(source), xOffset, yOffset);
217         return this;
218     }
219 
220     /**
221      * Build an action to execute out of the provided sequence. This builder will be reset after calling that method.
222      *
223      * @return
224      */
225     Action build()
226     {
227         return getActions().build();
228     }
229 
230     /**
231      * Execute the specified series of actions. This also resets the actions.
232      */
233     public void perform()
234     {
235         getActions().perform();
236     }
237 
238     /**
239      * Reset the sequence of actions to execute.
240      *
241      * @return this actions instance
242      */
243     public PageElementActions reset()
244     {
245         getActions().build(); // resets
246         return this;
247     }
248 }