How to swipe Left to Right in Appium? - appium

Since swipe() is deprecated, I am unable to swipe the screen from Left to Right. My App has 4 banners in it and I want to swipe to view all the banners.

This applies in all directions:
enum:
public enum DIRECTION {
DOWN, UP, LEFT, RIGHT;
}
actual code:
public static void swipe(MobileDriver driver, DIRECTION direction, long duration) {
Dimension size = driver.manage().window().getSize();
int startX = 0;
int endX = 0;
int startY = 0;
int endY = 0;
switch (direction) {
case RIGHT:
startY = (int) (size.height / 2);
startX = (int) (size.width * 0.90);
endX = (int) (size.width * 0.05);
new TouchAction(driver)
.press(startX, startY)
.waitAction(Duration.ofMillis(duration))
.moveTo(endX, startY)
.release()
.perform();
break;
case LEFT:
startY = (int) (size.height / 2);
startX = (int) (size.width * 0.05);
endX = (int) (size.width * 0.90);
new TouchAction(driver)
.press(startX, startY)
.waitAction(Duration.ofMillis(duration))
.moveTo(endX, startY)
.release()
.perform();
break;
case UP:
endY = (int) (size.height * 0.70);
startY = (int) (size.height * 0.30);
startX = (size.width / 2);
new TouchAction(driver)
.press(startX, startY)
.waitAction(Duration.ofMillis(duration))
.moveTo(startX, endY)
.release()
.perform();
break;
case DOWN:
startY = (int) (size.height * 0.70);
endY = (int) (size.height * 0.30);
startX = (size.width / 2);
new TouchAction(driver)
.press(startX, startY)
.waitAction(Duration.ofMillis(duration))
.moveTo(startX, endY)
.release()
.perform();
break;
}
}
usage:
swipe(driver,DIRECTION.RIGHT);
Hope this helps,

try below method. It works with Appium 1.16.0 version.
I created this method to swipe left or right based on a particular element location on the screen. It takes 3 parameters
Element X: It is the X coordinate of the element on which swipe touch action needs to be performed.
Element Y: It is the Y coordinate of the element.
Direction: Left/Right
//method to left and right swipe on the screen based on coordinates
public void swipeAction(int Xcoordinate, int Ycoordinate, String direction) {
//get device width and height
Dimension dimension = driver.manage().window().getSize();
int deviceHeight = dimension.getHeight();
int deviceWidth = dimension.getWidth();
System.out.println("Height x Width of device is: " + deviceHeight + " x " + deviceWidth);
switch (direction) {
case "Left":
System.out.println("Swipe Right to Left");
//define starting and ending X and Y coordinates
int startX=deviceWidth - Xcoordinate;
int startY=Ycoordinate; // (int) (height * 0.2);
int endX=Xcoordinate;
int endY=Ycoordinate;
//perform swipe from right to left
new TouchAction((AppiumDriver) driver).longPress(PointOption.point(startX, startY)).moveTo(PointOption.point(endX, endY)).release().perform();
break;
case "Right":
System.out.println("Swipe Left to Right");
//define starting X and Y coordinates
startX=Xcoordinate;
startY=Ycoordinate;
endX=deviceWidth - Xcoordinate;
endY=Ycoordinate;
//perform swipe from left to right
new TouchAction((AppiumDriver) driver).longPress(PointOption.point(startX, startY)).moveTo(PointOption.point(endX, endY)).release().perform();
break;
}
}
To fetch the element X,Y coordinates. try below methods
int elementX= driver.findElement(elementLocator).getLocation().getX();
int elementY= driver.findElement(elementLocator).getLocation().getY();

Assuming you created driver instance of AndroidDriver you can swipe left:
// Get location of element you want to swipe
WebElement banner = driver.findElement(<your_locator>);
Point bannerPoint = banner.getLocation();
// Get size of device screen
Dimension screenSize = driver.manage().window().getSize();
// Get start and end coordinates for horizontal swipe
int startX = Math.toIntExact(Math.round(screenSize.getWidth() * 0.8));
int endX = 0;
TouchAction action = new TouchAction(driver);
action
.press(PointOption.point(startX, bannerPoint.getY()))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
.moveTo(PointOption.point(endX, bannerPoint.getY()))
.release();
driver.performTouchAction(action);
Use latest appium-java-client 6.1.0 and Appium 1.8.x server

