Search in sources :

Example 41 with CustomEventFiringWebDriver

use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.

the class ScreenshotUtil method capturePage.

/**
 * Capture current page (either web or app page)
 * This is a wrapper around the selenium screenshot capability
 * @return
 */
public BufferedImage capturePage(int cropTop, int cropBottom) {
    if (driver == null) {
        return null;
    }
    try {
        // Don't capture snapshot for htmlunit
        if (uiDriver != null && uiDriver.getConfig().getBrowserType() == BrowserType.HTMLUNIT) {
            return null;
        }
        TakesScreenshot screenShot = (TakesScreenshot) driver;
        // android does not support screenshot from webview context, switch temporarly to native_app context to take screenshot
        if (uiDriver != null && uiDriver.getConfig().getBrowserType() == BrowserType.BROWSER) {
            ((AndroidDriver<WebElement>) ((CustomEventFiringWebDriver) driver).getWebDriver()).context("NATIVE_APP");
        }
        String screenshotB64 = screenShot.getScreenshotAs(OutputType.BASE64);
        if (uiDriver != null && uiDriver.getConfig().getBrowserType() == BrowserType.BROWSER) {
            ((AndroidDriver<WebElement>) ((CustomEventFiringWebDriver) driver).getWebDriver()).context("WEBVIEW");
        }
        BufferedImage capturedImage = ImageProcessor.loadFromB64String(screenshotB64);
        // crop capture by removing headers
        if (cropTop >= 0 && cropBottom >= 0) {
            // in case driver already capture the whole content, do not crop anything as cropping is used to remove static headers when scrolling
            Dimension contentDimension = ((CustomEventFiringWebDriver) driver).getContentDimension();
            if (capturedImage.getWidth() == contentDimension.width && capturedImage.getHeight() == contentDimension.height) {
                return capturedImage;
            }
            Dimension dimensions = ((CustomEventFiringWebDriver) driver).getViewPortDimensionWithoutScrollbar();
            capturedImage = ImageProcessor.cropImage(capturedImage, 0, cropTop, dimensions.getWidth(), dimensions.getHeight() - cropTop - cropBottom);
        }
        return capturedImage;
    } catch (Exception ex) {
        // Ignore all exceptions
        logger.error("capturePageScreenshotToString: ", ex);
    }
    return null;
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) AndroidDriver(io.appium.java_client.android.AndroidDriver) Dimension(org.openqa.selenium.Dimension) BufferedImage(java.awt.image.BufferedImage) WebDriverException(org.openqa.selenium.WebDriverException) ScenarioException(com.seleniumtests.customexception.ScenarioException) IOException(java.io.IOException) JavascriptException(org.openqa.selenium.JavascriptException) TakesScreenshot(org.openqa.selenium.TakesScreenshot)

Example 42 with CustomEventFiringWebDriver

use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.

the class ScreenshotUtil method capture.

/**
 * Capture a picture
 * @param target		which picture to take, screen or page.
 * @param exportClass	The type of export to perform (File, ScreenShot, String, BufferedImage)
 * @param allWindows	if true, will take a screenshot for all windows (only available for browser capture)
 * @param force			force capture even if set to false in SeleniumTestContext. This allows PictureElement and ScreenZone to work
 * @param scrollDelay	time in ms between the scrolling (when it's needed) and effective capture. A higher value means we have chance all picture have been loaded (with progressive loading)
 * 						but capture take more time
 * @return				The image in the requested format
 */
public <T extends Object> List<T> capture(SnapshotTarget target, Class<T> exportClass, boolean allWindows, boolean force, int scrollDelay) {
    if (!force && (SeleniumTestsContextManager.getThreadContext() == null || outputDirectory == null || !SeleniumTestsContextManager.getThreadContext().getCaptureSnapshot())) {
        return new ArrayList<>();
    }
    LocalDateTime start = LocalDateTime.now();
    List<NamedBufferedImage> capturedImages = captureAllImages(target, allWindows, scrollDelay);
    // back to page top
    try {
        if (target.isPageTarget()) {
            ((CustomEventFiringWebDriver) driver).scrollTop();
        }
    } catch (WebDriverException e) {
    // ignore errors here.
    // com.seleniumtests.it.reporter.TestTestLogging.testManualSteps() with HTMLUnit driver
    // org.openqa.selenium.WebDriverException: Can't execute JavaScript before a page has been loaded!
    }
    return exportBufferedImages(exportClass, start, capturedImages);
}
Also used : LocalDateTime(java.time.LocalDateTime) CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) ArrayList(java.util.ArrayList) WebDriverException(org.openqa.selenium.WebDriverException)

Example 43 with CustomEventFiringWebDriver

use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.

the class CompositeActions method updateHandles.

