Search in sources :

Example 51 with SeleniumRobotSnapshotServerConnector

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

the class TestPerformanceReporter method testSnapshotComparisonKoAddTestResult.

/**
 * Check that when snapshot server is used with behavior "addTestResult" 2 results should be presented: one with the result of selenium test, a second one with the result of snapshot comparison.
 * Both are the same but second test is there for integration with junit parser so that we can differentiate navigation result from GUI result.
 * @throws Exception
 */
@Test(groups = { "it" })
public void testSnapshotComparisonKoAddTestResult() throws Exception {
    try {
        System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE, "true");
        System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_COMPARE_SNAPSHOT, "true");
        System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_COMPARE_SNAPSHOT_BEHAVIOUR, "addTestResult");
        System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_RECORD_RESULTS, "true");
        System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL, "http://localhost:4321");
        SeleniumRobotSnapshotServerConnector server = configureMockedSnapshotServerConnection();
        createServerMock("GET", SeleniumRobotSnapshotServerConnector.TESTCASEINSESSION_API_URL + "15", 200, "{'testSteps': [], 'computed': true, 'isOkWithSnapshots': false}");
        SeleniumTestsContextManager.removeThreadContext();
        executeSubTest(1, new String[] { "com.seleniumtests.it.stubclasses.StubTestClass" }, ParallelMode.METHODS, new String[] { "testAndSubActions" });
        // test both files are available
        String detailedReportContent1 = readTestMethodPerfFile("snapshots-testAndSubActions");
        Assert.assertTrue(detailedReportContent1.contains("<testcase classname=\"com.seleniumtests.it.stubclasses.StubTestClass\" name=\"Step 8: Snapshot comparison\" time=\"0.0\">"));
        // this file is not re-generated with "snapshot comparison" step, but this not a problem. Important fact is that both files are present
        String detailedReportContent2 = readTestMethodPerfFile("testAndSubActions");
        Assert.assertTrue(detailedReportContent2.contains("<testcase classname=\"com.seleniumtests.it.stubclasses.StubTestClass\" name=\"Step 6: Test end\""));
    } finally {
        System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE);
        System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL);
        System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_COMPARE_SNAPSHOT);
        System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_COMPARE_SNAPSHOT_BEHAVIOUR);
        System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_RECORD_RESULTS);
    }
}
Also used : SeleniumRobotSnapshotServerConnector(com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector) Test(org.testng.annotations.Test)

Example 52 with SeleniumRobotSnapshotServerConnector

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

the class ReporterControler method changeTestResultsWithSnapshotComparison.

/**
 * If snapshot comparison has been enabled, request snapshot server for each test result to know if comparison was successful
 * /!\ This method is aimed to be called only once all test suites have been completed
 * @param suites	test suites
 */
private void changeTestResultsWithSnapshotComparison(List<ISuite> suites) {
    if (!(SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerActive() && SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerCompareSnapshot())) {
        return;
    }
    SeleniumRobotSnapshotServerConnector snapshotServer = SeleniumRobotSnapshotServerConnector.getInstance();
    for (ISuite suite : suites) {
        for (String suiteString : suite.getResults().keySet()) {
            ISuiteResult suiteResult = suite.getResults().get(suiteString);
            Set<ITestResult> resultSet = new HashSet<>();
            resultSet.addAll(suiteResult.getTestContext().getFailedTests().getAllResults());
            resultSet.addAll(suiteResult.getTestContext().getPassedTests().getAllResults());
            resultSet.addAll(suiteResult.getTestContext().getSkippedTests().getAllResults());
            for (ITestResult testResult : resultSet) {
                // check if we have an id from snapshot server
                Integer testCaseInSessionId = TestNGResultUtils.getSnapshotTestCaseInSessionId(testResult);
                if (testCaseInSessionId == null) {
                    continue;
                }
                StringBuilder errorMessage = new StringBuilder();
                int snapshotComparisonResult = snapshotServer.getTestCaseInSessionComparisonResult(testCaseInSessionId, errorMessage);
                // update snapshot comparison result of the run test.
                TestNGResultUtils.setSnapshotComparisonResult(testResult, snapshotComparisonResult);
                // create a step for snapshot comparison
                createTestStepForComparisonResult(testResult, snapshotComparisonResult, errorMessage.toString());
                changeTestResultWithSnapshotComparison(suiteResult, testResult, snapshotComparisonResult);
            }
        }
    }
}
Also used : ITestResult(org.testng.ITestResult) ISuite(org.testng.ISuite) SeleniumRobotSnapshotServerConnector(com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector) ISuiteResult(org.testng.ISuiteResult) HashSet(java.util.HashSet)

Example 53 with SeleniumRobotSnapshotServerConnector

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

the class SeleniumRobotServerTestRecorder method generateReport.

