Search This Blog

Showing posts with label webdriver. Show all posts
Showing posts with label webdriver. Show all posts

Friday, 23 October 2015

Capture full screen screenshot using Selenium + Java

It is always a good idea to have a screenshot for investigating what was the state of the application at the time of exception/halt of execution.

Although Selenium WebDriver provides default functions to take the screenshots of the browser at the exception. See example here

The alternate is to capture the full screenshot (Screenshot covering the full desktop) at the time of exception or error occurred on the screen.

Note: When the test execution is failing because of "Model Pop Up" on the browser, default function of the WebDriver will not be able to capture the Model Pop Up in the screenshot.

Git Hub Link



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class FullScreenShot {
    final static String fileLocation = "D:\\ScreenShot\\";


    public static void main(String[] args) {
        try {
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.testautomation4all.com");
            driver.findElement(By.id("some-non-existing-element")).click();
        } catch (Exception e) {
            captureEnirePageScreenshot("Unexpected error");
        }

    }

    public static void captureEnirePageScreenshot(String screenShotName) {
        try {
            // Capture the dimensions of the screen
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

            // Convert the dimensions into rectangle object type
            Rectangle rec = new Rectangle(d);

            // Take the screenshot using the Robot instance
            Robot robot = new Robot();

            BufferedImage myImage = robot.createScreenCapture(rec);

            File img = new File(fileLocation + screenShotName + ".png");

            ImageIO.write(myImage, "png", img);

            System.out.println("Done");

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

How to Print Google Search result in the Eclipse Console?

Example to extract the text from google search result

To extract the search result count (which is displayed on the top) from the google search result, we have to

  1.  Find the WebElement of the search result and extract the WebElement
  2. The resulted string (result) is having extra characters like "About 5050 results"
  3. Extract the sub string from the obtained result string
  4. Replace all the extra characters from the result string
  5. Now parse the String to number format to convert the string to number.
GitHub Url

The program for the above example is as below



import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class GoogleSearchResult {
    public static void main(String[] args) throws InterruptedException {
        Selenium selenium = new DefaultSelenium("localhost", 4444, "firefox", "http://www.google.com/");
        selenium.start();
        selenium.open("/?gws_rd=ssl");
        selenium.type("//input[@id='lst-ib']", "test automation 4 all");
        selenium.click("name=btnG");
//        selenium.waitForPageToLoad("30000");
        Thread.sleep(2000);
        String result = selenium.getText("id=resultStats");
        System.out.println("The Raw format" + result);

        result = result.substring(7, result.indexOf("result"));
        System.out.println("After extracting the substring of the result : " + result);

        result = result.replaceAll(",", "").trim();
        System.out.println("Removing the extra characters from the remaining string " + result);

        System.out.println("Now the remaining string is in the String format. Converting them to the Integer format");
        int searchResultCount = Integer.parseInt(result);

        System.out.println("Total Number of search results : " + searchResultCount);

        selenium.stop();
    }

}