Search in sources :

Example 6 with TestStep

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

the class WebUIDriver method createRemoteWebDriver.

/**
 * prepare driver:
 * - create it
 * - add listeners
 * - create and start video capture
 * - create and start network capture proxy
 * - record driver and browser pid so that they can be deleted at the end of test session
 * @return
 */
public WebDriver createRemoteWebDriver() {
    webDriverBuilder = getWebDriverBuilderFactory();
    logger.info("driver mode: " + config.getMode());
    synchronized (createDriverLock) {
        // get browser info used to start this driver. It will be used then for managing pids
        BrowserInfo browserInfo = webDriverBuilder.getSelectedBrowserInfo();
        List<Long> existingPids = new ArrayList<>();
        // get pid pre-existing the creation of this driver. This helps filtering drivers launched by other tests or users
        if (browserInfo != null) {
            existingPids.addAll(browserInfo.getDriverAndBrowserPid(new ArrayList<>()));
        }
        TestStep cuurrentTestStep = TestStepManager.getCurrentRootTestStep();
        long start = new Date().getTime();
        long duration;
        try {
            driver = webDriverBuilder.createWebDriver();
        } finally {
            duration = new Date().getTime() - start;
            if (cuurrentTestStep != null) {
                cuurrentTestStep.setDurationToExclude(duration);
            }
            scenarioLogger.info(String.format("driver creation took: %.1f secs", duration / 1000.0));
        }
        WaitHelper.waitForSeconds(2);
        List<Long> driverPids = new ArrayList<>();
        // get the created PIDs
        if (browserInfo != null) {
            driverPids = browserInfo.getDriverAndBrowserPid(existingPids);
        }
        // issue #280: we use 'webDriverBuilder.getSelectedBrowserInfo()' as 'browserInfo' variable is null for grid, whereas, 'webDriverBuilder.getSelectedBrowserInfo()'
        // gets an updated version once the driver has been created on grid
        driver = handleListeners(driver, webDriverBuilder.getSelectedBrowserInfo(), driverPids);
        if (driver != null) {
            MutableCapabilities caps = ((CustomEventFiringWebDriver) driver).getInternalCapabilities();
            caps.setCapability(DriverUsage.STARTUP_DURATION, duration);
            caps.setCapability(DriverUsage.START_TIME, start);
            // capability from IDestkopCapabilitiesFactory is not available when we request capabilities from driver.
            if (config.getTestContext() != null && config.getTestContext().getTestNGResult() != null) {
                String testName = TestNGResultUtils.getTestName(config.getTestContext().getTestNGResult());
                caps.setCapability(DriverUsage.TEST_NAME, testName);
            }
        }
        if (config.getBrowserMobProxy() != null) {
            config.getBrowserMobProxy().newHar(SeleniumTestsContextManager.getThreadContext().getRelativeOutputDir());
        }
        if (config.getVideoCapture() != VideoCaptureMode.FALSE && videoRecorder.get() == null) {
            try {
                VideoRecorder recorder = CustomEventFiringWebDriver.startVideoCapture(SeleniumTestsContextManager.getThreadContext().getRunMode(), SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnector(), new File(SeleniumTestsContextManager.getThreadContext().getOutputDirectory()), "videoCapture.avi");
                videoRecorder.set(recorder);
                TestStepManager.setVideoStartDate();
            } catch (ScenarioException e) {
                logger.warn("Video capture won't start: " + e.getMessage());
            }
        }
    }
    return driver;
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) BrowserInfo(com.seleniumtests.browserfactory.BrowserInfo) ArrayList(java.util.ArrayList) VideoRecorder(com.seleniumtests.util.video.VideoRecorder) Date(java.util.Date) MutableCapabilities(org.openqa.selenium.MutableCapabilities) File(java.io.File) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 7 with TestStep

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

the class TestStepManager method logTestStep.

/**
 * Logs the testStep for this test
 * Once logging is done, parentTestStep and currentRootTestStep are reset to avoid storing new data in them
 * @param testStep
 * @param storeStep
 */