@Override
protected void generateReport(Map<ITestContext, Set<ITestResult>> resultSet, String outdir, boolean optimizeReport, boolean finalGeneration) {
    ITestContext testCtx = SeleniumTestsContextManager.getGlobalContext().getTestNGContext();
    if (testCtx == null) {
        logger.error("Looks like your class does not extend from SeleniumTestPlan!");
        return;
    }
    // issue #81: use global context because these parameters are known from there (thread context is too narrow)
    if (!Boolean.TRUE.equals(SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerActive()) || !SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerRecordResults() && !SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerCompareSnapshot()) {
        return;
    }
    // check that seleniumRobot server is alive
    SeleniumRobotSnapshotServerConnector serverConnector = getServerConnector();
    if (!serverConnector.getActive()) {
        logger.info("selenium-robot-server not found or down");
        return;
    } else {
        try {
            // create session only if it has not been before
            for (ITestContext testContext : resultSet.keySet()) {
                recordTestSession(testContext);
            }
        } catch (SeleniumRobotServerException | ConfigurationException e) {
            logger.error("Error contacting selenium robot server", e);
            return;
        }
    }
    try {
        recordResults(serverConnector, resultSet);
    } catch (SeleniumRobotServerException | ConfigurationException e) {
        logger.error("Error recording result on selenium robot server", e);
    }
}
Also used : ConfigurationException(com.seleniumtests.customexception.ConfigurationException) ITestContext(org.testng.ITestContext) SeleniumRobotSnapshotServerConnector(com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Example 54 with SeleniumRobotSnapshotServerConnector

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

the class TestNGResultUtils method changeTestResultWithSnapshotComparison.

/**
 * Change the test result when snapshot comparison fails
 * @param testResult
 */
public static void changeTestResultWithSnapshotComparison(final ITestResult testResult) {
    if (// test is already failed
    testResult.getStatus() == ITestResult.FAILURE || !Boolean.TRUE.equals(SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerActive()) || // as the comparison result is only displayed, do not retry
    SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerCompareSnapshotBehaviour() == SnapshotComparisonBehaviour.DISPLAY_ONLY || // complicated to set the test failed, and then success again
    SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerCompareSnapshotBehaviour() == SnapshotComparisonBehaviour.ADD_TEST_RESULT || !SeleniumTestsContextManager.getGlobalContext().getSeleniumRobotServerCompareSnapshot()) {
        return;
    }
    SeleniumRobotSnapshotServerConnector serverConnector = SeleniumRobotSnapshotServerConnector.getInstance();
    List<TestStep> testSteps = getSeleniumRobotTestContext(testResult).getTestStepManager().getTestSteps();
    if (testSteps == null) {
        return;
    }
    for (TestStep testStep : testSteps) {
        for (Snapshot snapshot : new ArrayList<>(testStep.getSnapshots())) {
            if (snapshot.getCheckSnapshot().recordSnapshotOnServerForComparison()) {
                if (snapshot.getName() == null || snapshot.getName().isEmpty()) {
                    logger.warn("Snapshot hasn't any name, it won't be sent to server");
                    continue;
                }
                try {
                    SnapshotComparisonResult comparisonResult = serverConnector.checkSnapshotHasNoDifferences(snapshot, CommonReporter.getTestCaseName(testResult), testStep.getName());
                    if (comparisonResult == SnapshotComparisonResult.KO) {
                        testResult.setStatus(ITestResult.FAILURE);
                        testResult.setThrowable(new ScenarioException("Snapshot comparison failed"));
                        // move test from passedTests to failedTests if test is not already in failed tests
                        if (testResult.getTestContext().getPassedTests().getAllMethods().contains(testResult.getMethod())) {
                            testResult.getTestContext().getPassedTests().removeResult(testResult);
                            testResult.getTestContext().getFailedTests().addResult(testResult, testResult.getMethod());
                        }
                        return;
                    }
                } catch (SeleniumRobotServerException e) {
                    logger.error("Could not create snapshot on server", e);
                }
            }
        }
    }
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ArrayList(java.util.ArrayList) SeleniumRobotSnapshotServerConnector(com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector) ScenarioException(com.seleniumtests.customexception.ScenarioException) SnapshotComparisonResult(com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector.SnapshotComparisonResult) SeleniumRobotServerException(com.seleniumtests.customexception.SeleniumRobotServerException)

Example 55 with SeleniumRobotSnapshotServerConnector

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

the class ConnectorsTest method configureMockedSnapshotServerConnection.

/**
 * simulate an alive snapshot sever responding to all requests
 * @throws UnirestException
 */
protected SeleniumRobotSnapshotServerConnector configureMockedSnapshotServerConnection() throws UnirestException {
    // snapshot server comes with variable server
    configureMockedVariableServerConnection();
    when(getAliveRequest.asString()).thenReturn(responseAliveString);
    when(responseAliveString.getStatus()).thenReturn(200);
    when(Unirest.get(SERVER_URL + "/snapshot/")).thenReturn(getAliveRequest);
    when(unirestInstance.get(SERVER_URL + "/snapshot/")).thenReturn(getAliveRequest);
    SeleniumTestsContextManager.getThreadContext().setSeleniumRobotServerUrl(SERVER_URL);
    SeleniumTestsContextManager.getThreadContext().setSeleniumRobotServerActive(true);
    // set default reply from server. To override this behaviour, redefine some steps in test after connector creation
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.APPLICATION_API_URL, 200, "{'id': '9'}");
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.ENVIRONMENT_API_URL, 200, "{'id': '10'}");
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.VERSION_API_URL, 200, "{'id': '11'}");
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.TESTCASE_API_URL, 200, "{'id': '12'}");
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.TESTCASEINSESSION_API_URL, 200, "{'id': '15'}");
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.SNAPSHOT_API_URL, 200, "{'id': '16', 'computed': true, 'computingError': '', 'diffPixelPercentage': 0.0, 'tooManyDiffs': false}");
    createServerMock("PUT", SeleniumRobotSnapshotServerConnector.SNAPSHOT_API_URL, 200, "{'id': '16', 'computed': true, 'computingError': '', 'diffPixelPercentage': 0.0, 'tooManyDiffs': false}");
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.EXCLUDE_API_URL, 200, "{'id': '18'}");
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.STEPRESULT_API_URL, 200, "{'id': '17'}");
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.SESSION_API_URL, 200, "{'id': '13'}");
    createServerMock("PATCH", SeleniumRobotSnapshotServerConnector.SESSION_API_URL + "13/", 200, "{\"id\":13,\"sessionId\":\"4b2e32f4-69dc-4f05-9644-4287acc2c9ac\",\"date\":\"2017-07-24\",\"browser\":\"*none\",\"environment\":\"DEV\",\"version\":2}");
    createServerMock("GET", SeleniumRobotSnapshotServerConnector.TESTCASEINSESSION_API_URL + "15", 200, "{'testSteps': [], 'computed': true, 'isOkWithSnapshots': true}");
    createServerMock("PATCH", SeleniumRobotSnapshotServerConnector.TESTCASEINSESSION_API_URL + "15/", 200, "{\"id\":12,\"name\":\"Test 1\",\"version\":11,\"testSteps\":[14]}");
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.TESTSTEP_API_URL, 200, "{'id': '14'}");
    createServerMock("GET", SeleniumRobotServerConnector.NAMED_APPLICATION_API_URL, 200, "{'id': 9}");
    createServerMock("GET", SeleniumRobotServerConnector.NAMED_ENVIRONMENT_API_URL, 200, "{'id': 10}");
    createServerMock("GET", SeleniumRobotServerConnector.NAMED_TESTCASE_API_URL, 200, "{'id': 12}");
    createServerMock("GET", SeleniumRobotServerConnector.NAMED_VERSION_API_URL, 200, "{'id': 11}");
    // upload reference image for step
    createServerMock("POST", SeleniumRobotSnapshotServerConnector.STEP_REFERENCE_API_URL, 200, "{'result': 'OK'}");
    // get reference image
    createServerMock("GET", SeleniumRobotSnapshotServerConnector.STEP_REFERENCE_API_URL + "17/", 200, Paths.get(SeleniumTestsContextManager.getApplicationDataPath(), "images", "googleSearch.png").toFile());
    SeleniumRobotSnapshotServerConnector connector = new SeleniumRobotSnapshotServerConnector(true, SERVER_URL);
    // reset default value to force creation
    connector.setVersionId(null);
    return connector;
}
Also used : SeleniumRobotSnapshotServerConnector(com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector)

Aggregations

SeleniumRobotSnapshotServerConnector (com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector)113 Test (org.testng.annotations.Test)106 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)86 ConnectorsTest (com.seleniumtests.ConnectorsTest)84 HttpRequest (kong.unirest.HttpRequest)15 SnapshotComparisonResult (com.seleniumtests.connectors.selenium.SeleniumRobotSnapshotServerConnector.SnapshotComparisonResult)6 File (java.io.File)6 Rectangle (org.openqa.selenium.Rectangle)5 MultipartBody (kong.unirest.MultipartBody)4 SeleniumRobotServerException (com.seleniumtests.customexception.SeleniumRobotServerException)3 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)2 ReporterTest (com.seleniumtests.it.reporter.ReporterTest)2 StubTestClassForDriverParallelTest (com.seleniumtests.it.stubclasses.StubTestClassForDriverParallelTest)2 Snapshot (com.seleniumtests.reporter.logger.Snapshot)2 TestStep (com.seleniumtests.reporter.logger.TestStep)2 ArrayList (java.util.ArrayList)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 ITestContext (org.testng.ITestContext)2 ITestResult (org.testng.ITestResult)2 XmlTest (org.testng.xml.XmlTest)2