This should work,
Dimension size = driver.manage().window().getSize();
System.out.println(size.height+"height");
System.out.println(size.width+"width");
System.out.println(size);
int startPoint = (int) (size.width * 0.99);
int endPoint = (int) (size.width * 0.15);
int ScreenPlace =(int) (size.height*0.40);
int y=(int)size.height*20;
TouchAction ts = new TouchAction(driver);
//for(int i=0;i<=3;i++) {
ts.press(PointOption.point(startPoint,ScreenPlace ))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
.moveTo(PointOption.point(endPoint,ScreenPlace )).release().perform();

This is for iOS Mobile:
//Here i am trying to swipe list of images from right to left
//First i am getting parent element (table/cell) id
//Then using predicatestring am searching for the element present or not then trying to click
List<MobileElement> ele = getMobileElement(listBtnQuickLink).findElements(By.xpath(".//XCUIElementTypeButton"));
for(int i=1 ;i<=20;i++) {
MobileElement ele1 = ele.get(i);
String parentID = getMobileElement(listBtnQuickLink).getId();
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("element", parentID); //This is parent element id (not same element)
scrollObject.put("predicateString", "label == '"+ele1.getText()+"'");
scrollObject.put("direction", "left");
driver.executeScript("mobile:swipe", scrollObject); // scroll to the target element
System.out.println("Element is visible : "+ele1.isDisplayed());
}

Unfortunately i was noted that TouchAction doesn't work on Android 11 with Selenium 4. So if you use Selenide and Appium you can try this:
public class SwipeToLeft implements Command<SelenideElement> {
#Nullable
#Override
public SelenideElement execute(SelenideElement proxy, WebElementSource locator, #Nullable Object[] args) throws IOException {
Selenide.sleep(2000);
var driver = WebDriverRunner.getWebDriver();
var element = proxy.getWrappedElement();
((JavascriptExecutor) driver).executeScript("mobile: swipeGesture", ImmutableMap.of(
"elementId", ((RemoteWebElement) element).getId(),
"direction", "left",
"percent", 0.75
));
return proxy;
}
}
And then you can use:
$('your seleniumLocator').shouldBe(visible).execute(new SwipeToLeft());

Related

I have an area on which i have to draw something like signature how can i do this in appium version 1.15 java,Image link is attached

I have an area on which i have to draw something like signature how can I do this in appium version 1.15 java,Image link is attached.
I have tried actions and Touch Actions but it does not worked.
TouchActions action= new TouchActions(driver)
.longPress((WebElement) PointOption.point(464, 727)).(PointOption.point(977, 7)).release().perform();
Image of signature location
Swipe left using -
Dimension size = appiumDriver.manage().window().getSize();
int startx = (int) (size.width * 0.8);
int endx = (int) (size.width * 0.20);
int starty = size.height / 2;
appiumDriver.swipe(startx, starty, endx, starty, 1000);
Swipe right-
appiumDriver.context("NATIVE_APP");
Dimension size = appiumDriver.manage().window().getSize();
int endx = (int) (size.width * 0.8);
int startx = (int) (size.width * 0.20);
int starty = size.height / 2;
appiumDriver.swipe(startx, starty, endx, starty, 1000);
OR
new TouchAction(driver).longPress(250, 1200).moveTo(900, 1200).release().perform();

Appium scrollto()/hidekeyboard() methods are not working with client jar 3.3.0/3.4.0 in iphone

I have tried most of the possible method(java script/touchaction) but not able to scroll
if any one have solution please help me, thanks
Try below code:
Dimension size = driver.manage().window().getSize();
int x = size.width / 2;
int endy = (int) (size.height * 0.75);
int starty = (int) (size.height * 0.20);
driver.swipe(x, starty, x, endy, 1000);
For more details check below post:
Click Here

scroll functionality is not working in ipad, can anyone do help on this? using appium

