How to find acquire test device's screen dimensions?

7,764 views
Skip to first unread message

Andrew Dinh

unread,
Nov 27, 2013, 5:06:35 PM11/27/13
to appium-...@googlegroups.com
Hi all,
 
I am writing Appium tests using Java.  I was wondering how we can acquire the test device screen's width and height?  Thanks.
 
 

Dan Cuellar

unread,
Nov 27, 2013, 5:48:12 PM11/27/13
to appium-...@googlegroups.com
Take a screenshot and measure.

Andrew Dinh

unread,
Nov 27, 2013, 6:51:56 PM11/27/13
to appium-...@googlegroups.com
 Hi Dan,
 
Why can't we acquire the width and height of the screen at run time to use as a variable?  Thanks.  

bootstrap online

unread,
Nov 27, 2013, 7:10:41 PM11/27/13
to Andrew Dinh, appium-...@googlegroups.com
You can. I use @driver.manage.window.size in the Ruby gem.
> --
> http://appium.io
> ---
> You received this message because you are subscribed to the Google Groups
> "Appium-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to appium-discus...@googlegroups.com.
> Visit this group at http://groups.google.com/group/appium-discuss.
> For more options, visit https://groups.google.com/groups/opt_out.

Anil Kumar

unread,
Nov 29, 2013, 2:03:31 AM11/29/13
to appium-...@googlegroups.com
If you mean on iOS, you can also run a javascript "target.rect();"

Andrew Dinh

unread,
Dec 1, 2013, 5:10:11 AM12/1/13
to appium-...@googlegroups.com
Hi Anil,
 I am just testing on Android at the moment.  Thanks for your help though! 
 
 

Andrew Dinh

unread,
Dec 1, 2013, 5:11:52 AM12/1/13
to appium-...@googlegroups.com, Andrew Dinh
Hi bootstrap,
 
Thanks for your help!  I will try to see if I can do similar things with Java.  Thanks!

Andrew Dinh

unread,
Dec 2, 2013, 6:29:06 PM12/2/13
to appium-...@googlegroups.com, Andrew Dinh
Reporting back on my experiment:
 
I was able to get driver.manage().window().getSize() to return something.  However, I don't know what object it retuns.  But I could see that I am getting the screen's dimensions by converting it to string and printing it out.
 
The output looks like a Python tuple, e.g., (768, 1184).
 
Anyway, my background is QA and my level of any programming language is limited to writing scripts and googling syntax.  Java is not an exception.  So, I did a what works for me, though it is probably not the most efficient or elegant codewise. I converted the returned object to String, split it into string array, get the width and height strings and convert them back into integers.  You are probably laughing your behinds off right now, and if you are, please suggest a better way after you are done laughing :-).
 
String[] dimensions = driver.manage().window().getSize().toString().split("\\D");
int screenWidth = Integer.parseInt(dimensions[1]);
int screenHeight = Integer.parseInt(dimensions[3]);
 
And now, I have my screen's dimensions for use in my script. 
 
I might have been able to use the dimensions.getWidth() and dimensions.getHeight() methods and not have to convert it to String if I know the return type of getSize(). 
 
Anyway, this is the way that currently works for me.

Dan Doveralba

unread,
Dec 2, 2013, 6:33:31 PM12/2/13
to Andrew Dinh, appium-...@googlegroups.com
Hey Andrew,

I’m curious, what’s the scenario that’s requiring you to need screen dimensions?

Thanks,
Dan

Andrew Dinh

unread,
Dec 2, 2013, 6:53:09 PM12/2/13
to appium-...@googlegroups.com, Andrew Dinh
I can think of at least one Use Case.  It really is a little edge case.
 
Let's say I am testing Android Gallery.  When I am in Grid View, I can see elements on the actionbar as uiautomator sees them.  However, I am unable to see the photo thumbnails.  Now, if I know the screen dimensions, I might be able to approximate and tap on a particular thumbnail, e.g., the 3rd of 6 thumbnails, if I understand the behavior of my test app reasonably well. 
 
But then, again, this doesn't seem to be terribly important right now as I think about your question.
 
I will update this post later if I can think of a more legitimate Use Case :).
 
 

Andrew Dinh

unread,
Dec 2, 2013, 6:57:36 PM12/2/13
to appium-...@googlegroups.com, Andrew Dinh
On a somewhat related note, right now you can only long-tap on an object in Appium.  You cannot long-tap on a coordinate.
 
If I want to long-tap on an Android Gallery thumbnail, I am out of luck because Uiautomator does not see the thumbnails.
 
Just something I noticed while learning to use Appium. 

bootstrap online

unread,
Dec 2, 2013, 7:13:53 PM12/2/13
to Andrew Dinh, appium-...@googlegroups.com
There are examples of how to use this API properly on GitHub.
https://github.com/search?q=driver.manage%28%29.window%28%29.getSize%28%29&ref=cmdform&type=Code

It's org.openqa.selenium.Dimension so you can use .getWidth() and .getHeight().
http://selenium.googlecode.com/git/docs/api/java/index.html

Andrew Dinh

unread,
Dec 2, 2013, 7:36:30 PM12/2/13
to appium-...@googlegroups.com, Andrew Dinh
Hi bootstrap,
 
Yes, that's the info I need.  Thanks.
 
FYI, I changed my script to:
 
        Dimension dimensions = driver.manage().window().getSize();
        int screenWidth = dimensions.getWidth();
        int screenHeight = dimensions.getHeight();
 
tada.... Thanks, again!

bootstrap online

unread,
Dec 2, 2013, 7:46:27 PM12/2/13
to Andrew Dinh, appium-...@googlegroups.com
Great!

Sean Wang

unread,
May 25, 2014, 11:38:03 PM5/25/14
to appium-...@googlegroups.com, Andrew Dinh
I got here from search, but no Python version. I got the result after some search:
driver.get_window_size()

hope it helps

在 2013年12月3日星期二UTC+8上午8时46分27秒,bootstraponline写道:
Reply all
Reply to author
Forward
0 new messages