Search in sources :

Example 1 with ElementInfo

use of com.qaprosoft.carina.core.foundation.utils.metadata.model.ElementInfo 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)

Example 2 with ElementInfo

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

the class ExtendedWebElement method getElementInfo.

@SuppressWarnings("unchecked")
private ElementInfo getElementInfo(ExtendedWebElement extendedWebElement) {
    ElementInfo elementInfo = new ElementInfo();
    if (extendedWebElement.isElementPresent(1)) {
        Point location = extendedWebElement.getElement().getLocation();
        Dimension size = extendedWebElement.getElement().getSize();
        elementInfo.setRect(new Rect(location.getX(), location.getY(), size.getWidth(), size.getHeight()));
        elementInfo.setElementsAttributes((Map<String, String>) ((RemoteWebDriver) driver).executeScript(ATTRIBUTE_JS, extendedWebElement.getElement()));
        try {
            elementInfo.setText(extendedWebElement.getText());
        } catch (Exception e) {
            elementInfo.setText("");
        }
        return elementInfo;
    } else {
        return null;
    }
}
Also used : Rect(com.qaprosoft.carina.core.foundation.utils.metadata.model.Rect) ElementInfo(com.qaprosoft.carina.core.foundation.utils.metadata.model.ElementInfo) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

ElementInfo (com.qaprosoft.carina.core.foundation.utils.metadata.model.ElementInfo)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 ElementsInfo (com.qaprosoft.carina.core.foundation.utils.metadata.model.ElementsInfo)1 Rect (com.qaprosoft.carina.core.foundation.utils.metadata.model.Rect)1 ScreenShootInfo (com.qaprosoft.carina.core.foundation.utils.metadata.model.ScreenShootInfo)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)1