This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
13 / 37
Oct 2018

package project;

import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Scanner;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.PerformsTouchActions;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.offset.PointOption;
public class firstpro {static AppiumDriver driver = null;
public static void main(String[] args) {
caps.setCapability(“appPackage”, “com.daraz.android”);
caps.setCapability(“appActivity”, “com.lazada.activities.EnterActivity”);
caps.setCapability(“noReset”, true);
//Set ChromeDriver location
System.setProperty(“webdriver.chrome.driver”,“C:\Computer\PC\Documents\chromedriver_win32\chromedriver.exe”);
//Instantiate Appium Driver
try {
driver = new AndroidDriver(new URL(“http://0.0.0.0:4723/wd/hub”), caps);

			} catch (MalformedURLException e) {
			System.out.println(e.getMessage());
			}
			
			scrollTo();		
	}


public static void scrollTo(String direction, int times) throws InterruptedException {
	Thread.sleep(1000);
	if (direction.equals(“down”)) {
	Dimension dim = driver.manage().window().getSize();
	int width = dim.getWidth() / 2;
	for (int i = 0; i < times; i++) {
	int startY = (int) (dim.getHeight() * 0.7);
	int endY = (int) (dim.getHeight() * 0.5);
	new TouchAction(driver).press(point(width, startY)).waitAction(waitOptions(Duration.ofSeconds(1)))
	.moveTo(point(width, endY)).release().perform();

			}

		}
}

}

THIS IS MY CODE BUT ITS GIVING ERRORS. kindly tell me how to resolve it

I did try using this code, but it was difficult to understand. I’m a beginner so I’m really struggling with all this.

I have to produce a method that works on all applications and all of them have “android.widget.FrameLayout” in common or i guess it can be done by dimension. I really dont know so can you suggest a method that can work on all applications and not only this? Thank you for your reply :slight_smile:

man first of all
1- you should change “” because you take it as not programming chars
2- you should call your driver i don’t know what you named it
3- you should import all needed jars

Hi Sarah
Its really appreciated that you are struggling but the code which you share above is working with coordinates but the which i posted is working with text coordinates will change with the screen dimmision but the UIselector code which i share with you working with text so any type of application like native hybrid webapp can be handled with this code
Your code is pasted below in this i cannot understand what you required
public static void scrollTo(String direction, int times) throws InterruptedException {
Thread.sleep(1000);
if (direction.equals(“down”)) {
Dimension dim = driver.manage().window().getSize();
int width = dim.getWidth() / 2;
for (int i = 0; i < times; i++) {
int startY = (int) (dim.getHeight() * 0.7);
int endY = (int) (dim.getHeight() * 0.5);
new TouchAction(driver).press(point(width, startY)).waitAction(waitOptions(Duration.ofSeconds(1)))
.moveTo(point(width, endY)).release().perform();

so please use my code

which is easy i already comment on each line which you have mentioned as error

Everytime i execute your code, its giving me some errors so I have to change it to for the errors to be resolved
public static void scrollTo(String direction, int times) throws InterruptedException {
Thread.sleep(1000);
Object down = null;
if (direction.equals(down)) {
org.openqa.selenium.Dimension dim = driver.manage().window().getSize();
int width = dim.getWidth() / 2;
for (int i = 0; i < times; i++) {
int startY = (int) (dim.getHeight() * 0.7);
int endY = (int) (dim.getHeight() * 0.5);
new TouchAction(driver).press(point(width, startY)).waitAction(waitOptions(Duration.ofSeconds(1)))
.moveTo(point(width, endY)).release().perform();{

}

}
}
}

private static WaitOptions waitOptions(Duration ofSeconds) {
	// TODO Auto-generated method stub
	return null;
}


private static PointOption point(int width, int startY) {
	// TODO Auto-generated method stub
	return null;
}

and im calling it by scrollTo(“down”, 1);
But its still not working. Can you please help me with this?

Is scroll supported in 1.9.1 appium version. can someone share the code snippet if its working plz

i’m getting these errors do you know how to solve them?

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:13,“value”:“Drag did not complete successfully”}
[AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Matched JSONWP error code 13 to UnknownError

I know there has been many responses, here is what I use,

public boolean scrollToElement (By by) throws Exception {
  boolean isFoundTheElement = appiumServer.getDriver().findElements(by).size() > 0;
  while (isFoundTheElement == false) {
    swipeVertical(0.8, 0.1, 0.5, 2000);
    isFoundTheElement = appiumServer.getDriver().findElements(by).size() > 0;
  }

  return isFoundTheElement;
}

public void swipeVertical (
    double startPercentage, double finalPercentage, double anchorPercentage, int duration)
        throws Exception {
  Dimension size = appiumServer.getDriver().manage().window().getSize();
  int anchor = (int) (size.width * anchorPercentage);
  int startPoint = (int) (size.height * startPercentage);
  int endPoint = (int) (size.height * finalPercentage);
  getTouchAction().press(PointOption.point(anchor, startPoint))
  .waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
  .moveTo(PointOption.point(anchor, endPoint)).release().perform();
}

public TouchAction getTouchAction () {
  return new TouchAction(appiumServer.getDriver());
}

I was continously getting an error on "getDrive. So, i have changed the code a bit. But can you tell me how to call it in the main method?
public static boolean scrollToElement (By by) throws Exception {
boolean isFoundTheElement = driver.findElements(by).size() > 0;
while (isFoundTheElement == false) {
swipeVertical(0.8, 0.1, 0.5, 2000);
isFoundTheElement = driver.findElements(by).size() > 0;
}

	  return isFoundTheElement;
	}

	public static void swipeVertical (
	  double startPercentage, double finalPercentage, double anchorPercentage, int duration)
	  throws Exception {
	  org.openqa.selenium.Dimension size = driver.manage().window().getSize();
	  int anchor = (int) (size.width * anchorPercentage);
	  int startPoint = (int) (size.height * startPercentage);
	  int endPoint = (int) (size.height * finalPercentage);
	  getTouchAction().press(PointOption.point(anchor, startPoint))
	  .waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
	  .moveTo(PointOption.point(anchor, endPoint)).release().perform();
	}

	public static TouchAction getTouchAction () {
	  return new TouchAction(driver);
	}

It is just some methods. Add it to a class (existing or new class). From any method (main() is just a method, in a class), you instantiate the class, then call the method ()

myClass.scrollToElement (by);

its continuously giving me error on (by) when I call it in the main method saying “by cannot be resolved to a variable”

image

Really?

Import isn’t making a variable.

By by = By.xpath("//someXpath");
scrollToElement(by);