I am trying to do scroll functionality ipad I tried all the possibility
ex: ScrollTo("") and ScrollToExtract() and also JavaScript am not able to scroll in ipad but iphone it's working fine
Use this method:
public void scrollDown() {
Dimension size = driver.manage().window().getSize();
int x = size.width / 2;
int starty = (int) (size.height * 0.85);
int endy = (int) (size.height * 0.10);
driver.swipe(x, starty, x, endy, 2000);
}

Improve javaFx processing performance

I'm working on Image processing with javaFx. I think that my code is not enouth efficient (With HD images, refresh is very slow). Because I do a for on each pixel of my image everytime I have to refresh it. But I don't know how to do differently.
So I need help to improve the performance of my processing.
This is my code :
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Example extends Application {
private Image src;
private WritableImage dest;
private int width;
private int height;
int value = 0;
#Override
public void start(Stage stage) {
AnchorPane root = new AnchorPane();
initImage(root);
Scene scene = new Scene(root);
stage.setTitle("Demo processing");
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
private void initImage(AnchorPane root) {
src = new Image(
"http://mikecann.co.uk/wp-content/uploads/2009/12/ScreenHunter_02-Dec.-10-19.41-1024x484.jpg");
width = (int) src.getWidth();
height = (int) src.getHeight();
root.setPrefSize(800, 800 + 50);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setPrefHeight(600);
scrollPane.setPrefWidth(1000);
dest = new WritableImage(width, height);
ImageView destView = new ImageView(dest);
scrollPane.setContent(destView);
root.getChildren().add(scrollPane);
AnchorPane.setTopAnchor(scrollPane, 0.0);
Slider slider = new Slider(0, 255, 1);
slider.setPrefSize(800, 50);
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
slider.setSnapToTicks(true);
slider.setMajorTickUnit(1.0);
slider.setMinorTickCount(0);
slider.setLayoutY(700);
slider.valueProperty().addListener(new InvalidationListener() {
#Override
public void invalidated(Observable o) {
value = (int) ((DoubleProperty) o).get();
color();
}
});
root.getChildren().add(slider);
color();
}
private void color() {
PixelReader reader = src.getPixelReader();
PixelWriter writer = dest.getPixelWriter();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Color color = reader.getColor(x, y);
double red = (double) value * x * y / (width * height) / 255;
double green = color.getGreen();
double blue = (double) value * ((width * height) - x * y)
/ (width * height) / 255;
writer.setColor(x, y, Color.color(red, green, blue));
}
}
}
public static void main(String[] args) {
launch(args);
}
}
And this is with a full HD image :
src = new Image(
"http://www.freedomwallpaper.com//nature-wallpaper-hd/hd_sunshine_hd.jpg");
Getitng color of each pixel in loop is too slow. So, get entire pixels first, and change colors, finally wirte changed colors with PixelWriter.
Like this
private void color() {
PixelReader reader = src.getPixelReader();
WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance();
int[] pixels = new int[width * height]; // Buffer for all pixels
reader.getPixels(0, 0, width, height, format, pixels, 0, width); // get all pixels by argb format
int alpha = 0xFF << 24;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int index = x + y * width;
int argb = pixels[index];
int red = value * x * y / (width * height);
int green = (argb >> 8) & 0xFF;
int blue = value * ((width * height) - x * y)
/ (width * height);
int newArgb = alpha | (red << 16) | (green << 8) | blue;
pixels[index] = newArgb;
}
}
PixelWriter writer = dest.getPixelWriter();
writer.setPixels(0, 0, width, height, format, pixels, 0, width); // write entire image
}

Add a label to a BlackBerry ListField