public static void logTestStep(TestStep testStep, boolean storeStep) {
    List<TestAction> actionList = testStep.getStepActions();
    if (!actionList.isEmpty()) {
        for (TestAction action : actionList) {
            if (action instanceof TestStep) {
                logTestStep((TestStep) action, false);
            }
        }
    }
    if (storeStep) {
        // notify each TestStepManager about the new test step (useful for AfterClass / AfterTest configuration methods)
        for (SeleniumTestsContext testContext : SeleniumTestsContextManager.getContextForCurrentTestState()) {
            TestStepManager stepManager = testContext.getTestStepManager();
            stepManager.getTestSteps().add(testStep);
            stepManager.setRootTestStep(null);
            stepManager.setRunningTestStep(null);
        }
    }
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) TestAction(com.seleniumtests.reporter.logger.TestAction)

Example 8 with TestStep

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

the class TestTasks method terminateCurrentStep.

public static void terminateCurrentStep() {
    NLWebDriver neoloadDriver = WebUIDriver.getNeoloadDriver();
    // log the previous step if it exists and create the new one
    TestStep previousStep = TestStepManager.getCurrentRootTestStep();
    if (previousStep != null) {
        previousStep.updateDuration();
        TestStepManager.logTestStep(previousStep);
        if (neoloadDriver != null) {
            neoloadDriver.stopTransaction();
        }
    }
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) NLWebDriver(com.neotys.selenium.proxies.NLWebDriver)

Example 9 with TestStep

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

the class ErrorCauseFinder method compareStepInErrorWithReference.

/**
 * Compare the failed step with it's reference that can be found on seleniumRobot server
 * If reference cannot be found, skip this step
 * @return
 */
public List<ErrorCause> compareStepInErrorWithReference() {
    logger.info("Searching causes: comparing with references");
    List<ErrorCause> causes = new ArrayList<>();
    // do not seearch again
    if (TestNGResultUtils.isErrorCauseSearchedInReferencePicture(testResult)) {
        return causes;
    }
    // don't analyze if result has not been recorded on seleniumRobot server
    TestStepManager testStepManager = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager();
    if (testStepManager.getLastTestStep() == null || testStepManager.getLastTestStep().getStepResultId() == null) {
        return causes;
    }
    for (TestStep testStep : testStepManager.getTestSteps()) {
        // stepResultId is set when step recording is done on server
        Integer stepResultId = testStep.getStepResultId();
        if (Boolean.TRUE.equals(testStep.getFailed()) && !(testStep.getActionException() instanceof AssertionError) && stepResultId != null) {
            try {
                Snapshot stepSnapshot = testStep.getSnapshots().stream().filter(s -> s.getCheckSnapshot().recordSnapshotOnServerForReference()).collect(Collectors.toList()).get(0);
                File stepSnapshotFile = new File(stepSnapshot.getScreenshot().getFullImagePath());
                File referenceSnapshot = SeleniumRobotSnapshotServerConnector.getInstance().getReferenceSnapshot(stepResultId);
                if (referenceSnapshot == null) {
                    continue;
                }
                // perform a match between the picture of this step and the reference stored on server
                // We look at presence, position and text of each field
                List<Label> missingLabels = new ArrayList<>();
                List<Field> missingFields = new ArrayList<>();
                int matching = compareReferenceToStepSnapshot(stepSnapshotFile, referenceSnapshot, missingLabels, missingFields);
                // bad matching: the reference does not match at all the current step, we will check with other reference steps
                if (matching < 50) {
                    searchMatchingInPreviousStep(testStepManager, testStep, stepSnapshotFile, causes);
                // middle matching: we may be on the right web page but the page has changed (some fields appeared or disappeared)
                // or the text changed slightly. This could mean application changed
                } else if (matching < 90) {
                    // draw missing labels and fields
                    for (Label missingLabel : missingLabels) {
                        Rectangle rect = missingLabel.getRectangle();
                        Line2D.Double line = new Line2D.Double(rect.x, rect.y + rect.height, rect.x + rect.width, rect.y + rect.height);
                        ImageProcessor.drawLines(referenceSnapshot, Color.RED, line);
                    }
                    ImageProcessor.drawRectangles(referenceSnapshot, Color.RED, missingFields.stream().map(Field::getRectangle).collect(Collectors.toList()).toArray(new Rectangle[] {}));
                    causes.add(new ErrorCause(ErrorType.APPLICATION_CHANGED, formatApplicationChangedDescription(missingLabels, missingFields), testStep));
                }
                break;
            } catch (IndexOutOfBoundsException e) {
            // skip this step
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }
    TestNGResultUtils.setErrorCauseSearchedInReferencePicture(testResult, true);
    return causes;
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) TestStepManager(com.seleniumtests.core.TestStepManager) ArrayList(java.util.ArrayList) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) Rectangle(java.awt.Rectangle) Line2D(java.awt.geom.Line2D) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) Snapshot(com.seleniumtests.reporter.logger.Snapshot) Field(com.seleniumtests.connectors.selenium.fielddetector.Field) File(java.io.File)

