Search in sources :

Example 6 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot in project seleniumRobot by bhecquet.

the class TestTestStep method testSnapshotRenaming.

/**
 * Test that when adding a snapshot to a test step, it's renamed with a name
 * containing test name, step name and index
 *
 * @throws IOException
 */
@Test(groups = { "ut" })
public void testSnapshotRenaming() throws IOException {
    TestStep step = new TestStep("step1", null, new ArrayList<>(), true);
    ScreenShot screenshot = new ScreenShot();
    File tmpImgFile = File.createTempFile("img", ".png");
    File tmpHtmlFile = File.createTempFile("html", ".html");
    screenshot.setOutputDirectory(tmpImgFile.getParent());
    screenshot.setLocation("http://mysite.com");
    screenshot.setTitle("mysite");
    screenshot.setImagePath(tmpImgFile.getName());
    screenshot.setHtmlSourcePath(tmpHtmlFile.getName());
    step.addSnapshot(new Snapshot(screenshot, "main", SnapshotCheckType.TRUE), 0, null);
    Assert.assertEquals(step.getSnapshots().get(0).getScreenshot().getImagePath(), "N-A_0-1_step1--" + tmpImgFile.getName().substring(tmpImgFile.getName().length() - 10));
    Assert.assertEquals(step.getSnapshots().get(0).getScreenshot().getHtmlSourcePath(), "N-A_0-1_step1--" + tmpHtmlFile.getName().substring(tmpHtmlFile.getName().length() - 10));
    tmpImgFile.deleteOnExit();
    tmpHtmlFile.deleteOnExit();
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) File(java.io.File) GenericFile(com.seleniumtests.reporter.logger.GenericFile) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest)

Example 7 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot 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 8 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot in project seleniumRobot by bhecquet.

the class TestTestStep method testMultipleSnapshotsInStep.

@Test(groups = { "ut" })
public void testMultipleSnapshotsInStep() throws IOException {
    TestStep step = new TestStep("step1", null, new ArrayList<>(), true);
    ScreenShot screenshot = new ScreenShot();
    File tmpImgFile = File.createTempFile("img", ".png");
    File tmpImgFile2 = Paths.get(tmpImgFile.getParent(), "screenshots", tmpImgFile.getName()).toFile();
    FileUtils.moveFile(tmpImgFile, tmpImgFile2);
    File tmpHtmlFile = File.createTempFile("html", ".html");
    File tmpHtmlFile2 = Paths.get(tmpHtmlFile.getParent(), "htmls", tmpHtmlFile.getName()).toFile();
    FileUtils.moveFile(tmpHtmlFile, tmpHtmlFile2);
    screenshot.setOutputDirectory(tmpImgFile.getParent());
    screenshot.setLocation("http://mysite.com");
    screenshot.setTitle("mysite");
    screenshot.setImagePath("screenshots/" + tmpImgFile2.getName());
    screenshot.setHtmlSourcePath("htmls/" + tmpHtmlFile2.getName());
    step.addSnapshot(new Snapshot(screenshot, "main", SnapshotCheckType.TRUE), 0, null);
    ScreenShot screenshotSubStep = new ScreenShot();
    File tmpImgFileSubStep = File.createTempFile("img", ".png");
    File tmpImgFileSubStep2 = Paths.get(tmpImgFileSubStep.getParent(), "screenshots", tmpImgFileSubStep.getName()).toFile();
    FileUtils.moveFile(tmpImgFileSubStep, tmpImgFileSubStep2);
    File tmpHtmlFileSubStep = File.createTempFile("html", ".html");
    File tmpHtmlFileSubStep2 = Paths.get(tmpHtmlFileSubStep.getParent(), "htmls", tmpHtmlFileSubStep.getName()).toFile();
    FileUtils.moveFile(tmpHtmlFileSubStep, tmpHtmlFileSubStep2);
    screenshotSubStep.setOutputDirectory(tmpImgFileSubStep.getParent());
    screenshotSubStep.setLocation("http://mysite.com");
    screenshotSubStep.setTitle("mysite");
    screenshotSubStep.setImagePath("screenshots/" + tmpImgFileSubStep2.getName());
    screenshotSubStep.setHtmlSourcePath("htmls/" + tmpHtmlFileSubStep2.getName());
    step.addSnapshot(new Snapshot(screenshotSubStep, "main", SnapshotCheckType.TRUE), 0, null);
    Assert.assertEquals(step.getSnapshots().get(0).getScreenshot().getImagePath(), "screenshots/N-A_0-1_step1--" + tmpImgFile2.getName().substring(tmpImgFile2.getName().length() - 10));
    Assert.assertEquals(step.getSnapshots().get(0).getScreenshot().getHtmlSourcePath(), "htmls/N-A_0-1_step1--" + tmpHtmlFile2.getName().substring(tmpHtmlFile2.getName().length() - 10));
    Assert.assertEquals(step.getSnapshots().get(1).getScreenshot().getImagePath(), "screenshots/N-A_0-2_step1--" + tmpImgFileSubStep2.getName().substring(tmpImgFileSubStep2.getName().length() - 10));
    Assert.assertEquals(step.getSnapshots().get(1).getScreenshot().getHtmlSourcePath(), "htmls/N-A_0-2_step1--" + tmpHtmlFileSubStep2.getName().substring(tmpHtmlFileSubStep2.getName().length() - 10));
    tmpImgFile.deleteOnExit();
    tmpHtmlFile.deleteOnExit();
    tmpImgFileSubStep.deleteOnExit();
    tmpHtmlFileSubStep.deleteOnExit();
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) File(java.io.File) GenericFile(com.seleniumtests.reporter.logger.GenericFile) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest)

Example 9 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot in project seleniumRobot by bhecquet.

the class TestTestStep method testSnapshotRenamingNoRandom.

/**
 * Check case where random part of attachment is removed
 * @throws IOException
 */
@Test(groups = { "ut" })
public void testSnapshotRenamingNoRandom() throws IOException {
    SeleniumTestsContextManager.getThreadContext().setRandomInAttachmentNames(false);
    TestStep step = new TestStep("step1", null, new ArrayList<>(), true);
    ScreenShot screenshot = new ScreenShot();
    File tmpImgFile = File.createTempFile("img", ".png");
    File tmpHtmlFile = File.createTempFile("html", ".html");
    screenshot.setOutputDirectory(tmpImgFile.getParent());
    screenshot.setLocation("http://mysite.com");
    screenshot.setTitle("mysite");
    screenshot.setImagePath(tmpImgFile.getName());
    screenshot.setHtmlSourcePath(tmpHtmlFile.getName());
    step.addSnapshot(new Snapshot(screenshot, "main", SnapshotCheckType.TRUE), 0, null);
    Assert.assertEquals(step.getSnapshots().get(0).getScreenshot().getImagePath(), "N-A_0-1_step1-.png");
    Assert.assertEquals(step.getSnapshots().get(0).getScreenshot().getHtmlSourcePath(), "N-A_0-1_step1-.html");
    tmpImgFile.deleteOnExit();
    tmpHtmlFile.deleteOnExit();
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) File(java.io.File) GenericFile(com.seleniumtests.reporter.logger.GenericFile) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest)

Example 10 with Snapshot

use of com.seleniumtests.reporter.logger.Snapshot in project seleniumRobot by bhecquet.

the class TestTestStep method testTestSubSnapshotPositionAndParent.

@Test(groups = { "ut" })
public void testTestSubSnapshotPositionAndParent() throws IOException {
    TestStep step = new TestStep("step1", null, new ArrayList<>(), true);
    TestStep subStep = new TestStep("subStep1", null, new ArrayList<>(), true);
    step.addStep(subStep);
    ScreenShot screenshot = new ScreenShot();
    File tmpImgFile = File.createTempFile("img", ".png");
    File tmpHtmlFile = File.createTempFile("html", ".html");
    screenshot.setOutputDirectory(tmpImgFile.getParent());
    screenshot.setLocation("http://mysite.com");
    screenshot.setTitle("mysite");
    screenshot.setImagePath(tmpImgFile.getName());
    screenshot.setHtmlSourcePath(tmpHtmlFile.getName());
    Snapshot snapshot = new Snapshot(screenshot, "main", SnapshotCheckType.TRUE);
    step.addSnapshot(snapshot, 0, null);
    Assert.assertEquals(snapshot.getPosition(), 0);
    Assert.assertEquals(snapshot.getParent(), step);
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot) File(java.io.File) GenericFile(com.seleniumtests.reporter.logger.GenericFile) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest)

Aggregations

Snapshot (com.seleniumtests.reporter.logger.Snapshot)29 ScreenShot (com.seleniumtests.driver.screenshots.ScreenShot)23 TestStep (com.seleniumtests.reporter.logger.TestStep)22 File (java.io.File)18 Test (org.testng.annotations.Test)18 GenericTest (com.seleniumtests.GenericTest)15 ArrayList (java.util.ArrayList)11 GenericFile (com.seleniumtests.reporter.logger.GenericFile)8 TestAction (com.seleniumtests.reporter.logger.TestAction)5 SeleniumRobotServerException (com.seleniumtests.customexception.SeleniumRobotServerException)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 Matcher (java.util.regex.Matcher)3 Field (com.seleniumtests.connectors.selenium.fielddetector.Field)2 Label (com.seleniumtests.connectors.selenium.fielddetector.Label)2 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Rectangle (org.openqa.selenium.Rectangle)2