How to get the DUT screen-size during test?

hi,

I’m using appium for android and ios automation test. Sometimes i need click the relative point on the screen for the situation no elements can be located. So I need know the size of the device under test to adapt different devices.

i searched the methods in the class AppiumDriver but no related method found,

Hi sunflower,

I use below code to get screen size and perform swipe action.

private void getScreenSize() {
this.height = driver.manage().window().getSize().getHeight();
this.width = driver.manage().window().getSize().getWidth();
}

public void swipetobottom() {
getScreenSize();
driver.swipe(width / 4, (height * 80 / 100), width / 4,
			(height * 20 / 100), 2000);

try {
	Thread.sleep(2000);
} catch (InterruptedException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

}

Hi Ram,

please tell how to swipe UP(to top). I mean how to change the dimensions?

Thanks.

Please note, we have set swipeduration variable to 3000; [in milliseconds]

 public void swipetotop() {
	getScreenSize();
	if (height > 1500) {
		driver.swipe(width / 4, (height * 20 / 100), width / 4,
				(height * 80 / 100), swipeDuration);
	} else if (height > 1000) {
		driver.swipe(width / 4, (height * 15 / 100), width / 4,
				(height * 85 / 100), swipeDuration);
	} else {
		driver.swipe(width / 4, (height * 10 / 100), width / 4,
				(height * 90 / 100), swipeDuration);
	}
	try {
		Thread.sleep(2000);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}

hi, Ram

Thanks for your quick reply, your answer really help me. But one more confusion, is that set swipeDuration to 3000 is a suggestion or is it necessary? Cause i tried to set it to 500 and it work well on sumsung.

Hi ram,

Thanks for the needful help. Its working to me. But it swipes till half of the screen. Not till the bottom of the screen. Somehow, I am not clear about how to set the coordiantes to swipe and down.

Regards,
Uma

Hi sunflower,
Its just a safety net :smile:

This works better when u have to do multiple swipes [say swipe down for 2 or 3 times]

Even 2000 ms worked fine for us, till we hit a slow device.

Hi Uma,
Ideally each swipe should cover close to 60-70% area, as we set 20% [or 15%] on each side to cover for action bar and any footer space.

If u want to swipe more, you may want to call it multiple times or change 20 [and 80] with appropriate percentage value. [Ex: 10 , 90]

Hi Ram,

Awesome. That worked for me. I gave a loop so that it will iterate and reach the bottom of the screen. Thanks.

I have one more question. Can we use the same for left and right swipe ? Thanks for the reply.

Yes, u can use with minor modification as shown below -

 driver.swipe(100, height / 2, width - 100, height / 2, swipeDuration); 

[100 can be set to percentage value]