Unable to Scroll horizontal Left to right uiautomator2


#1

I have tried using this code .
Android 7.0
Appium 1.7.2

To scroll horizontal left to right:

@AndroidFindBy(xpath="//android.widget.TextView[@text='Chicken Fajita Taco']")
public MobileElement element1;	

@AndroidFindBy(xpath="//android.widget.TextView[@text='Grilled Chicken Melt']")
public MobileElement element2;	

public void scroll(AndroidDriver<MobileElement> driver)
{
         TouchAction action = new TouchAction(driver);

         int startY = element1.getLocation().getY() + (element1.getSize().getHeight() / 2);
         int startX = element1.getLocation().getX() + (element1.getSize().getWidth() / 2);
         int endX = element2.getLocation().getX() + (element2.getSize().getWidth() / 2);
         int endY = element2.getLocation().getY() + (element2.getSize().getHeight() / 2);
         action.press(startX, startY).waitAction(2000).moveTo(endX, endY).release().perform();         
}

It gives error:
Can’t locate an element by this strategy: Locator map:

  • native content: “By.xpath: //android.widget.TextView[@text=‘Grilled Chicken Melt’]”
  • html content: "by id or name “element2"”
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: ‘2.53.0’, revision: ‘35ae25b1534ae328c771e0856c93e187490ca824’, time: ‘2016-03-15 10:43:46’
    System info: host: ‘Sheebi-PC’, ip: ‘192.168.1.3’, os.name: ‘Windows 7’, os.arch: ‘x86’, os.version: ‘6.1’, java.version: ‘1.8.0_131’
    Driver info: driver.version: unknown

Here Element2 is not identified ,how to specify correctly


#2

I solved Horizontal scroll by this approach:

public void HorizontalSwipe (AndroidDriver driver,int times,double percentage)
{
Dimension size =driver.manage().window().getsize();
int height = size.getHeight();
int width= size.getWidth();
int y=(int)(height * percentage);
int startx=(int)(width * 0.75);
int endx=(int)(width * 0.20);

for(int i=0;i<times;i++)
{
driver.swipe(startx,y,endx,y,1000);
}

Usage:
HorizontalSwipe(driver,3,0.40);
3 -No of times to be scrolled
Here 0.40 means 40% of screen Height(Y axis)
Y axis can be calculated based on 0 to 100 percentage scale ,where Y axis must be changed depend on height.
For Example:
Attached screenshot we have Category Chicken which comes under 40% of screen height.