Search in sources :

Example 16 with ScreenShot

use of com.seleniumtests.driver.screenshots.ScreenShot in project seleniumRobot by bhecquet.

the class SeleniumRobotServerTestRecorder method recordSteps.

/**
 * Record test steps to server
 * @param serverConnector
 * @param sessionId
 * @param testCaseInSessionId
 * @param testSteps
 */
private void recordSteps(SeleniumRobotSnapshotServerConnector serverConnector, Integer sessionId, Integer testCaseInSessionId, List<TestStep> testSteps, ITestResult testResult) {
    for (TestStep testStep : testSteps) {
        logger.info(String.format("Recording step %s on server", testStep.getName()));
        // record test step
        Integer testStepId = serverConnector.createTestStep(testStep.getName(), testCaseInSessionId);
        String stepLogs = testStep.toJson().toString();
        Integer stepResultId = serverConnector.recordStepResult(!testStep.getFailed(), stepLogs, testStep.getDuration(), sessionId, testCaseInSessionId, testStepId);
        testStep.setStepResultId(stepResultId);
        // sends all snapshots that are flagged as comparable
        for (Snapshot snapshot : new ArrayList<>(testStep.getSnapshots())) {
            if (snapshot.getCheckSnapshot().recordSnapshotOnServerForComparison() && SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerCompareSnapshot()) {
                if (snapshot.getName() == null || snapshot.getName().isEmpty()) {
                    logger.warn("Snapshot hasn't any name, it won't be sent to server");
                    continue;
                }
                try {
                    Integer snapshotId = serverConnector.createSnapshot(snapshot, sessionId, testCaseInSessionId, stepResultId);
                    for (Rectangle excludeZone : snapshot.getCheckSnapshot().getExcludeElementsRect()) {
                        serverConnector.createExcludeZones(excludeZone, snapshotId);
                    }
                    logger.info("Check snapshot created");
                } catch (SeleniumRobotServerException e) {
                    logger.error("Could not create snapshot on server", e);
                }
            // record reference image on server if step is successful
            } else if (snapshot.getCheckSnapshot().recordSnapshotOnServerForReference() && SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerRecordResults()) {
                if (Boolean.FALSE.equals(testStep.getFailed())) {
                    try {
                        serverConnector.createStepReferenceSnapshot(snapshot, stepResultId);
                        logger.info("Step OK: reference created");
                    } catch (SeleniumRobotServerException e) {
                        logger.error("Could not create reference snapshot on server", e);
                    }
                    // remove this snapshot, extracted from video as it won't be used anymore
                    testStep.getSnapshots().remove(snapshot);
                } else {
                    try {
                        // move snapshot to "screenshots" directory as "video" directory will be removed at the end of the test
                        snapshot.relocate(TestNGResultUtils.getSeleniumRobotTestContext(testResult).getOutputDirectory(), ScreenshotUtil.SCREENSHOT_DIR + "/" + snapshot.getScreenshot().getImageName());
                        File referenceSnapshot = serverConnector.getReferenceSnapshot(stepResultId);
                        if (referenceSnapshot != null) {
                            logger.info("Step KO: reference snapshot got from server");
                            Path newPath = Paths.get(TestNGResultUtils.getSeleniumRobotTestContext(testResult).getScreenshotOutputDirectory(), referenceSnapshot.getName()).toAbsolutePath();
                            FileUtils.moveFile(referenceSnapshot, newPath.toFile());
                            testStep.addSnapshot(new Snapshot(new ScreenShot(newPath.getParent().getParent().relativize(newPath).toString()), "Valid-reference", SnapshotCheckType.FALSE), 0, null);
                            // change flag so that it's displayed in report (by default reference image extracted from video are not displayed)
                            snapshot.setDisplayInReport(true);
                        }
                    } catch (SeleniumRobotServerException e) {
                        logger.error("Could not get reference snapshot from server", e);
                    } catch (IOException e) {
                        logger.error("Could not copy reference snapshot", e);
                    }
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) ArrayList(java.util.ArrayList) Rectangle(org.openqa.selenium.Rectangle) IOException(java.io.IOException) File(java.io.File) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Example 17 with ScreenShot

use of com.seleniumtests.driver.screenshots.ScreenShot in project seleniumRobot by bhecquet.

the class PageObject method captureDesktopSnapshot.

/**
 * Capture a desktop snapshot (only the main screen in multiple screen environment) for storing in test step
 * @param snapshotName		the snapshot name
 * @param checkSnapshot		if true, will send snapshot to server (when seleniumRobot is configured for this) for comparison
 */
public void captureDesktopSnapshot(String snapshotName, SnapshotCheckType checkSnapshot) {
    ScreenShot screenShot = screenshotUtil.capture(SnapshotTarget.MAIN_SCREEN, ScreenShot.class);
    storeSnapshot(snapshotName, screenShot, checkSnapshot);
}
Also used : ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot)

Example 18 with ScreenShot

use of com.seleniumtests.driver.screenshots.ScreenShot in project seleniumRobot by bhecquet.

the class PageObject method captureElementSnapshot.

/**
 * Capture a portion of the page by giving the element to capture
 * @param snapshotName		the snapshot name
 * @param element			the element to capture
 * @param checkSnapshot		if true, will send snapshot to server (when seleniumRobot is configured for this) for comparison
 */
public void captureElementSnapshot(String snapshotName, WebElement element, SnapshotCheckType checkSnapshot) {
    SnapshotTarget snapshotTarget = new SnapshotTarget(element);
    ScreenShot screenShot = screenshotUtil.capture(snapshotTarget, ScreenShot.class, computeScrollDelay(checkSnapshot));
    // check SnapshotCheckType configuration is compatible with the snapshot
    checkSnapshot.check(snapshotTarget);
    storeSnapshot(snapshotName, screenShot, checkSnapshot);
}
Also used : SnapshotTarget(com.seleniumtests.driver.screenshots.SnapshotTarget) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot)

Example 19 with ScreenShot

use of com.seleniumtests.driver.screenshots.ScreenShot in project seleniumRobot by bhecquet.

the class TestScreenshotUtil method testScreenshotIsNotNullWhenForced.

@Test(groups = { "it" })
public void testScreenshotIsNotNullWhenForced(ITestContext testContext) throws Exception {
    SeleniumTestsContextManager.getThreadContext().setTestType(TestType.WEB);
    SeleniumTestsContextManager.getThreadContext().setCaptureSnapshot(false);
    SeleniumTestsContextManager.getThreadContext().setBrowser("chrome");
    WebDriver localDriver = null;
    try {
        localDriver = WebUIDriver.getWebDriver(true);
        ScreenShot screenshot = new ScreenshotUtil(localDriver).capture(SnapshotTarget.PAGE, ScreenShot.class, true);
        Assert.assertNotNull(screenshot);
        Assert.assertTrue(new File(screenshot.getFullImagePath()).exists());
    } finally {
        if (localDriver != null) {
            localDriver.close();
            WebUIDriver.cleanUp();
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) ScreenshotUtil(com.seleniumtests.driver.screenshots.ScreenshotUtil) File(java.io.File) Test(org.testng.annotations.Test) ReporterTest(com.seleniumtests.it.reporter.ReporterTest)

Example 20 with ScreenShot

use of com.seleniumtests.driver.screenshots.ScreenShot in project seleniumRobot by bhecquet.

the class TestBrowserSnapshot method testMultipleWindowsCapture.

/**
 * Check we get capture for each window
 * Check also we remain on the same window handle after the capture
 * Check that first captured image (popup) is smaller than the second one (main page)
 */
@Test(groups = { "it" })
public void testMultipleWindowsCapture() {
    String currentWindowHandle = driver.getWindowHandle();
    DriverTestPage.link.click();
    List<ScreenShot> screenshots = new ScreenshotUtil().capture(SnapshotTarget.PAGE, ScreenShot.class, true, false);
    Assert.assertEquals(screenshots.size(), 2);
    Assert.assertEquals(currentWindowHandle, driver.getWindowHandle());
    Assert.assertTrue(FileUtils.sizeOf(new File(((ScreenShot) screenshots.get(0)).getFullImagePath())) < FileUtils.sizeOf(new File(((ScreenShot) screenshots.get(1)).getFullImagePath())));
}
Also used : ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) ScreenshotUtil(com.seleniumtests.driver.screenshots.ScreenshotUtil) File(java.io.File) Test(org.testng.annotations.Test) GenericMultiBrowserTest(com.seleniumtests.it.driver.support.GenericMultiBrowserTest)

Aggregations

ScreenShot (com.seleniumtests.driver.screenshots.ScreenShot)63 Test (org.testng.annotations.Test)45 File (java.io.File)37 GenericTest (com.seleniumtests.GenericTest)35 Snapshot (com.seleniumtests.reporter.logger.Snapshot)24 TestStep (com.seleniumtests.reporter.logger.TestStep)24 ArrayList (java.util.ArrayList)14 ScreenshotUtil (com.seleniumtests.driver.screenshots.ScreenshotUtil)10 GenericFile (com.seleniumtests.reporter.logger.GenericFile)8 GenericMultiBrowserTest (com.seleniumtests.it.driver.support.GenericMultiBrowserTest)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 CustomEventFiringWebDriver (com.seleniumtests.driver.CustomEventFiringWebDriver)5 TestAction (com.seleniumtests.reporter.logger.TestAction)5 BrowserInfo (com.seleniumtests.browserfactory.BrowserInfo)3 BasicIssue (com.atlassian.jira.rest.client.api.domain.BasicIssue)2 User (com.atlassian.jira.rest.client.api.domain.User)2 IssueInput (com.atlassian.jira.rest.client.api.domain.input.IssueInput)2 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)2 ScenarioException (com.seleniumtests.customexception.ScenarioException)2 SnapshotTarget (com.seleniumtests.driver.screenshots.SnapshotTarget)2