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;
}
Aggregations