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);
}
}
}
}
}
}
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);
}
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);
}
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();
}
}
}
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())));
}
Aggregations