Search in sources :

Example 1 with CustomEventFiringWebDriver

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

the class ScreenshotUtil method captureWebPage.

/**
 * Captures a web page. If the browser natively returns the whole page, nothing more is done. Else (only webview is returned), we scroll down the page to get more of the page
 * @param scrollDelay	time in ms to wait between scrolling and snapshot.
 * @return
 */
private BufferedImage captureWebPage(int scrollDelay) {
    Dimension contentDimension = ((CustomEventFiringWebDriver) driver).getContentDimension();
    Dimension viewDimensions = ((CustomEventFiringWebDriver) driver).getViewPortDimensionWithoutScrollbar();
    Integer topPixelsToCrop = SeleniumTestsContextManager.getThreadContext().getSnapshotTopCropping();
    Integer bottomPixelsToCrop = SeleniumTestsContextManager.getThreadContext().getSnapshotBottomCropping();
    double devicePixelRatio = ((CustomEventFiringWebDriver) driver).getDeviceAspectRatio();
    // issue #34: prevent getting image from HTMLUnit driver
    if (uiDriver != null && uiDriver.getConfig().getBrowserType() == BrowserType.HTMLUNIT) {
        return null;
    }
    // if cropping is automatic, get fixed header size to configure cropping
    if (topPixelsToCrop == null) {
        topPixelsToCrop = ((CustomEventFiringWebDriver) driver).getTopFixedHeaderSize().intValue();
    }
    if (bottomPixelsToCrop == null) {
        bottomPixelsToCrop = ((CustomEventFiringWebDriver) driver).getBottomFixedFooterSize().intValue();
    }
    int scrollY = 0;
    int scrollX = 0;
    // when cropping, we do not crop the first header and last footer => loops computing must take it into account (contentDimension.height - topPixelsToCrop - bottomPixelsToCrop)
    int maxLoops = (((contentDimension.height - topPixelsToCrop - bottomPixelsToCrop) / (viewDimensions.height - topPixelsToCrop - bottomPixelsToCrop)) + 1) * ((contentDimension.width / viewDimensions.width) + 1) + 3;
    // if a modal is displayed, do not capture more than the viewport
    if (((CustomEventFiringWebDriver) driver).isModalDisplayed()) {
        maxLoops = 1;
    }
    int loops = 0;
    int currentImageHeight = 0;
    // issue #435: be sure maxLoops is positive (could be negative in presence of fixed modal on long page)
    maxLoops = Math.max(1, maxLoops);
    try {
        ((CustomEventFiringWebDriver) driver).scrollTop();
    } catch (JavascriptException e) {
        maxLoops = 1;
    }
    BufferedImage currentImage = null;
    while (loops < maxLoops) {
        // do not crop top for the first vertical capture
        // do not crop bottom for the last vertical capture of if maxLoops == 1 (only a single capture)
        int cropTop = currentImageHeight != 0 ? topPixelsToCrop : 0;
        int cropBottom = currentImageHeight + (viewDimensions.height - cropTop) < contentDimension.height && maxLoops != 1 ? bottomPixelsToCrop : 0;
        // do not scroll to much so that we can crop fixed header without loosing content
        scrollY = currentImageHeight - cropTop;
        try {
            ((CustomEventFiringWebDriver) driver).scrollTo((int) (scrollX / devicePixelRatio), (int) (scrollY / devicePixelRatio));
        } catch (JavascriptException e) {
        // ignore javascript errors
        }
        // wait some time (if > 0) to let picture loading
        WaitHelper.waitForMilliSeconds(scrollDelay);
        BufferedImage image = capturePage(cropTop, cropBottom);
        if (image == null) {
            logger.error("Cannot capture page");
            break;
        }
        if (currentImage == null) {
            // issue #435: in case of a single capture, contentDimension may be different from viewDimension (with a displayed modal), create an image with viewDimension
            if (maxLoops == 1) {
                currentImage = new BufferedImage(viewDimensions.getWidth(), viewDimensions.getHeight(), BufferedImage.TYPE_INT_RGB);
            } else {
                currentImage = new BufferedImage(contentDimension.getWidth(), contentDimension.getHeight(), BufferedImage.TYPE_INT_RGB);
            }
            currentImage.createGraphics().drawImage(image, 0, 0, null);
            currentImageHeight = image.getHeight();
        } else {
            // crop top of the picture in case of the last vertical snapshot. It prevents duplication of content
            if (currentImageHeight + image.getHeight() > contentDimension.getHeight() || scrollX + image.getWidth() > contentDimension.getWidth()) {
                image = ImageProcessor.cropImage(image, Math.max(0, image.getWidth() - (contentDimension.getWidth() - scrollX)), Math.max(0, image.getHeight() - (contentDimension.getHeight() - currentImageHeight)), Math.min(image.getWidth(), contentDimension.getWidth() - scrollX), Math.min(image.getHeight(), contentDimension.getHeight() - currentImageHeight));
            }
            currentImage = ImageProcessor.concat(currentImage, image, scrollX, currentImageHeight);
            currentImageHeight += image.getHeight();
        }
        // all captures done, exit
        if ((currentImageHeight >= contentDimension.getHeight() && scrollX + image.getWidth() >= contentDimension.getWidth()) || SeleniumTestsContextManager.isAppTest()) {
            break;
        // we are at the bottom but something on the right has not been captured, move to the right and go on
        } else if (currentImageHeight >= contentDimension.getHeight()) {
            scrollX += image.getWidth();
            currentImageHeight = 0;
        }
        loops += 1;
    }
    return currentImage;
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) JavascriptException(org.openqa.selenium.JavascriptException) Dimension(org.openqa.selenium.Dimension) BufferedImage(java.awt.image.BufferedImage)

