I tested on Amazon shopping app. I have an issue after swipe to find an element. Because the list view is too long. I need to scroll (using the swipe API). But after I swipe, I cannot click the element anymore. It returned no error but the app have no responses.
The function ScrollToElement() is to swipe to search element. I tried to use touch action tap but no hope. I tried to tap an exact(372,466) location and it works but it is not as expected. Thanks in advance.
Code:
public class FirstTest {
private AppiumDriver driver;
private Dimension size;
@BeforeClass
public void Setup() throws MalformedURLException {
String appActivityText = "com.amazon.mShop.home.HomeActivity";
String appPackageText = "in.amazon.mShop.android.shopping";
String fileLocation = "/system/app/";
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "\\STC");
File app = new File(appDir, fileLocation);
System.out.println(app);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
capabilities.setCapability(CapabilityType.PLATFORM, "Android");
capabilities.setCapability(CapabilityType.VERSION, "4.4.2");
capabilities.setCapability("deviceName", "420373d0de528100");//420373d0de528100 01a61316598f30e6
capabilities.setCapability("newCommandTimeout", "100");
// capabilities.setCapability("app", "Chrome"/*app.getAbsolutePath()*/);
capabilities.setCapability("appPackage", appPackageText);
capabilities.setCapability("appActivity", appActivityText);
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
}
@Test
public void Login() throws Exception {
// Click on Shop by Deparment link
driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
System.out.println("Click on Shop by Deparment link");
driver.findElement(By.id("web_home_shop_by_department_label")).click();
Thread.sleep(1000);
WebElement element = driver.findElement(By.name("Office Products Link"));
ScrollToElement(driver, element);
Thread.sleep(3000);
// TouchAction action = new TouchAction(driver);
// action.tap(372,466).perform();
driver.findElement(By.name("Office Products Link")).click();
}
@AfterClass
public void closeApp() {
// driver.closeApp();
}
public static void ScrollToElement(AppiumDriver driver, WebElement element){
size = driver.manage().window().getSize();
// Find swipe start and end point from screen's with and height.
// Find starty point which is at bottom side of screen.
int start = (int) (size.height * 0.20);
int starty = (int) (size.height * 0.80);
// Find endy point which is at top side of screen.
int endy = (int) (size.height * 0.20);
// Find horizontal point where you wants to swipe. It is in middle of
// screen width.
int startx = size.width / 2;
while (true) {
driver.swipe(startx, starty, startx, endy, 3000);
start = start + (starty - endy);
if (element.getLocation().getY() - start < size.height - endy) {
break;
}
}
}
}