Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignoreElements not working #295

Open
mayurbidkar opened this issue Apr 29, 2022 · 0 comments
Open

ignoreElements not working #295

mayurbidkar opened this issue Apr 29, 2022 · 0 comments

Comments

@mayurbidkar
Copy link

Please check below snippet of code:

public void verifyMapLayout() throws IOException {
String ignoreRefresh = "time[title]";
String alertCritical = "div[class*='bg-critical']";
String alertError = "div[class*='bg-error']";
String alertWarning = "div[class*='bg-warning']";
String alertSdt = "div[class*='bg-sdt']";
String imgSrc = "img[id='companyLogo']";
String canvasXpath = "//div[contains(@Class,'MapContainer__TopologyMapWrapper')]";
String canvasCss = "div.yfiles-svgpanel";
String mapGroupXpath = "//span[@Class='group-name' and text()='mapTest']";
String mapLinkXpath = "//span[@Class='group-name' and text()='mapTest']/../../../../div[@Class='group-container']//tbody/tr[@data-name='MapHierachical']//a";
uiCommonLib.waitForElementVisible(driver.findElement(By.xpath(mapGroupXpath)));
uiCommonLib.click(driver.findElement(By.xpath(mapGroupXpath)));
uiCommonLib.waitForElementVisible(driver.findElement(By.xpath(mapLinkXpath)));
uiCommonLib.click(driver.findElement(By.xpath(mapLinkXpath)));
uiCommonLib.waitForElementVisible(driver.findElement(By.xpath(canvasXpath)));
WebElement mapElement = driver.findElement(By.xpath(canvasXpath));
Dimension d = mapElement.getSize();
logger.info("height:"+ d.height+" width: "+d.width);

    Set<By> ignoredElements = new HashSet<>();
    WebElement elementRefresh = driver.findElement(By.cssSelector(ignoreRefresh));
    Point p = elementRefresh.getLocation();
    ignoredElements.add(By.cssSelector(ignoreRefresh));
    ignoredElements.add(By.cssSelector(alertWarning));
    ignoredElements.add(By.cssSelector(alertSdt));
    ignoredElements.add(By.cssSelector(alertCritical));
    ignoredElements.add(By.cssSelector(alertError));
    ignoredElements.add(By.cssSelector(imgSrc));
    Set<Coords> ignoredCords = getCords(ignoredElements, driver);
    String layout = "Hierarchical (top down)";
    Screenshot actualScreenShot = takeScreenAshot(driver,"Hierarchical_top_down", ignoredElements);

    BufferedImage expectedImage = ImageIO.read(new File(getUsersProjectRootDirectory(osName)+"/ExpectedImages/"+layout+".png"));

    // Create ImageDiffer object and call method makeDiff()
    Screenshot expectedScreenShot = new Screenshot(expectedImage);
    expectedScreenShot.setIgnoredAreas(actualScreenShot.getIgnoredAreas());
    expectedScreenShot.setCoordsToCompare(actualScreenShot.getCoordsToCompare());
    ImageDiffer imgDiff = new ImageDiffer();
    ImageDiff diff = imgDiff.makeDiff(actualScreenShot.getImage(), expectedScreenShot.getImage());

    if (diff.hasDiff()) {
        makeDiffImage(actualScreenShot.getImage(), expectedScreenShot.getImage(), "Hierarchical (top down)", diff);
        softAssert.assertFalse(diff.hasDiff(), "images are not same");
    } else {
        logger.info("images are same");
    }

    softAssert.assertAll();

}

// i have used it for to get cords but i am not using it as of now
public static Set getCords(Set ignoredElements, WebDriver driver) {
Set ignoredCoords = new HashSet<>();
for (By locator : ignoredElements) {
Point point = driver.findElement(locator).getLocation();
Dimension dimension = driver.findElement(locator).getSize();
ignoredCoords.add(new Coords(point.getX(), point.getY(), dimension.getWidth(), dimension.getHeight()));
}
return ignoredCoords;
}

public Screenshot takeScreenAshot(WebDriver driver, String layout, Set ignoredElements) {
String destination = null;
ImageIO.setUseCache(false);
AShot shooter = new AShot().shootingStrategy(ShootingStrategies.simple()).
ignoredElements(ignoredElements).coordsProvider(new WebDriverCoordsProvider());
Screenshot screenshot= shooter.takeScreenshot(driver);
if (pathForStoringScreenshots == null) {
pathForStoringScreenshots = getUsersProjectRootDirectory(osName) + "/Screenshots/";
}
try {
destination =
pathForStoringScreenshots + "/" + layout
+ ".png";
ImageIO.write(screenshot.getImage(), "png", new File(destination));
// FileUtils.copyFile(scrFile, new File(destination));
} catch (Exception e) {
logger.error("Unable to save Screenshot due to error :: ", e);
}
return screenshot;
}

public void makeDiffImage(BufferedImage actualImage, BufferedImage expectedImage, String layout, ImageDiff diff) throws IOException {
    String destination = null;
    if (pathForStoringScreenshots == null) {
        pathForStoringScreenshots = getUsersProjectRootDirectory(osName) + "/Screenshots/Difference/";
    }
    try {
        destination =
                pathForStoringScreenshots + "/" + "diff_" + layout
                        + ".png";
        BufferedImage diffImage = diff.getMarkedImage();
        ImageIO.write(diffImage, "png", new File(destination));
    } catch (Exception e) {
        logger.error("Unable to save Screenshot due to error :: ", e);
    }
}

But still i am getting difference image even if i added list of ignored elements with css values.
Tried every way to achieve comparison but Ashot not doing it in correct way.
Please let me know thing i am missing. need help for this.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant