Thursday 20 November 2014

Automation beyond testing


I was asked to take screenshots of every page of few web applications and put it in a demo site's root folder.

This is not the first time, that i'm doing this kind of work, which i feel too lazy to do it. But then this time learning a programming language, a tool and the 'Lazy' part helped me doing this in lesser time.

Wrote a piece of code in a matter of minutes, and by just browsing through the pages did its task.. this saved me a lot of time.

So creating an executable, and putting it in our space, will help any one who needs to take screen shots :)

I'm not saying this is a great work or a life saver, but it helped and that matters!!

What i learned:

  • Identifying the right scenario and automating it will save you lot of time, so you have more time to test the ambiguous!!
  • Be prepared, you need to have enough weapons in your armoury, it will save you lot of blood 


Code snippet:

You simply browse the pages of your website and the screen shot will be getting saved to your system!!

What you need:

  • Java
  • Jar files for Selenium WebDriver
  • And a basic understanding of coding
  • Any tool to run this code (say Eclipse IDE)

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Screenshots {

static WebDriver driver;
static String url = "http://www.google.co.in/"; // Specify Your website URL
static String folderpath = "d:\\Screenshots"; // Where you want to save it?
static int FilenameAndCount = 1;
static int screenshotInterval = 3; // In Seconds
static int maxScreenshot = 100; // How many screen shots you need!!

public static void setup() {

driver = new FirefoxDriver(); // I'm using Firefox browser 
driver.get(url); // Open the website
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().window().maximize();

}

public static void takescrshot() throws IOException, InterruptedException {

try {
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(folderpath + "\\"
+ FilenameAndCount + ".png"));

FilenameAndCount++;
} catch (Exception e) {
System.err.println("e");

}
}

public static void main(String[] args) throws IOException,
InterruptedException {

setup();
try {
while (FilenameAndCount <= maxScreenshot) {
takescrshot(); // Call Screenshot method
Thread.sleep(screenshotInterval * 1000);

}
} catch (Exception e) {
System.err.print(e);
}
System.exit(1);
driver.quit();

}
}

                                                        ***************