下面列出了怎么用org.openqa.selenium.interactions.touch.TouchActions的API类实例代码及写法,或者点击链接到github查看源代码。
private void clickAt( WebElement webElement, WebDriver webDriver, int x, int y )
{
if ( webElement != null )
{
Dimension elementSize = webElement.getSize();
int useX = (int) ((double) elementSize.getWidth() * ((double) x / 100.0)) + webElement.getLocation().getX();
int useY = (int) ((double) elementSize.getHeight() * ((double) y / 100.0)) + webElement.getLocation().getY();
if ( ((DeviceWebDriver) webDriver).getNativeDriver() instanceof AppiumDriver )
{
new TouchAction( (AppiumDriver) ((DeviceWebDriver) webDriver).getNativeDriver() ).press( PointOption.point( useX, useY ) ).perform();
}
else if ( ((DeviceWebDriver) webDriver).getNativeDriver() instanceof RemoteWebDriver )
{
if ( ((DeviceWebDriver) webDriver).getNativeDriver() instanceof HasTouchScreen )
new TouchActions( webDriver ).move( useX, useY ).click().build().perform();
else
new Actions( webDriver ).moveByOffset(useX, useY ).click().build().perform();
}
}
}
/**
* Moves the focus to the specified element. Currently issued with tap
*/
@PublicAtsApi
public void focus() {
try {
MobileElement mobileElement = (MobileElement) MobileElementFinder.findElement(appiumDriver,
element);
// use tap to focus
new TouchActions(appiumDriver).singleTap(mobileElement).perform();
} catch (Exception se) {
throw new MobileOperationException("Error trying to set the focus to " + getElementDescription(),
se);
}
}
@Override
protected boolean _executeGesture( WebDriver webDriver )
{
TouchActions dAction = new TouchActions( (WebDriver) ( (DeviceWebDriver) webDriver ).getNativeDriver() );
WebElement fromElement = (WebElement) getFromElement().getNative();
WebElement toElement = (WebElement) getToElement().getNative();
dAction.clickAndHold( fromElement ).moveToElement( toElement ).release().perform();
return true;
}
@Override
protected boolean _executeGesture( WebDriver webDriver )
{
WebElement fromElement = (WebElement) getFromElement().getNative();
Point fromPoint = new Point( fromElement.getLocation().getX() + ( fromElement.getSize().getWidth() / 2 ), fromElement.getLocation().getY() + ( fromElement.getSize().getHeight() / 2 ) );
Point toPoint = new Point( fromElement.getLocation().getX() + ( fromElement.getSize().getWidth() / 2 ), fromElement.getLocation().getY() + ( fromElement.getSize().getHeight() / 2 ) );
TouchActions tActions = new TouchActions( (WebDriver) ( (DeviceWebDriver) webDriver).getNativeDriver() );
tActions.down( fromPoint.getX(), fromPoint.getY() ).move( toPoint.getX(), toPoint.getY() ).release().perform();
return true;
}
public boolean _moveTo()
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchAction((AppiumDriver) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).moveTo(createPoint(webElement)).perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof HasTouchScreen)
new TouchActions(getWebDriver()).moveToElement(webElement).build().perform();
else
new Actions(getWebDriver()).moveToElement(webElement).build().perform();
}
return true;
}
}
return false;
}
public boolean _press()
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchAction((AppiumDriver) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).press(createPoint(webElement)).perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof HasTouchScreen)
new TouchActions(getWebDriver()).clickAndHold(webElement).build().perform();
else
new Actions(getWebDriver()).clickAndHold(webElement).build().perform();
}
return true;
}
}
return false;
}
public boolean _mouseDoubleClick()
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchActions((AppiumDriver) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).doubleTap(webElement).perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof HasTouchScreen)
new TouchActions(getWebDriver()).doubleClick().build().perform();
else
new Actions(getWebDriver()).doubleClick(webElement).build().perform();
}
return true;
}
}
return false;
}
public boolean _release()
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchAction((AppiumDriver) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).release().perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof HasTouchScreen)
new TouchActions(getWebDriver()).release().build().perform();
else
new Actions(getWebDriver()).release().build().perform();
}
return true;
}
}
return false;
}
public void androidSwipe(int startX, int startY, int endX, int endY) throws Exception {
TouchActions actions = new TouchActions(driver);
actions.down(startX, startY).perform();
sleep(0.5);
actions.move(endX, endY).perform();
actions.up(endX, endY).perform();
}
public void swipe(double startX, double startY, double endX, double endY) throws Exception {
TouchActions actions = new TouchActions(driver);
Dimension size = driver.manage().window().getSize();
Point screen = new Point(size.getWidth(), size.getHeight());
Point swipe_start = new Point(screen.x * startX, screen.y * startY);
Point swipe_end = new Point(screen.x * endX, screen.y * endY);
if (PlatformType.IOS.equals(PlatformType.IOS)) {
iOSSwipe((int) swipe_start.x, (int) swipe_start.y, (int) swipe_end.x, (int) swipe_end.y);
} else {
androidSwipe((int) swipe_start.x, (int) swipe_start.y, (int) swipe_end.x, (int) swipe_end.y);
}
}
@Test
public void testSlider() throws Exception {
//get the slider
WebElement slider = driver.findElement(By.xpath("//seek[1]"));
// assertEquals(slider.getAttribute("value"), "50%");
TouchActions drag = new TouchActions(driver).flick(slider, new Integer(-1), 0, 0);
// drag.perform();
// assertEquals(slider.getAttribute("value"), "0%");
}
@Test
public void testSlider() throws Exception {
//get the slider
WebElement slider = driver.findElement(By.xpath("//slider[1]"));
assertEquals(slider.getAttribute("value"), "50%");
TouchActions drag = new TouchActions(driver).flick(slider, new Integer(-1), 0, 0);
drag.perform();
assertEquals(slider.getAttribute("value"), "0%");
}
public boolean _clickFor( int length )
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchAction((AppiumDriver<?>) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).press(createPoint(webElement)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(length))).release().perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
CloudActionProvider aP = getWebDriver().getCloud().getCloudActionProvider();
Point clickLocation = aP.translatePoint(getWebDriver(), webElement.getLocation());
Dimension clickSize = aP.translateDimension(getWebDriver(), webElement.getSize());
Dimension windowSize = aP.translateDimension(getWebDriver(), getWebDriver().manage().window().getSize());
double x = clickLocation.getX() + (clickSize.getWidth() / 2);
double y = clickLocation.getY() + (clickSize.getHeight() / 2);
int percentX = (int) (x / windowSize.getWidth() * 100.0);
int percentY = (int) (y / windowSize.getHeight() * 100.0);
try
{
new TouchActions(getWebDriver()).longPress(webElement).build().perform();
}
catch (Exception e)
{
((DeviceWebDriver) getWebDriver()).getCloud().getCloudActionProvider().tap((DeviceWebDriver) getWebDriver(), new PercentagePoint(percentX, percentY, true), length);
}
}
return true;
}
}
return false;
}
public void selendroidTapAtCoordinate(int x, int y, int secs) throws Exception {
TouchActions actions = new TouchActions(driver);
actions.down(x, y).perform();
sleep(secs);
actions.up(x, y).perform();
}