/**
 * Update window handles when a click is requested in a composite Action (to get the same behavior between native clicks
 * and clicks in CompositeAction
 * Capture is done on all Action sub-classes, else it would never be done
 *
 * TO KEEP until ClickAction and other equivalents are there in selenium code
 *
 * @param joinPoint
 * @throws SecurityException
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
@Before("call(public void org.openqa.selenium.interactions.Action+.perform ())")
public void updateHandles(JoinPoint joinPoint) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    if (!(joinPoint.getTarget() instanceof CompositeAction)) {
        return;
    }
    CompositeAction compositeAction = (CompositeAction) joinPoint.getTarget();
    Field actionListField = CompositeAction.class.getDeclaredField("actionsList");
    actionListField.setAccessible(true);
    @SuppressWarnings("unchecked") List<Action> actionsList = (List<Action>) actionListField.get(compositeAction);
    boolean clickRequested = false;
    for (Action action : actionsList) {
        if (action instanceof ClickAction) {
            clickRequested = true;
        }
    }
    if (clickRequested) {
        ((CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false)).updateWindowsHandles();
    }
}
Also used : Field(java.lang.reflect.Field) CompositeAction(org.openqa.selenium.interactions.CompositeAction) Action(org.openqa.selenium.interactions.Action) ClickAction(org.openqa.selenium.interactions.ClickAction) CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) ClickAction(org.openqa.selenium.interactions.ClickAction) CompositeAction(org.openqa.selenium.interactions.CompositeAction) List(java.util.List) LinkedList(java.util.LinkedList) Before(org.aspectj.lang.annotation.Before)

Example 44 with CustomEventFiringWebDriver

use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.

the class CompositeActions method updateWindowHandles.

/**
 * Analyse action list and determine if a clic action occured. If it's the case, update window handles
 * @param actionsList
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
private void updateWindowHandles(LinkedList<Interaction> actionsList) throws NoSuchFieldException, IllegalAccessException {
    Boolean clic = null;
    boolean clickRequested = false;
    for (Interaction action : actionsList) {
        if (action.getClass().getName().contains("PointerInput$PointerPress")) {
            Field buttonField = action.getClass().getDeclaredField("button");
            buttonField.setAccessible(true);
            int button = buttonField.getInt(action);
            Field directionField = action.getClass().getDeclaredField("direction");
            directionField.setAccessible(true);
            String direction = directionField.get(action).toString();
            // only left button
            if (button != 0) {
                clic = null;
                continue;
            }
            // check we have a DOWN -> UP sequence
            if ("DOWN".equals(direction) && clic == null) {
                clic = true;
            } else if (Boolean.TRUE.equals(clic) && "UP".equals(direction)) {
                clic = null;
                clickRequested = true;
            } else {
                clic = null;
            }
        } else {
            clic = null;
        }
    }
    if (clickRequested) {
        ((CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false)).updateWindowsHandles();
    }
}
Also used : Field(java.lang.reflect.Field) CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) Interaction(org.openqa.selenium.interactions.Interaction) JoinPoint(org.aspectj.lang.JoinPoint)

Example 45 with CustomEventFiringWebDriver

use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.

the class Element method checkForMobile.

/**
 * Check if the current platform is a mobile platform
 * if it's the case, search for the element, else, raise a ScenarioException
 */
protected PerformsTouchActions checkForMobile() {
    CustomEventFiringWebDriver driver = (CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false);
    if (driver == null) {
        throw new ScenarioException("Driver has not already been created");
    }
    if (!SeleniumTestsContextManager.isMobileTest()) {
        throw new ScenarioException("action is available only for mobile platforms");
    }
    if (!(driver.getWebDriver() instanceof AppiumDriver<?>)) {
        throw new ScenarioException("action is available only for mobile platforms");
    }
    findElement(true);
    return (PerformsTouchActions) driver.getWebDriver();
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) AppiumDriver(io.appium.java_client.AppiumDriver) ScenarioException(com.seleniumtests.customexception.ScenarioException) PerformsTouchActions(io.appium.java_client.PerformsTouchActions)

Aggregations

CustomEventFiringWebDriver (com.seleniumtests.driver.CustomEventFiringWebDriver)77 Test (org.testng.annotations.Test)53 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)36 MockitoTest (com.seleniumtests.MockitoTest)35 Dimension (org.openqa.selenium.Dimension)26 WebDriver (org.openqa.selenium.WebDriver)14 GenericDriverTest (com.seleniumtests.GenericDriverTest)12 Point (org.openqa.selenium.Point)12 GenericTest (com.seleniumtests.GenericTest)10 ScenarioException (com.seleniumtests.customexception.ScenarioException)9 WebDriverException (org.openqa.selenium.WebDriverException)7 BeforeMethod (org.testng.annotations.BeforeMethod)7 DriverExceptionListener (com.seleniumtests.driver.DriverExceptionListener)6 SkipException (org.testng.SkipException)6 NLWebDriver (com.neotys.selenium.proxies.NLWebDriver)5 File (java.io.File)5 IOException (java.io.IOException)5 Rectangle (org.openqa.selenium.Rectangle)5 WebElement (org.openqa.selenium.WebElement)5 BrowserInfo (com.seleniumtests.browserfactory.BrowserInfo)4