private void ExecuteScript(bool dataIsFile) { try { _currentStatus = EngineStatus.Running; //create stopwatch for metrics tracking _stopWatch = new Stopwatch(); _stopWatch.Start(); //log starting ReportProgress("Bot Engine Started: " + DateTime.Now.ToString()); //get automation script Script automationScript; if (dataIsFile) { ReportProgress("Deserializing File"); Log.Information("Script Path: " + AutomationEngineContext.FilePath); FileName = AutomationEngineContext.FilePath; automationScript = Script.DeserializeFile(AutomationEngineContext); } else { ReportProgress("Deserializing JSON"); automationScript = Script.DeserializeJsonString(AutomationEngineContext.FilePath); } ReportProgress("Creating Variable List"); //set variables if they were passed in if (AutomationEngineContext.Variables != null) { foreach (var var in AutomationEngineContext.Variables) { var variableFound = automationScript.Variables.Where(f => f.VariableName == var.VariableName).FirstOrDefault(); if (variableFound != null) { variableFound.VariableValue = var.VariableValue; } } } AutomationEngineContext.Variables = automationScript.Variables; //update ProjectPath variable var projectPathVariable = AutomationEngineContext.Variables.Where(v => v.VariableName == "ProjectPath").SingleOrDefault(); if (projectPathVariable != null) { projectPathVariable.VariableValue = AutomationEngineContext.ProjectPath; } else { projectPathVariable = new ScriptVariable { VariableName = "ProjectPath", VariableType = typeof(string), VariableValue = AutomationEngineContext.ProjectPath }; AutomationEngineContext.Variables.Add(projectPathVariable); } ReportProgress("Creating Argument List"); //set arguments if they were passed in if (AutomationEngineContext.Arguments != null) { foreach (var arg in AutomationEngineContext.Arguments) { var argumentFound = automationScript.Arguments.Where(f => f.ArgumentName == arg.ArgumentName).FirstOrDefault(); if (argumentFound != null) { argumentFound.ArgumentValue = arg.ArgumentValue; } } } AutomationEngineContext.Arguments = automationScript.Arguments; ReportProgress("Creating Element List"); //set elements if they were passed in if (AutomationEngineContext.Elements != null) { foreach (var elem in AutomationEngineContext.Elements) { var elementFound = automationScript.Elements.Where(f => f.ElementName == elem.ElementName).FirstOrDefault(); if (elementFound != null) { elementFound.ElementValue = elem.ElementValue; } } } AutomationEngineContext.Elements = automationScript.Elements; ReportProgress("Creating App Instance Tracking List"); //create app instances and merge in global instances if (AutomationEngineContext.AppInstances == null) { AutomationEngineContext.AppInstances = new Dictionary <string, object>(); } var GlobalInstances = GlobalAppInstances.GetInstances(); foreach (var instance in GlobalInstances) { AutomationEngineContext.AppInstances[instance.Key] = instance.Value; } //execute commands ScriptAction startCommand = automationScript.Commands.Where(x => x.ScriptCommand.LineNumber <= AutomationEngineContext.StartFromLineNumber) .Last(); int startCommandIndex = automationScript.Commands.FindIndex(x => x.ScriptCommand.LineNumber == startCommand.ScriptCommand.LineNumber); while (startCommandIndex < automationScript.Commands.Count) { if (IsCancellationPending) { ReportProgress("Cancelling Script"); ScriptFinished(ScriptFinishedResult.Cancelled); return; } ExecuteCommand(automationScript.Commands[startCommandIndex]); startCommandIndex++; } if (IsCancellationPending) { //mark cancelled - handles when cancelling and user defines 1 parent command or else it will show successful ScriptFinished(ScriptFinishedResult.Cancelled); } else { //mark finished ScriptFinished(ScriptFinishedResult.Successful); } } catch (Exception ex) { ScriptFinished(ScriptFinishedResult.Error, ex.ToString()); } if ((AutomationEngineContext.ScriptEngine != null && !AutomationEngineContext.ScriptEngine.IsChildEngine) || (IsServerExecution && !IsServerChildExecution)) { AutomationEngineContext.EngineLogger.Dispose(); } }
public void ExecuteScript(string data, bool dataIsFile) { Core.Client.EngineBusy = true; try { CurrentStatus = EngineStatus.Running; //create stopwatch for metrics tracking sw = new System.Diagnostics.Stopwatch(); sw.Start(); //log starting ReportProgress("Bot Engine Started: " + DateTime.Now.ToString()); //get automation script Core.Script.Script automationScript; if (dataIsFile) { ReportProgress("Deserializing File"); engineLogger.Information("Script Path: " + data); FileName = data; automationScript = Core.Script.Script.DeserializeFile(data); } else { ReportProgress("Deserializing XML"); automationScript = Core.Script.Script.DeserializeXML(data); } if (serverSettings.ServerConnectionEnabled && taskModel == null) { taskModel = HttpServerClient.AddTask(data); } else if (serverSettings.ServerConnectionEnabled && taskModel != null) { taskModel = HttpServerClient.UpdateTask(taskModel.TaskID, "Running", "Running Server Assignment"); } //track variables and app instances ReportProgress("Creating Variable List"); //set variables if they were passed in if (VariableList != null) { foreach (var var in VariableList) { var variableFound = automationScript.Variables.Where(f => f.VariableName == var.VariableName).FirstOrDefault(); if (variableFound != null) { variableFound.VariableValue = var.VariableValue; } } } VariableList = automationScript.Variables; ReportProgress("Creating App Instance Tracking List"); //create app instances and merge in global instances this.AppInstances = new Dictionary <string, object>(); var GlobalInstances = GlobalAppInstances.GetInstances(); foreach (var instance in GlobalInstances) { this.AppInstances.Add(instance.Key, instance.Value); } //execute commands foreach (var executionCommand in automationScript.Commands) { if (IsCancellationPending) { ReportProgress("Cancelling Script"); ScriptFinished(ScriptFinishedEventArgs.ScriptFinishedResult.Cancelled); return; } ExecuteCommand(executionCommand); } if (IsCancellationPending) { //mark cancelled - handles when cancelling and user defines 1 parent command or else it will show successful ScriptFinished(ScriptFinishedEventArgs.ScriptFinishedResult.Cancelled); } else { //mark finished ScriptFinished(ScriptFinishedEventArgs.ScriptFinishedResult.Successful); } } catch (Exception ex) { ScriptFinished(ScriptFinishedEventArgs.ScriptFinishedResult.Error, ex.ToString()); } }
private void ExecuteScript(string data, bool dataIsFile, string projectPath) { try { _currentStatus = EngineStatus.Running; //create stopwatch for metrics tracking _stopWatch = new Stopwatch(); _stopWatch.Start(); //log starting ReportProgress("Bot Engine Started: " + DateTime.Now.ToString()); //get automation script Script automationScript; if (dataIsFile) { ReportProgress("Deserializing File"); Log.Information("Script Path: " + data); FileName = data; automationScript = Script.DeserializeFile(data); } else { ReportProgress("Deserializing JSON"); automationScript = Script.DeserializeJsonString(data); } //track variables and app instances ReportProgress("Creating Variable List"); //set variables if they were passed in if (VariableList != null) { foreach (var var in VariableList) { var variableFound = automationScript.Variables.Where(f => f.VariableName == var.VariableName).FirstOrDefault(); if (variableFound != null) { variableFound.VariableValue = var.VariableValue; } } } VariableList = automationScript.Variables; //update ProjectPath variable var projectPathVariable = VariableList.Where(v => v.VariableName == "ProjectPath").SingleOrDefault(); if (projectPathVariable != null) { projectPathVariable.VariableValue = projectPath; } else { projectPathVariable = new ScriptVariable { VariableName = "ProjectPath", VariableValue = projectPath }; VariableList.Add(projectPathVariable); } //track elements ReportProgress("Creating Element List"); //set elements if they were passed in if (ElementList != null) { foreach (var elem in ElementList) { var elementFound = automationScript.Elements.Where(f => f.ElementName == elem.ElementName).FirstOrDefault(); if (elementFound != null) { elementFound.ElementValue = elem.ElementValue; } } } ElementList = automationScript.Elements; ReportProgress("Creating App Instance Tracking List"); //create app instances and merge in global instances AppInstances = new Dictionary <string, object>(); var GlobalInstances = GlobalAppInstances.GetInstances(); foreach (var instance in GlobalInstances) { AppInstances.Add(instance.Key, instance.Value); } //execute commands foreach (var executionCommand in automationScript.Commands) { if (IsCancellationPending) { ReportProgress("Cancelling Script"); ScriptFinished(ScriptFinishedResult.Cancelled); return; } ExecuteCommand(executionCommand); } if (IsCancellationPending) { //mark cancelled - handles when cancelling and user defines 1 parent command or else it will show successful ScriptFinished(ScriptFinishedResult.Cancelled); } else { //mark finished ScriptFinished(ScriptFinishedResult.Successful); } } catch (Exception ex) { ScriptFinished(ScriptFinishedResult.Error, ex.ToString()); } }