Appium scroll and click in 1.9.1

How Can I do auto click on a web element in 1.9.1.
In 1.7.2 UiAutomatir does this for me, in which auto scroll was there and then it click on right element.
I was expecting the same thing in 1.9.1, but Failed to do this job.
Can anyone help me here to click the element if the element is not present on the screen but present in either horizontal scroll or vertical scroll?

You need to implement scroll by touch action. Please refer to this:
http://appium.io/docs/en/writing-running-appium/touch-actions/
http://appium.io/docs/en/commands/interactions/touch/touch-perform/

Try this something like this, you can also scrollIntoView to a resourceID

@AndroidFindBy(uiAutomator = “new UiScrollable(new UiSelector()).scrollIntoView(text(“yourText”))”)
public MobileElement yourElementName;

Or something like this, for me works perfectly:

    public void swipeUpUntilTextExists(String expected, int times) {
    		int i = 0;
    		do {
    			swipeUp();
    			i++;
    			if (i == times)
    				break;
    		} while (!driver.getPageSource().contains(expected));
    	}

    public void swipeUp() {
    		Dimension size = driver.manage().window().getSize();
    		int starty = (int) (size.height * 0.8);
    		int endy = (int) (size.height * 0.2);
    		int startx = (int) (size.width / 2.2);
    		try {
    			log.info("Trying to swipe up from x:" + startx + " y:" + starty + ", to x:" + startx + " y:" + endy);
    			new TouchAction(driver).press(point(startx, starty)).waitAction(waitOptions(ofSeconds(3)))
    					.moveTo(point(startx, endy)).release().perform();
    		} catch (Exception e) {
    			log.info("Swipe did not complete succesfully.");
    		}
    	}

You can calculate and change the starting, ending points in swipeUp method in such way to swipe left, right or down.

Thank you so much @MrZigaS.

But this creating an issue like when I am looking for an element which is in left direction, but the scroll always seek for element in right direction.

How it will find that the scrolling would have done in left direction?

RemoteWebElement element = (RemoteWebElement)mAppiumDriver. findElement(By.name(accessiblityID));
	String elementID = element.getId();
	HashMap<String, String> scrollObject = new HashMap<String, String>();
	scrollObject.put("element", elementID);
	scrollObject.put("toVisible", "not an empty string");
	mAppiumDriver.executeScript("mobile:scroll", scrollObject);

I suggest you implement it like this:

new TouchAction(driver).press(PointOption.point(x, scrollStart)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(1300))).moveTo(PointOption.point(x, scrollEnd)).release().perform();

Hey, It will very helpful if you can lightening on the above statement.
How will I calculate the scrollStart & scrollEnd?

You can call:

driver.manage().window().getSize().getHeight()

or

 driver.manage().window().getSize().getWidth()

to get sizes of the screen. You can use these to specify how much to scroll.
E.g.:

int scrollStart =  (int) (driver.manage().window().getSize().getHeight() * 0.7)
int scrollEnd =  (int) (driver.manage().window().getSize().getHeight() * 0.3)

… don’t forget about the X. Then scroll until your element is visible/displayed and press on it.

1 Like

What happen if There are three scroll bar and I need to scroll upto a element which is present in one of them scroll bar?

Sorry, I do not understand your question. Can you post a printscreen of the Appium Desktop?..or be more specific?

In testing app there are two scroll bars. One of them is for menu items and second one is for subOptions.
To access any subitem first I have to click on the menu item then only sub options will be opened.

So clearing with example. Two scroll bar A & B. A contains for menu and B contains submenu options. To click on any item of B first we need to tap on buttons that is parent.
How can I do this dynamically so that any item looking in A or B can be searchable

Ok, so what you need (if I understand) is to scroll inside that scroll bar. To do that use element’s location and size instead of the window’s.

E.g.:

 scrollBar.getSize() // From here you can get Height and Width.
 scrollBar.getLocation() // From here you can get X and Y.

All you need to do now is to calculate the coordinates where to scroll. I hope this helps.

Thank you @MrZigaS

Could you please assist me which is fair way to click on element which is present on screen?
Way1:
IOSTouchAction action = new IOSTouchAction(mAppiumDriver);
action.press(IOSPressOptions.iosPressOptions().withElement(ElementOption.element(ele))).waitAction(WaitOptions.waitOptions(Duration.ofMillis(2000)));
action.release();
action.perform();
Way2:
WebElement ele = findElementUsingAccessibilityID(accessibilityID);
ele.click();

I am asking because I am seeing this two methods are not completely 100% true. Sometimes way-1 works and another time way-2 gives result.

I’d go with the way 2.

Thanks, One more thing need to clear.
How can I change the text of selected lines in Appium 1.9.1?
Previously I was using driver.getKeyboard.sendKeys(text)

You need to do:

element.sendKeys(text);

See if this helps