Created
October 6, 2014 00:35
-
-
Save mspiegel/0eaee709716e24c110a4 to your computer and use it in GitHub Desktop.
Implementation of available actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.interactions.Action; | |
import org.openqa.selenium.interactions.Actions; | |
import org.openqa.selenium.support.ui.ExpectedCondition; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
/** | |
* The class {@link org.openqa.selenium.interactions.Actions} | |
* is throws exceptions for unsupported operations. This wrapper class is intended | |
* to skip those operations. The builder pattern returns | |
* objects of its own type in its method signatures. Therefore | |
* we have to override all the methods to return instances | |
* of the subclass. | |
*/ | |
public class AvailableActions extends Actions { | |
final String browserName; | |
public AvailableActions(WebDriver driver, String browserName) { | |
super(driver); | |
this.browserName = browserName; | |
} | |
/** | |
* Implementation of wait as an action. Provides a structured | |
* API to implement waiting within a sequence of actions. | |
*/ | |
private static final class WaitAction implements Action { | |
private final WebDriverWait wait; | |
private final ExpectedCondition<WebElement> condition; | |
public WaitAction(WebDriverWait wait, ExpectedCondition<WebElement> condition) { | |
this.wait = wait; | |
this.condition = condition; | |
} | |
@Override | |
public void perform() { | |
wait.until(condition); | |
} | |
} | |
/** | |
* The guarantees of {@link org.openqa.selenium.WebElement#click()} | |
* are stronger than the effects of | |
* {@link org.openqa.selenium.interactions.Mouse#click( | |
*org.openqa.selenium.interactions.internal.Coordinates)}. | |
* This provides the stronger click event. | |
*/ | |
private static final class RobustClickAction implements Action { | |
private final WebElement element; | |
public RobustClickAction(WebElement element) { | |
this.element = element; | |
} | |
@Override | |
public void perform() { | |
element.click(); | |
} | |
} | |
/** | |
* The safari and android drivers currently do not fully support the | |
* Interactions API so perform a robust click on these platforms. | |
* https://code.google.com/p/selenium/issues/detail?id=4136 | |
*/ | |
@Override | |
public AvailableActions click(WebElement element) { | |
if (browserName.equals("safari") || browserName.equals("android")) { | |
robustClick(element); | |
} else { | |
super.click(element); | |
} | |
return this; | |
} | |
/** | |
* The guarantees of {@link org.openqa.selenium.WebElement#click()} | |
* are stronger than the effects of | |
* {@link org.openqa.selenium.interactions.Mouse#click( | |
* org.openqa.selenium.interactions.internal.Coordinates)}. | |
* This provides the stronger click event. | |
* If this causes a new page to load, this method will | |
* attempt to block until the page has loaded. | |
*/ | |
public AvailableActions robustClick(WebElement element) { | |
moveToElement(element); | |
action.addAction(new RobustClickAction(element)); | |
return this; | |
} | |
/** | |
* The safari and android drivers currently do not fully support the | |
* Interactions API so just ignore these events. | |
* https://code.google.com/p/selenium/issues/detail?id=4136 | |
*/ | |
@Override | |
public AvailableActions moveToElement(WebElement toElement) { | |
if (browserName.equals("safari") || browserName.equals("android")) { | |
// do nothing | |
} else { | |
super.moveToElement(toElement); | |
} | |
return this; | |
} | |
public AvailableActions waitAction(WebDriverWait wait, ExpectedCondition<WebElement> condition) { | |
action.addAction(new WaitAction(wait, condition)); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment