Search in sources :

Example 1 with ScreenShootInfo

use of com.qaprosoft.carina.core.foundation.utils.metadata.model.ScreenShootInfo in project carina by qaprosoft.

the class ExtendedWebElement method captureElements.

private void captureElements() {
    if (!Configuration.getBoolean(Parameter.SMART_SCREENSHOT)) {
        return;
    }
    if (!BrowserType.CHROME.equalsIgnoreCase(Configuration.get(Parameter.BROWSER))) {
        return;
    }
    String currentUrl;
    if (!Configuration.get(Parameter.BROWSER).isEmpty()) {
        currentUrl = driver.getCurrentUrl();
    } else {
        // change for XBox and looks like mobile part
        currentUrl = driver.getTitle();
    }
    String cache = getUrlWithoutParameters(currentUrl);
    if (!MetadataCollector.getAllCollectedData().containsKey(cache)) {
        try {
            ElementsInfo elementsInfo = new ElementsInfo();
            elementsInfo.setCurrentURL(currentUrl);
            String metadataScreenPath = Screenshot.captureMetadata(getDriver(), String.valueOf(cache.hashCode()));
            // TODO: double check that file exist because due to the
            // different reason screenshot can miss
            File newPlace = new File(metadataScreenPath);
            ScreenShootInfo screenShootInfo = new ScreenShootInfo();
            screenShootInfo.setScreenshotPath(newPlace.getAbsolutePath());
            BufferedImage bimg = ImageIO.read(newPlace);
            screenShootInfo.setWidth(bimg.getWidth());
            screenShootInfo.setHeight(bimg.getHeight());
            elementsInfo.setScreenshot(screenShootInfo);
            List<WebElement> all = driver.findElements(By.xpath("//*"));
            List<WebElement> control = driver.findElements(By.xpath("//input[not(contains(@type,'hidden'))] | //button | .//*[contains(@class, 'btn') and not(self::span)] | //select"));
            for (WebElement webElement : control) {
                ElementInfo elementInfo = getElementInfo(new ExtendedWebElement(webElement, driver));
                int elementPosition = all.indexOf(webElement);
                for (int i = 1; i < 5; i++) {
                    if (elementPosition - i < 0) {
                        break;
                    }
                    if (control.indexOf(all.get(elementPosition - i)) > 0) {
                        break;
                    }
                    if (!all.get(elementPosition - i).isDisplayed()) {
                        continue;
                    }
                    String sti = all.get(elementPosition - i).getText();
                    if (sti == null || sti.isEmpty() || control.get(0).getText().equals(sti)) {
                        continue;
                    } else {
                        elementInfo.setTextInfo(getElementInfo(new ExtendedWebElement(all.get(elementPosition - i), driver)));
                        break;
                    }
                }
                elementsInfo.addElement(elementInfo);
                MetadataCollector.putPageInfo(cache, elementsInfo);
            }
        } catch (IOException e) {
            LOGGER.error("Unable to capture elements metadata!", e);
        } catch (Exception e) {
            LOGGER.error("Unable to capture elements metadata!", e);
        } catch (Throwable thr) {
            LOGGER.error("Unable to capture elements metadata!", thr);
        }
    }
}
Also used : ElementsInfo(com.qaprosoft.carina.core.foundation.utils.metadata.model.ElementsInfo) ElementInfo(com.qaprosoft.carina.core.foundation.utils.metadata.model.ElementInfo) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ScreenShootInfo(com.qaprosoft.carina.core.foundation.utils.metadata.model.ScreenShootInfo) File(java.io.File)

Aggregations

ElementInfo (com.qaprosoft.carina.core.foundation.utils.metadata.model.ElementInfo)1 ElementsInfo (com.qaprosoft.carina.core.foundation.utils.metadata.model.ElementsInfo)1 ScreenShootInfo (com.qaprosoft.carina.core.foundation.utils.metadata.model.ScreenShootInfo)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1