Search in sources :

Example 1 with Field

use of com.seleniumtests.connectors.selenium.fielddetector.Field 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 2 with Field

use of com.seleniumtests.connectors.selenium.fielddetector.Field 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)

Example 3 with Field

use of com.seleniumtests.connectors.selenium.fielddetector.Field in project seleniumRobot by bhecquet.

the class TestErrorCauseFinder method testCompareStepInErrorWithReferenceMediumMatch.

/**
 * Matching between reference picture stored on server and the one for current test is good enough (we are on the right page but something changed)
 * @throws Exception
 */
@Test(groups = { "ut" })
public void testCompareStepInErrorWithReferenceMediumMatch() throws Exception {
    ITestResult testResult = Reporter.getCurrentTestResult();
    TestNGResultUtils.setSeleniumRobotTestContext(testResult, SeleniumTestsContextManager.getThreadContext());
    SeleniumTestsContextManager.getThreadContext().getTestStepManager().setTestSteps(Arrays.asList(step1, stepFailed, lastStep));
    // comparison successful
    PowerMockito.whenNew(StepReferenceComparator.class).withArguments(new File(stepFailed.getSnapshots().get(0).getScreenshot().getFullImagePath()), referenceImgStep2).thenReturn(stepReferenceComparatorStep2);
    when(stepReferenceComparatorStep2.compare()).thenReturn(50);
    when(stepReferenceComparatorStep2.getMissingFields()).thenReturn(Arrays.asList(new Field(0, 100, 0, 20, "", "field")));
    List<ErrorCause> causes = new ErrorCauseFinder(testResult).compareStepInErrorWithReference();
    Assert.assertEquals(causes.size(), 1);
    Assert.assertEquals(causes.get(0).getType(), ErrorType.APPLICATION_CHANGED);
    Assert.assertEquals(causes.get(0).getDescription(), "1 field(s) missing: \n" + "field[text=]: java.awt.Rectangle[x=0,y=0,width=100,height=20]\n");
    Assert.assertTrue(TestNGResultUtils.isErrorCauseSearchedInReferencePicture(testResult));
    // check rectangle has been drawn around the missing field
    BufferedImage image = ImageIO.read(referenceImgStep2);
    Assert.assertEquals(new Color(image.getRGB(0, 20)), Color.RED);
    Assert.assertEquals(new Color(image.getRGB(0, 0)), Color.RED);
    Assert.assertEquals(new Color(image.getRGB(100, 20)), Color.RED);
    Assert.assertEquals(new Color(image.getRGB(100, 0)), Color.RED);
}
Also used : Field(com.seleniumtests.connectors.selenium.fielddetector.Field) ErrorCauseFinder(com.seleniumtests.core.testanalysis.ErrorCauseFinder) ITestResult(org.testng.ITestResult) ErrorCause(com.seleniumtests.core.testanalysis.ErrorCause) Color(java.awt.Color) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest)

Example 4 with Field

use of com.seleniumtests.connectors.selenium.fielddetector.Field in project seleniumRobot by bhecquet.

the class TestErrorCauseFinder method testSearchInLastStepErrorMessageWithFailedStep.

/**
 * When a failed step is present, attach ErrorCause to this one instead of "Last Step"
 * @throws Exception
 */
@Test(groups = { "ut" })
public void testSearchInLastStepErrorMessageWithFailedStep() throws Exception {
    ITestResult testResult = Reporter.getCurrentTestResult();
    TestNGResultUtils.setSeleniumRobotTestContext(testResult, SeleniumTestsContextManager.getThreadContext());
    SeleniumTestsContextManager.getThreadContext().getTestStepManager().setTestSteps(Arrays.asList(step1, stepFailed, lastStep));
    PowerMockito.whenNew(ImageFieldDetector.class).withArguments(new File(lastStep.getSnapshots().get(0).getScreenshot().getFullImagePath()), (double) 1, FieldType.ERROR_MESSAGES_AND_FIELDS).thenReturn(imageFieldDetector);
    List<Label> labels = new ArrayList<>();
    labels.add(new Label(0, 100, 20, 50, "il y a eu un gros problème"));
    labels.add(new Label(0, 100, 100, 120, "some error"));
    List<Field> fields = new ArrayList<>();
    fields.add(new Field(0, 100, 0, 20, "", "field"));
    fields.add(new Field(0, 100, 200, 220, "", "field"));
    when(imageFieldDetector.detectFields()).thenReturn(fields);
    when(imageFieldDetector.detectLabels()).thenReturn(labels);
    List<ErrorCause> causes = new ErrorCauseFinder(testResult).findErrorInLastStepSnapshots();
    Assert.assertEquals(causes.size(), 2);
    Assert.assertEquals(causes.get(0).getType(), ErrorType.ERROR_MESSAGE);
    Assert.assertEquals(causes.get(0).getDescription(), "il y a eu un gros problème");
    // error cause is associated to the last failed step
    Assert.assertEquals(causes.get(0).getTestStep(), stepFailed);
    Assert.assertEquals(causes.get(1).getType(), ErrorType.ERROR_MESSAGE);
    Assert.assertEquals(causes.get(1).getDescription(), "some error");
    // error cause is associated to the last failed step
    Assert.assertEquals(causes.get(1).getTestStep(), stepFailed);
    Assert.assertTrue(TestNGResultUtils.isErrorCauseSearchedInLastStep(testResult));
}
Also used : Field(com.seleniumtests.connectors.selenium.fielddetector.Field) ErrorCauseFinder(com.seleniumtests.core.testanalysis.ErrorCauseFinder) ITestResult(org.testng.ITestResult) ErrorCause(com.seleniumtests.core.testanalysis.ErrorCause) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest)