Example 2 with CustomEventFiringWebDriver

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

the class TestCustomEventFiringWebDriver method testContentDimension.

/**
 * Check standard web case where dimension comes from javascript call
 */
@Test(groups = { "ut" })
public void testContentDimension() {
    when(driver.executeScript(anyString(), eq(true))).thenReturn(Arrays.asList(120L, 80L));
    Dimension dim = ((CustomEventFiringWebDriver) eventDriver).getContentDimension();
    // no need to switch to default content if size is correctly returned
    verify(driver, never()).switchTo();
    // check we get the window dimension
    Assert.assertEquals(dim.height, 80);
    Assert.assertEquals(dim.width, 120);
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) Dimension(org.openqa.selenium.Dimension) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with CustomEventFiringWebDriver

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

the class TestCustomEventFiringWebDriver method testViewPortDimensionWithoutScrollbarNotReturned.

/**
 * issue #233: Test the case where JS_GET_VIEWPORT_SIZE_WIDTH and JS_GET_VIEWPORT_SIZE_HEIGHT returns 100000x100000 because dimension is not found
 * In this case, return the browser size
 */
@Test(groups = { "ut" })
public void testViewPortDimensionWithoutScrollbarNotReturned() {
    when(driver.executeScript(anyString(), eq(true))).thenReturn(100000L);
    Dimension dim = ((CustomEventFiringWebDriver) eventDriver).getViewPortDimensionWithoutScrollbar();
    // check we get the window dimension
    Assert.assertEquals(dim.height, 10000);
    Assert.assertEquals(dim.width, 2000);
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) Dimension(org.openqa.selenium.Dimension) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with CustomEventFiringWebDriver

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

the class TestCustomEventFiringWebDriver method testViewPortDimensionWithoutScrollbarNotGet2.

@Test(groups = { "ut" })
public void testViewPortDimensionWithoutScrollbarNotGet2() {
    // retry when getting height
    when(driver.executeScript(anyString(), eq(true))).thenReturn(120L).thenReturn(100000L).thenReturn(80L);
    Dimension dim = ((CustomEventFiringWebDriver) eventDriver).getViewPortDimensionWithoutScrollbar();
    verify(driver).switchTo();
    // check we get the window dimension
    Assert.assertEquals(dim.height, 80);
    Assert.assertEquals(dim.width, 120);
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) Dimension(org.openqa.selenium.Dimension) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with CustomEventFiringWebDriver

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

the class TestCustomEventFiringWebDriver method testContentDimensionWithoutScrollbarNonWebTest.

/**
 * For non web test, dimension is returned from driver call, not javascript
 */
@Test(groups = { "ut" })
public void testContentDimensionWithoutScrollbarNonWebTest() {
    eventDriver = spy(new CustomEventFiringWebDriver(driver, null, null, false, DriverMode.LOCAL, null, null).register(new DriverExceptionListener()));
    when(driver.executeScript(anyString(), eq(true))).thenReturn(120L).thenReturn(80L);
    Dimension dim = ((CustomEventFiringWebDriver) eventDriver).getViewPortDimensionWithoutScrollbar();
    // check we get the window dimension
    Assert.assertEquals(dim.height, 100);
    Assert.assertEquals(dim.width, 100);
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) DriverExceptionListener(com.seleniumtests.driver.DriverExceptionListener) Dimension(org.openqa.selenium.Dimension) Test(org.testng.annotations.Test) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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