I have a implemented a ListField on BlackBerry. How do I add 3 labels to the list?
Follow this tutorial:
http://berrytutorials.blogspot.com/2009/11/create-custom-listfield-change.html
After completed, modify the extended ListField class by adding some extra components to your list (graphics.drawText(CALLBACK OBJECT, X, Y)). Change the String callback to an object of your type(or just an Array) with the availability for more elements.
EXAMPLE OF THE PAINT METHOD INSIDE THE EXTENDED LISTFIELD CLASS:
public void paint(Graphics graphics) {
int width = (int) (300 * resizeWidthFactor);
// Get the current clipping region
XYRect redrawRect = graphics.getClippingRect();
// Side lines
// graphics.setColor(Color.GRAY);
// graphics.drawLine(0, 0, 0, redrawRect.height);
// graphics.setColor(Color.GRAY);
// graphics.drawLine(redrawRect.width-1, 0, redrawRect.width-1,
// redrawRect.height);
if (redrawRect.y < 0) {
throw new IllegalStateException("Error with clipping rect.");
}
// Determine the start location of the clipping region and end.
int rowHeight = getRowHeight();
int curSelected;
// If the ListeField has focus determine the selected row.
if (hasFocus) {
curSelected = getSelectedIndex();
} else {
curSelected = -1;
}
int startLine = redrawRect.y / rowHeight;
int endLine = (redrawRect.y + redrawRect.height - 1) / rowHeight;
endLine = Math.min(endLine, getSize() - 1);
int y = (startLine * rowHeight) + heightMargin;
// Setup the data used for drawing.
int[] yInds = new int[] { y, y, y + rowHeight, y + rowHeight };
int[] xInds = new int[] { 0, width, width, 0 };
// Set the callback - assuming String values.
ListFieldCallback callBack = this.getCallback();
// Draw each row
for (; startLine <= endLine; ++startLine) {
// If the line we're drawing is the currentlySelected line then draw the
// fill path in LIGHTYELLOW and the
// font text in Black.
//OBJECT OF OWN TYPE FOR MULTIPLE PARAMETERS
ProductDetails data = (ProductDetails) callBack.get(this, startLine);
String productDescription = "";
String errorDescription = "";
if (data.isError()) {
errorDescription = TextLineSplitter.wrapString1Line(data.getErrorMessage(), (int) ((300 - (2 * widthMargin)) * resizeWidthFactor), getFont());
} else {
productDescription = TextLineSplitter.wrapString1Line(data.getProductDesc(), (int) ((300 - (2 * widthMargin)) * resizeWidthFactor), getFont());
}
// Set differences by row (selected or not)
if (startLine == curSelected) {
graphics.setColor(Color.WHITE);
} else {
// Draw the odd or selected rows.
graphics.setColor(Color.BLACK);
}
// Set text values
if (!data.isError()) {
// If no error found
//FIRST LABEL
graphics.setFont(getFont().derive(Font.BOLD));
graphics.drawText("Result search " + Integer.toString(data.getSearchId()) + ":", widthMargin, yInds[0]);
graphics.drawText(data.getManufacturerItemIdentifier(), widthMargin + (int) (140 * resizeWidthFactor), yInds[0]);
//SECOND LABEL
graphics.setFont(getFont().derive(Font.PLAIN));
graphics.drawText(productDescription, widthMargin, yInds[0] + (int) (20 * resizeHeightFactor));
} else {
// Error found
graphics.setColor(Color.GRAY);
graphics.setFont(getFont().derive(Font.BOLD));
graphics.drawText("Result search " + Integer.toString(data.getSearchId()) + ":", widthMargin, yInds[0]);
graphics.setFont(getFont().derive(Font.PLAIN));
graphics.drawText(errorDescription, widthMargin, yInds[0] + (int) (20 * resizeHeightFactor));
}
// Bottom line
if (startLine == endLine) {
graphics.setColor(Color.GRAY);
graphics.drawLine(0, yInds[2] - (heightMargin + 1), (int) (300 * resizeWidthFactor), yInds[2] - (heightMargin + 1));
}
// Horizontal lines
graphics.setColor(Color.GRAY);
graphics.drawLine(0, yInds[0] - heightMargin, (int) (300 * resizeWidthFactor), yInds[0] - heightMargin);
// Assign new values to the y axis moving one row down.
y += rowHeight;
yInds[0] = y;
yInds[1] = yInds[0];
yInds[2] = y + rowHeight;
yInds[3] = yInds[2];
}
// super.paint(graphics);
}

Resources