Search in sources :

Example 1 with ScreenshotUploadTask

use of com.testsigma.automator.utilities.ScreenshotUploadTask in project testsigma by testsigmahq.

the class TestcaseRunner method run.

public TestCaseResult run() throws Exception {
    resetThreadContextData();
    populateThreadContextData();
    log.info("Running testcase - " + testCaseEntity.getTestCaseName());
    if (testDeviceEntity.getCreateSessionAtCaseLevel()) {
        startSession();
    } else {
        restartCurrentSession();
    }
    environmentRunResult.setSessionCreatedOn(new Timestamp(System.currentTimeMillis()));
    ResultConstant result = ResultConstant.SUCCESS;
    List<TestCaseStepEntity> stepList = testCaseEntity.getTestSteps();
    List<TestCaseStepResult> testCaseStepsResult = new ArrayList<>();
    testCaseResult.setTestCaseStepResults(testCaseStepsResult);
    testCaseResult.setIsStepGroup(testCaseEntity.getIsStepGroup());
    testCaseResult.setDataDriven(testCaseEntity.getIsDataDriven());
    testCaseResult.setTestPlanResultId(testDeviceEntity.getExecutionRunId());
    testCaseResult.setTestCaseName(testCaseEntity.getTestCaseName());
    try {
        HashMap<Long, TestCaseStepResult> parentStatus = new HashMap<>();
        int currentIndex = 0;
        testCaseResult.setCurrentIndex(currentIndex);
        int lastStep = stepList.size() - 1;
        ScreenCaptureUtil screenCaptureUtil = new ScreenCaptureUtil(new ArrayList<>());
        for (int i = 0; i < stepList.size(); i++) {
            TestCaseStepEntity testCaseStepEntity = stepList.get(i);
            TestCaseStepResult testCaseStepResult = new TestCaseStepResult();
            testCaseStepResult.setTestCaseStepId(testCaseStepEntity.getId());
            testCaseStepResult.setSkipExe(skipExecution);
            testCaseStepResult.setSkipMessage(resultFailureMessage);
            /*  if (!skipExecution && (workspaceType.equals(WorkspaceType.WebApplication) || workspaceType
          .equals(WorkspaceType.MobileWeb))) {
          url = getUrl();
        }*/
            boolean skipGetUrl = false;
            if (testCaseStepEntity.getAction() != null) {
                skipGetUrl = Arrays.asList(SKIP_GETURL).stream().filter(action -> testCaseStepEntity.getAction().contains(action)).count() > 0;
            }
            TestStepType type = testCaseStepEntity.getType();
            TestcaseStepRunner testcaseStepRunner = new TestcaseStepRunnerFactory().getRunner(this.workspaceType, testDeviceSettings.getOs(), type);
            TestCaseStepResult parentResult = parentStatus.get(testCaseStepEntity.getParentId());
            RunnerUtil util = new RunnerUtil();
            boolean isFailure = util.canSkipNormalStep(parentResult, testCaseStepEntity, testCaseStepResult) || util.nestedConditionalStep(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipIfElse(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipIfElseIf(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipElseIfElseIf(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipElseIfElse(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipIfCondition(parentResult, testCaseStepEntity, testCaseStepResult) || util.canSkipForLoop(parentResult, testCaseStepEntity, testCaseStepResult);
            log.info(new ObjectMapperService().convertToJson(testCaseStepEntity));
            int stepResultUpdateSize = 10;
            if (!skipExecution && isFailure) {
                parentStatus.put(testCaseStepEntity.getId(), testCaseStepResult);
                testCaseStepResult.setResult(ResultConstant.NOT_EXECUTED);
                int processedSteps = processedStepCount(testCaseStepsResult);
                if (processedSteps % stepResultUpdateSize == 0) {
                    testCaseResult.setTestCaseStepResults(testCaseStepsResult);
                    postTestcaseResult();
                    currentIndex = +stepResultUpdateSize;
                    processedSteps = 0;
                    testCaseStepsResult = new ArrayList<>();
                    testCaseResult.setCurrentIndex(currentIndex);
                }
                if (screenCaptureUtil.screenshots.size() > 0 && (lastStep == i)) {
                    log.debug("Screenshots task for :::" + testCaseStepResult);
                    final String requestId = ThreadContext.get("X-Request-Id");
                    UploadThreadPool.getInstance().upload(new ScreenshotUploadTask(screenCaptureUtil.screenshots, requestId, testDeviceEntity));
                    screenCaptureUtil.screenshots = new ArrayList<>();
                }
                testCaseStepResult = testcaseStepRunner.run(testCaseStepEntity, testCaseStepResult, mapStepResult, testCaseResult, parentStatus, false, false, screenCaptureUtil);
                testCaseStepsResult.add(testCaseStepResult);
                mapStepResult.put(testCaseStepEntity.getId(), testCaseStepResult);
                continue;
            }
            testCaseStepResult = testcaseStepRunner.run(testCaseStepEntity, testCaseStepResult, mapStepResult, testCaseResult, parentStatus, false, false, screenCaptureUtil);
            if (screenCaptureUtil.screenshots.size() == SAVE_BATCH_IMAGES || (lastStep == i)) {
                final String requestId = ThreadContext.get("X-Request-Id");
                UploadThreadPool.getInstance().upload(new ScreenshotUploadTask(screenCaptureUtil.screenshots, requestId, testDeviceEntity));
                screenCaptureUtil.screenshots = new ArrayList<>();
            }
            skipExecution = testCaseStepResult.getSkipExe();
            resultFailureMessage = testCaseStepResult.getSkipMessage();
            testCaseStepsResult.add(testCaseStepResult);
            mapStepResult.put(testCaseStepEntity.getId(), testCaseStepResult);
            TestCaseStepEntity stepGroup = null;
            TestCaseStepResult stepGroupResult = null;
            if (testCaseStepEntity.getStepGroupId() != null) {
                stepGroup = ((testCaseStepEntity.getTestCaseSteps() != null) && (testCaseStepEntity.getTestCaseSteps().size() > 0)) ? testCaseStepEntity.getTestCaseSteps().get(0) : null;
                stepGroupResult = ((testCaseStepResult.getStepResults() != null) && (testCaseStepResult.getStepResults().size() > 0)) ? testCaseStepResult.getStepResults().get(0) : null;
            }
            if (i == 0 && ((workspaceType == WorkspaceType.WebApplication) || (workspaceType == WorkspaceType.MobileWeb)) && (getUrl() != null) && !skipGetUrl && ((testCaseStepEntity.getStepGroupId() != null && stepGroup != null && (stepGroup.getStepGroupId() != null && !stepGroup.getAction().contains(AutomatorMessages.KEYWORD_GO_TO) && stepGroupResult.getResult() == ResultConstant.SUCCESS)) || (testCaseStepEntity.getAction() != null && !testCaseStepEntity.getAction().contains(AutomatorMessages.KEYWORD_GO_TO) && testCaseStepResult.getResult() == ResultConstant.SUCCESS))) {
            } else if (i == (stepList.size() - 1) && !skipGetUrl && testCaseStepResult.getResult() == ResultConstant.SUCCESS && (workspaceType.equals(WorkspaceType.WebApplication) || workspaceType.equals(WorkspaceType.MobileWeb))) {
                ExecutionEnvironmentRunner.addUrl(testPlanId, testCaseEntity.getId(), getUrl());
            }
            // TODO:use check based step type
            if (testCaseStepEntity.getConditionType() == null || testCaseStepEntity.getConditionType() == ConditionType.NOT_USED || ConditionType.LOOP_FOR == testCaseStepEntity.getConditionType()) {
                result = (result.getId() < testCaseStepResult.getResult().getId()) ? testCaseStepResult.getResult() : result;
            }
            int processedSteps = processedStepCount(testCaseStepsResult);
            if (processedSteps % stepResultUpdateSize == 0) {
                testCaseResult.setTestCaseStepResults(testCaseStepsResult);
                postTestcaseResult();
                currentIndex = +stepResultUpdateSize;
                processedSteps = 0;
                testCaseStepsResult = new ArrayList<>();
                testCaseResult.setCurrentIndex(currentIndex);
            }
        }
        testCaseResult.setTestCaseStepResults(testCaseStepsResult);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    testCaseResult.setResult(ObjectUtils.defaultIfNull(testCaseResult.getResult(), result));
    testCaseResult.setEndTime(new Timestamp(System.currentTimeMillis()));
    if ((testCaseResult.getResult() == ResultConstant.SUCCESS) && (result == ResultConstant.SUCCESS)) {
        testCaseResult.setMessage(AutomatorMessages.MSG_TEST_CASE_SUCCESS);
    } else if (StringUtils.isBlank(testCaseResult.getMessage())) {
        testCaseResult.setMessage(AutomatorMessages.MSG_TEST_CASE_FAILURE);
    }
    // check for browser closed condition
    if (testCaseResult.getResult() != ResultConstant.SUCCESS) {
        if (ErrorCodes.BROWSER_CLOSED.equals(testCaseResult.getErrorCode())) {
            DriverManager.getDriverManager().setRestartDriverSession(Boolean.TRUE);
        }
    }
    if (testDeviceEntity.getCreateSessionAtCaseLevel()) {
        endSession();
    }
    resetThreadContextData();
    return testCaseResult;
}
Also used : DriverSessionType(com.testsigma.automator.constants.DriverSessionType) AutomatorException(com.testsigma.automator.exceptions.AutomatorException) ThreadContext(org.apache.logging.log4j.ThreadContext) java.util(java.util) Timestamp(java.sql.Timestamp) AutomatorMessages(com.testsigma.automator.constants.AutomatorMessages) ScreenshotUploadTask(com.testsigma.automator.utilities.ScreenshotUploadTask) ScreenCaptureUtil(com.testsigma.automator.utilities.ScreenCaptureUtil) StringUtils(org.apache.commons.lang3.StringUtils) ObjectMapperService(com.testsigma.automator.service.ObjectMapperService) UploadThreadPool(com.testsigma.automator.utilities.UploadThreadPool) ObjectUtils(org.apache.commons.lang3.ObjectUtils) com.testsigma.automator.entity(com.testsigma.automator.entity) Log4j2(lombok.extern.log4j.Log4j2) AutomatorConfig(com.testsigma.automator.AutomatorConfig) ErrorCodes(com.testsigma.automator.constants.ErrorCodes) DriverManager(com.testsigma.automator.drivers.DriverManager) ScreenCaptureUtil(com.testsigma.automator.utilities.ScreenCaptureUtil) ObjectMapperService(com.testsigma.automator.service.ObjectMapperService) ScreenshotUploadTask(com.testsigma.automator.utilities.ScreenshotUploadTask) Timestamp(java.sql.Timestamp) AutomatorException(com.testsigma.automator.exceptions.AutomatorException)

Aggregations

AutomatorConfig (com.testsigma.automator.AutomatorConfig)1 AutomatorMessages (com.testsigma.automator.constants.AutomatorMessages)1 DriverSessionType (com.testsigma.automator.constants.DriverSessionType)1 ErrorCodes (com.testsigma.automator.constants.ErrorCodes)1 DriverManager (com.testsigma.automator.drivers.DriverManager)1 com.testsigma.automator.entity (com.testsigma.automator.entity)1 AutomatorException (com.testsigma.automator.exceptions.AutomatorException)1 ObjectMapperService (com.testsigma.automator.service.ObjectMapperService)1 ScreenCaptureUtil (com.testsigma.automator.utilities.ScreenCaptureUtil)1 ScreenshotUploadTask (com.testsigma.automator.utilities.ScreenshotUploadTask)1 UploadThreadPool (com.testsigma.automator.utilities.UploadThreadPool)1 Timestamp (java.sql.Timestamp)1 java.util (java.util)1 Log4j2 (lombok.extern.log4j.Log4j2)1 ObjectUtils (org.apache.commons.lang3.ObjectUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ThreadContext (org.apache.logging.log4j.ThreadContext)1