Slide action on an element

06

Dear experts,

I am not able to figure out how to do a slide action on any of the sliders in the image.

Most of the documentation I am following talks about swiping on the screen but on elements.

Thanks in advance for the help.

This is the a part of my scrolling method:

PointerInput indexFinger = new PointerInput(PointerInput.Kind.TOUCH, "indexFinger");
        Sequence scroll = new Sequence(indexFinger, 1);
        scroll.addAction(indexFinger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startPoint.getX(), startPoint.getY()));
        scroll.addAction(indexFinger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
        scroll.addAction(new Pause(indexFinger, pauseDuration));
        scroll.addAction(indexFinger.createPointerMove(scrollingSpeedDuration, PointerInput.Origin.viewport(), endPoint.getX(), endPoint.getY()));
        scroll.addAction(indexFinger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
        driver.perform(Collections.singletonList(scroll));

this should work for both android and iOS - just change the variables names and provide relevant startPoint and endPoint and it should “slide”.

to get the relevant points on the screen - first locate the element, the call element.getRect()

as well, you may want to remove the following line(pause):
scroll.addAction(new Pause(indexFinger, pauseDuration));
to create a clean swipe movement.

Note: I think there are platforms that if you call slider.sendKeys(“80”) for example - or something similar, it should set the value of the slider element. You can try it as well, I didn’t try it myself.

1 Like

Thanks for sharing.

Here is what I am doing in your shared snippet -

Duration scrollingSpeedDuration = Duration.ofMillis(200);

To get start X & Y co-ordinates:
int startX = webElement.getLocation().getX();
int startY = webElement.getLocation().getY();

Not sure how to get the endPoint X & Y.

slider.sendKeys(“1”) worked for me. The slider value supposes to be between 0 and 1.

Thanks for the help.

1 Like

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.offset.ElementOption;
import org.openqa.selenium.WebElement;

public class SliderAction {
private AndroidDriver driver;

public SliderAction(AndroidDriver<WebElement> driver) {
    this.driver = driver;
}

public void slideSlider(WebElement sliderElement, double slidePercentage) {
    int sliderWidth = sliderElement.getSize().getWidth();
    int sliderHeight = sliderElement.getSize().getHeight();
    int slidePosition = (int) (sliderWidth * slidePercentage);
    int centerX = sliderElement.getLocation().getX() + (sliderWidth / 2);
    int centerY = sliderElement.getLocation().getY() + (sliderHeight / 2);
    int startX = centerX - (sliderWidth / 2);
    int endX = startX + slidePosition;
    TouchAction touchAction = new TouchAction(driver);
    touchAction.longPress(ElementOption.element(sliderElement, startX, centerY))
            .moveTo(ElementOption.element(sliderElement, endX, centerY))
            .release()
            .perform();
}

}

1 Like

Thanks Siva.

´sendKeys()` worked like a charm in my case. I hope it stays the same :wink:

To my surprise, TouchAction didn’t work for me. I tried it before testing it with sendkeys()