There is time when you need to perform testing web app or site automatically on different screen resolutions. Today there are many online solutions that provide easy way of testing you site on different resolutions. It’s simple done using iframe with custom dimensions. But what if you need to test your site on couple of hundred different resolutions 🙂 ?
There is no better way to do that than using a automated test. Framework of choice for that is Selenium . It have numerous combination of programming language for creating tests with Selenium and web drivers.
Web drivers are responsible for communicating with certain browser. In this example I am using Java (NetBeans IDE) language and FireFox driver.
Step 1
First step is to create test Java project and import selenium_server jar to it.
Step 2
Create TestCase1 class with this content :
private WebDriver driver;
private String url;
Those fields are for storing “browser” object and url.
Preparing environment :
@Before
public void setUp() {
// test url
this.url = "http://www.google.com/";
// ChromeDriver
//System.setProperty("webdriver.chrome.driver", PATH_TO_CHROMEDRIVER);
//driver = new ChromeDriver();
driver = new FirefoxDriver();
driver.get(this.url);
}
We are using FireFox for testing, you can use other driver i.e. chrome. Just uncomment that part and add path to google driver exe.
Finally there is test method :
@Test
public void testResolution() {
driver.navigate().to(this.url);
String[] resulutions = {
"1366x768",
"1920x1080"
// add resolution
};
for (String resulution : resulutions) {
String[] parts = resulution.split("x");
// Screen resolution
Dimension screenRes = new Dimension(Integer.parseInt(parts[0]),Integer.parseInt(parts[1]));
// Set browser resolution
driver.manage().window().setSize(screenRes);
// little pause
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
//Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
}
driver.navigate().refresh();
// little pause
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
//Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
}
this.takeScreenShot(resulution);
}
}
And utilty method for taking screen shot of browser.
private void takeScreenShot(String fileName) {
File screenShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenShot, new File(fileName + ".png"));
} catch (IOException ioe) {
throw new RuntimeException(ioe.getMessage(), ioe);
}
}
Download class source : TestCase1.java
Step 3
After that just fire up test: right click on TestCase1.java and go to Test File or (Ctrl + F6). Result screen shots of site will be inside of project folder root. Named as <resolution>.png .
Notice that browser header dimension is different when browser is maximized and in custom windows dimension.
9 replies on “Testing web app or site automatically on different screen resolutions”
Thanks for your unique and very timely post.After reading this post I learn many new things that’s why I come again again on this blog. So thanks once again behalf of web application testing
It’s always so sweet and also full of a lot of fun for me personally and my office colleagues to search your blog a minimum of thrice in a week to see the new guidance you have got.
Thanks for providing the information . The articles in your blog helped me a lot for improving the knowledge on the subject. Also check my small collection on this at selenium Online Training blog
This is definitely a very helpful post and these line of codes can be very helpful in checking the screen resolutions because i have been working as Automation Engineer at HostNoc which provides web hosting services and dedicated server.
That is certainly a great way to check the resolution of your website or your app. Recently i have also been developing Task management software and had it has been working fine for both platforms.
That is certainly a fact when you have created an hybrid app because sometimes hybrid apps doesn’t work that good in some platforms, that’s why i specifically create an app for android and recently i created many running apps for android for people who do workout.
I like your blog, I read this blog please update more content on hacking, Nice post tableau online training
Certainly i have done many testing on my website but it is better to as a professional to do it. When i asked a penetration tester to test my blog Techvizer, he showed me many loop holes that i had to fix.
The web application should deliver the page effectively altogether goals the user uses.