Categories
Programming

Testing web app or site automatically on different screen resolutions

Testing web app or site automatically on different screen resolutions with tests. There are a bunch of web site for that, but if you wont to do that programmatically here is example how to test your website under different screen resolutions. Read post about creating Selenium tests for taking screen images of your site under different resolutions and storing it in local folder. You can see how your site look in different browsers like Chrome, FIrefox…

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.

testStep 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”

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.

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 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *