org.ednovo.gooru.web.util.WebDriverUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.ednovo.gooru.web.util.WebDriverUtil.java

Source

/*******************************************************************************
 * WebDriverUtil.java
 * gooru-screenshot
 * Created by gooru-screenshot on 2014
 * Copyright (c) 2014 Gooru. All rights reserved.
 * http://www.goorulearning.org/
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES O     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE     AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
package org.ednovo.gooru.web.util;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.springframework.stereotype.Component;

/**
 * @author Gooru API Team
 * 
 */

@Component
public class WebDriverUtil {

    private int imageWidth;

    private String errorMsg;

    /**
     * Generates screen shot for the given
     * 
     * @param url
     *            of dimension
     * @param width
     *            x
     * @param height
     * 
     * 
     */

    public byte[] generateScreenshot(String sourceUrl, int width, int height) throws Exception {

        /* Load driver and take screenshot */
        errorMsg = "Error to take screen shot";
        String targetFileName = "";
        WebDriver driver = new FirefoxDriver();
        driver.get(sourceUrl);
        Thread.sleep(2000);
        String fileName = sourceUrl.substring(sourceUrl.lastIndexOf("/") + 1);
        Dimension dimesions = driver.findElement(By.tagName("html")).getSize();
        imageWidth = dimesions.width;
        try {
            String scrFileName = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE).toString();
            targetFileName = fileName + ".png";
            new ImageScaler().scaleImage(scrFileName, width, height, targetFileName, imageWidth);
        } catch (Exception e) {
            return FileUtils.readFileToByteArray(new File(errorMsg));
        }

        driver.quit();
        imageWidth = 0;

        return FileUtils.readFileToByteArray(new File(targetFileName));
    }

}