Example 10 with TestStep

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

the class ErrorCauseFinder method findErrorInLastStepSnapshots.

/**
 * Search in snapshots of the last step if there are any displayed errors (error messages or fields in error)
 * @return
 */
public List<ErrorCause> findErrorInLastStepSnapshots() {
    logger.info("Searching causes: find errors in last snapshot");
    List<ErrorCause> causes = new ArrayList<>();
    // do not seearch again
    if (TestNGResultUtils.isErrorCauseSearchedInLastStep(testResult)) {
        return causes;
    }
    TestStep lastTestStep = TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getLastTestStep();
    TestStep lastFailedStep = lastTestStep;
    for (TestStep testStep : TestNGResultUtils.getSeleniumRobotTestContext(testResult).getTestStepManager().getTestSteps()) {
        if (Boolean.TRUE.equals(testStep.getFailed()) && testStep != lastTestStep) {
            lastFailedStep = testStep;
        }
    }
    if (lastTestStep != null) {
        for (Snapshot snapshot : lastTestStep.getSnapshots()) {
            try {
                ImageFieldDetector imageFieldDetector = new ImageFieldDetector(new File(snapshot.getScreenshot().getFullImagePath()), 1, FieldType.ERROR_MESSAGES_AND_FIELDS);
                List<Field> fields = imageFieldDetector.detectFields();
                List<Label> labels = imageFieldDetector.detectLabels();
                // are some text considered as error messages (mainly in red on page)
                parseFields(causes, fields, labels, lastFailedStep);
                // do some label contain "error" or "problem"
                parseLabels(causes, labels, lastFailedStep);
            } catch (Exception e) {
                logger.error("Error searching for errors in last snapshots: " + e.getMessage());
                break;
            }
        }
        TestNGResultUtils.setErrorCauseSearchedInLastStep(testResult, true);
    }
    return causes;
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) Field(com.seleniumtests.connectors.selenium.fielddetector.Field) ImageFieldDetector(com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector) ArrayList(java.util.ArrayList) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) File(java.io.File) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException)

Aggregations

TestStep (com.seleniumtests.reporter.logger.TestStep)190 Test (org.testng.annotations.Test)148 GenericTest (com.seleniumtests.GenericTest)120 ArrayList (java.util.ArrayList)80 TestAction (com.seleniumtests.reporter.logger.TestAction)47 File (java.io.File)37 ScreenShot (com.seleniumtests.driver.screenshots.ScreenShot)25 Snapshot (com.seleniumtests.reporter.logger.Snapshot)24 ErrorCause (com.seleniumtests.core.testanalysis.ErrorCause)19 TestMessage (com.seleniumtests.reporter.logger.TestMessage)16 GenericFile (com.seleniumtests.reporter.logger.GenericFile)15 MockitoTest (com.seleniumtests.MockitoTest)12 HashMap (java.util.HashMap)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 ITestResult (org.testng.ITestResult)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 Uft (com.seleniumtests.connectors.extools.Uft)8 DriverExceptions (com.seleniumtests.customexception.DriverExceptions)8 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)7 SeleniumRobotServerException (com.seleniumtests.customexception.SeleniumRobotServerException)6