Friday, September 13, 2019

Post#38.Capturing Screenshot in Selenium Part-4 (Compare two Screenshots)


Compare two Screenshots

Sometimes we may have situations where we have to compare the two screenshots. We can compare the screenshot using methods present in the selenium webdriver.

We can do it in a very simple way. Refer the below program to achieve the same.

package pack4;
import java.io.File;
import java.io.IOException;
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.firefox.FirefoxDriver;

public class CompareScreenshots {

              public static void main(String[] args) throws IOException {
                             // set the geckodriver.exe property
                             System.setProperty("webdriver.gecko.driver", "C:/PATH/geckodriver.exe");
                             WebDriver driver = new FirefoxDriver();
                             driver.get("http://www.google.com/");
                             File scrFile1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                            
                             driver.navigate().refresh();
                            
                             File scrFile2 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                             if(FileUtils.contentEquals(scrFile1, scrFile2))
                             {
                                           System.out.println("Images are same");
                             }
                             else
                             {
                                           System.out.println("Images are different");
                             }
                             driver.quit();

              }

}

No comments:

Post a Comment