Example 5 with Field

use of com.seleniumtests.connectors.selenium.fielddetector.Field in project seleniumRobot by bhecquet.

the class TestErrorCauseFinder method testSearchInLastStepAlreadyDone.

@Test(groups = { "ut" })
public void testSearchInLastStepAlreadyDone() throws Exception {
    ITestResult testResult = Reporter.getCurrentTestResult();
    TestNGResultUtils.setSeleniumRobotTestContext(testResult, SeleniumTestsContextManager.getThreadContext());
    SeleniumTestsContextManager.getThreadContext().getTestStepManager().setTestSteps(Arrays.asList(step1, lastStep));
    PowerMockito.whenNew(ImageFieldDetector.class).withArguments(new File(lastStep.getSnapshots().get(0).getScreenshot().getFullImagePath()), (double) 1, FieldType.ERROR_MESSAGES_AND_FIELDS).thenReturn(imageFieldDetector);
    List<Label> labels = new ArrayList<>();
    labels.add(new Label(0, 100, 20, 50, "il y a eu un gros problème"));
    labels.add(new Label(0, 100, 100, 120, "some error"));
    List<Field> fields = new ArrayList<>();
    fields.add(new Field(0, 100, 0, 20, "", "field"));
    fields.add(new Field(0, 100, 200, 220, "", "field"));
    when(imageFieldDetector.detectFields()).thenReturn(fields);
    when(imageFieldDetector.detectLabels()).thenReturn(labels);
    TestNGResultUtils.setErrorCauseSearchedInLastStep(testResult, true);
    List<ErrorCause> causes = new ErrorCauseFinder(testResult).findErrorInLastStepSnapshots();
    Assert.assertEquals(causes.size(), 0);
}
Also used : Field(com.seleniumtests.connectors.selenium.fielddetector.Field) ErrorCauseFinder(com.seleniumtests.core.testanalysis.ErrorCauseFinder) ITestResult(org.testng.ITestResult) ErrorCause(com.seleniumtests.core.testanalysis.ErrorCause) Label(com.seleniumtests.connectors.selenium.fielddetector.Label) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest)

Aggregations

Field (com.seleniumtests.connectors.selenium.fielddetector.Field)35 Test (org.testng.annotations.Test)29 GenericTest (com.seleniumtests.GenericTest)23 Label (com.seleniumtests.connectors.selenium.fielddetector.Label)22 File (java.io.File)17 MockitoTest (com.seleniumtests.MockitoTest)14 JSONObject (kong.unirest.json.JSONObject)14 ArrayList (java.util.ArrayList)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 ImageFieldDetector (com.seleniumtests.connectors.selenium.fielddetector.ImageFieldDetector)9 ErrorCause (com.seleniumtests.core.testanalysis.ErrorCause)8 ErrorCauseFinder (com.seleniumtests.core.testanalysis.ErrorCauseFinder)8 ITestResult (org.testng.ITestResult)8 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)5 Rectangle (java.awt.Rectangle)4 SeleniumRobotServerException (com.seleniumtests.customexception.SeleniumRobotServerException)2 Snapshot (com.seleniumtests.reporter.logger.Snapshot)2 TestStep (com.seleniumtests.reporter.logger.TestStep)2 StepReferenceComparator (com.seleniumtests.util.imaging.StepReferenceComparator)2 BrowserInfo (com.seleniumtests.browserfactory.BrowserInfo)1