Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Refactor ScreenshotUtils into element / ExtWebDriver methods, rename class to ScreenshotComparator and include only compare methods #87

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/org/finra/jtaf/ewd/ExtWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.finra.jtaf.ewd;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -418,5 +420,16 @@ public List<String> getCurrentWindowIds() {
* Selects the previously selected frame
*/
public void selectLastFrame();

/***
* Take a screenshot of the browser content. Note that while using RemoteWebDriver sessions (ie: with
* Sauce Labs), the screenshot will be a full page of content--not only the visible content where the
* page is scrolled (as when using a non-RemoteWebDriver session).
*
* @param toSaveAs - name of the file to save the picture in (Note: must be PNG)
* @throws IOException
* if problems saving file
*/
public void takeScreenshotOfPage(File toSaveAs) throws IOException;

}
21 changes: 21 additions & 0 deletions src/main/java/org/finra/jtaf/ewd/impl/DefaultExtWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.finra.jtaf.ewd.impl;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -35,18 +37,24 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.io.FileUtils;
import org.ccil.cowan.tagsoup.Parser;
import org.finra.jtaf.ewd.ExtWebDriver;
import org.finra.jtaf.ewd.HighlightProvider;
import org.finra.jtaf.ewd.TimeOutException;
import org.finra.jtaf.ewd.session.SessionManager;
import org.finra.jtaf.ewd.widget.IElement;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
Expand Down Expand Up @@ -884,4 +892,17 @@ public void setSessionId(String id) {
public String getSessionId() {
return this.sessionId;
}

@Override
public void takeScreenshotOfPage(File toSaveAs) throws IOException {
File screenshot;
if(!(wd instanceof RemoteWebDriver)) {
screenshot = ((TakesScreenshot) wd).getScreenshotAs(OutputType.FILE);
}
else {
Augmenter augmenter = new Augmenter();
screenshot = ((TakesScreenshot) augmenter.augment(wd)).getScreenshotAs(OutputType.FILE);
}
FileUtils.copyFile(screenshot, toSaveAs);
}
}
130 changes: 130 additions & 0 deletions src/main/java/org/finra/jtaf/ewd/utils/ScreenshotComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* (C) Copyright 2013 Java Test Automation Framework Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.finra.jtaf.ewd.utils;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.finra.jtaf.ewd.widget.IElement;
import org.finra.jtaf.ewd.widget.WidgetException;

/***
* To compare screenshots of elements
*
* @author Pulitand
*
*/
public class ScreenshotComparator {

private static Logger log = LoggerFactory.getLogger(ScreenshotComparator.class);

/***
* Prereq: The page on which you are taking the screenshot is fully loaded
*
* Take a screenshot of the element identified by element and save the file as toSaveAs
* (Note that this file should be saved as a png).
* Test that the control picture, controlPicture, is both the same size as,
* and, has a similarity value greater than or equal to the threshold.
*
* @param element - the element to be tested
* @param controlPicture - the file of the picture that will serve as the control
* @param toSaveTestPictureAs - for example, save the file at "testData/textFieldWidget/screenshot.png"
* @param threshold - you are asserting that the similarity between the two pictures
* is a double greater than or equal to this double (between 0.0 and 1.0)
* @throws Exception
*/
public static boolean isElementSimilarToScreenshot(IElement element, File controlPicture, File
toSaveTestPictureAs, double threshold) throws IOException, WidgetException {

element.captureElementScreenshot(toSaveTestPictureAs);

log.info("Screenshot was successful. Comparing against control...");

BufferedImage var = ImageIO.read(toSaveTestPictureAs);
BufferedImage cont = ImageIO.read(controlPicture);

return isSimilar(var, cont, threshold);
}

/***
* Prereq: The page on which you are taking the screenshot is fully loaded
*
* Take a screenshot of the element identified by element and save the file as toSaveAs
* (Note that this file should be saved as a png).
* Test that the control picture, controlPicture, is both the same size as,
* and, has a similarity value greater than or equal to the default threshold of .85.
*
* @param element - the element to be tested
* @param controlPicture - the file of the picture that will serve as the control
* @param toSaveTestPictureAs - for example, save the file at "testData/textFieldWidget/screenshot.png"
* @throws WidgetException
* @throws IOException
* @throws Exception
*/
public static boolean isElementSimilarToScreenshot(IElement element, File controlPicture,
File toSaveTestPictureAs) throws IOException, WidgetException {
return isElementSimilarToScreenshot(element, controlPicture, toSaveTestPictureAs, .85);
}

private static boolean isSimilar(BufferedImage var, BufferedImage cont, double threshold) {
return similarity(var, cont) >= threshold;
}

//Returns a double between 0 and 1.0
private static double similarity(BufferedImage var, BufferedImage cont) {

double[] varArr = new double[var.getWidth()*var.getHeight()*3];
double[] contArr = new double[cont.getWidth()*cont.getHeight()*3];

if (varArr.length!=contArr.length)
throw new IllegalStateException("The pictures are different sizes!");

//unroll pixels
for(int i = 0; i < var.getHeight(); i++) {
for(int j = 0; j < var.getWidth(); j++) {
varArr[i*var.getWidth() + j + 0] = new Color(var.getRGB(j, i)).getRed();
contArr[i*cont.getWidth() + j + 0] = new Color(cont.getRGB(j, i)).getRed();
varArr[i*var.getWidth() + j + 1] = new Color(var.getRGB(j, i)).getGreen();
contArr[i*cont.getWidth() + j + 1] = new Color(cont.getRGB(j, i)).getGreen();
varArr[i*var.getWidth() + j + 2] = new Color(var.getRGB(j, i)).getBlue();
contArr[i*cont.getWidth() + j + 2] = new Color(cont.getRGB(j, i)).getBlue();
}
}

double mins=0;
double maxs=0;
for(int i=0;i!=varArr.length;i++){
if (varArr[i]>contArr[i]){
mins+=contArr[i];
maxs+=varArr[i];
} else {
mins+=varArr[i];
maxs+=contArr[i];
}
}
return mins/maxs;
}


}
Loading