Exemplo n.º 1
0
        /// <summary>
        /// Starts the execution of the current test script
        /// </summary>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int StartExecution()
        {
            int result;
            var duration = new Stopwatch();

            duration.Start();

            Log.Enter(this, MethodBase.GetCurrentMethod().Name);
            dataManager.LoadDataForExecution(Environment.CurrentDirectory);

            dataManager.DTMstudioTestData.ReportData.FirmwareInformation.Clone(InfrastructureHandler.GetFimwareInformation(dataManager.DTMstudioTestData.DeviceTypeTestProject.FirmwareInformationAssembly));

            // Configurationsdaten speichern
            dataManager.SaveDeviceTypeProjectData();

            var initialization = false;

            try
            {
                this.executionObject.DtmStudioTestData = dataManager.DTMstudioTestData;
                initialization = TestReportHandler.InitializeExecutionObject(this.executionObject);
                if (initialization)
                {
                    this.executionObject = (new ControlDocumentHandler()).Execute(this.executionObject);
                }
            }
            catch (ImageNotFoundException e)
            {
                Log.Error(this, e.Message);
                this.executionObject.LatestSummaryResult = TestResult.Failed;
                result = -1;
                return(result);
            }
            catch (RanorexException e)
            {
                Log.Error(this, e.Message);
                this.executionObject.LatestSummaryResult = TestResult.Failed;
                result = -1;
                return(result);
            }
            catch (ThreadAbortException e)
            {
                Log.Error(this, e.Message);
                this.executionObject.LatestSummaryResult = TestResult.Failed;
                Thread.ResetAbort();
                result = -1;
                return(result);
            }
            catch (PathTooLongException e)
            {
                Log.Error(this, e.Message);
                System.Windows.Forms.MessageBox.Show(e.Message);
                this.executionObject.LatestSummaryResult = TestResult.Failed;
                result = -1;
                return(result);
            }
            catch (Exception e)
            {
                Log.Error(this, e.Message);
                this.executionObject.LatestSummaryResult = TestResult.Failed;
                result = -1;
                return(result);
            }

            duration.Stop();
            this.executionObject.Duration = duration.Elapsed;

            if (initialization)
            {
                // Get Result Data For Report Data
                dataManager.DTMstudioTestData.ReportData.TotalFailedCount  = TestReportHandler.GetTestCaseInfoErrorCount(this.executionObject);
                dataManager.DTMstudioTestData.ReportData.TotalSuccessCount = TestReportHandler.GetTestCaseInfoSuccessCount(this.executionObject);

                // Write Result Into Report Data
                if (dataManager.DTMstudioTestData.ReportData.TotalFailedCount > 0)
                {
                    this.executionObject.LatestSummaryResult = TestResult.Failed;
                    dataManager.DTMstudioTestData.ReportData.ResultOfTest = TestResult.Failed.ToString();
                }

                if (TestReportHandler.PrepareReporting(this.executionObject, dataManager.DTMstudioTestData.DeviceTypeProject.FDTDeviceTypeName))
                {
                    TestReportHandler.CreateReporting(this.executionObject);
                    TestReportHandler.FinishReporting(this.executionObject.LatestSummaryResult);

                    if (File.Exists(this.executionObject.ReportPathAndFileOverviewTemp))
                    {
                        InfrastructureHandler.InsertReportData(this.executionObject.ReportPathAndFileOverviewTemp, dataManager.DTMstudioTestData);
                        InfrastructureHandler.InsertDuration(this.executionObject.ReportPathAndFileOverviewTemp, this.executionObject.Duration);
                        InfrastructureHandler.ReplaceReportStyleSheet(this.executionObject.ReportPathAndFileOverviewTemp, "EHReportOverview");
                        InfrastructureHandler.CopyCustomizedReportDefaultStyle(Path.Combine(this.executionObject.DtmStudioTestData.DeviceTypeTestProject.ExecutionPath, "Report"), this.executionObject.ReportFolderOverview + @"\Temp", "EHReportOverview");
                        InfrastructureHandler.DeleteRanorexStyle(this.executionObject.ReportFolderOverview + @"\Temp");
                        InfrastructureHandler.ManipulateOverviewReportLayout(this.executionObject);
                        InfrastructureHandler.InsertHTMLCodeToReport(Path.Combine(this.executionObject.ReportPathAndFileOverviewTemp, "EHReportOverview.xsl"), dataManager.DTMstudioTestData.DeviceTypeProject.DeviceFunctions, dataManager.DTMstudioTestData.ReportData.FirmwareInformation.AdditionalInformation);
                    }

                    InfrastructureHandler.CopyReportAndDeleteTempFolder(this.executionObject.ReportFolderOverview + @"\Temp");
                    InfrastructureHandler.ExportTestResult(this.executionObject.ReportFolderOverview, this.executionObject.ReportPathAndFileOverview, dataManager.DTMstudioTestData);
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Error: Test project path [" + this.executionObject.ReportPathAndFileOverview + "] is too long, [" + this.executionObject.ReportPathAndFileOverview.Length + "] characters.");
                }

                this.OpenExplorer(this.executionObject.ReportFolderOverview);

                // return test result as integer
                if (this.executionObject.LatestSummaryResult.Equals(TestResult.Passed))
                {
                    result = 0;
                }
                else
                {
                    result = -1;
                }

                return(result);
            }

            return(-1);
        }
        /// <summary>
        /// Recursive method to read by object.
        /// </summary>
        /// <param name="executionObject">
        /// The execution object.
        /// </param>
        /// <returns>
        /// The <see cref="ExecutionObject"/>.
        /// </returns>
        private ExecutionObject ReadByObject(ExecutionObject executionObject)
        {
            Log.Enter(this, MethodBase.GetCurrentMethod().Name);
            if (executionObject != null)
            {
                var currentExecutionObject = new ExecutionObject(executionObject);

                foreach (var testObject in currentExecutionObject.ExecutionData.TestMethods)
                {
                    if (testObject.GetType() == typeof(TestSuite))
                    {
                        // Add new TestReportInfos with related TestSuiteName
                        var testReportInfo = new TestReportInfo(testObject.DisplayName);
                        currentExecutionObject.TestReportInfos.Add(testReportInfo);
                        currentExecutionObject.CurrentTestReportInfo = testReportInfo;

                        // Recursvive call
                        currentExecutionObject.CurrentTestObject         = testObject;
                        currentExecutionObject.ExecutionData.TestMethods = ((TestCollection)testObject).TestObjects;
                        currentExecutionObject = this.ReadByObject(currentExecutionObject);
                    }
                    else if (testObject.GetType() == typeof(TestFolder) && testObject.Parent.GetType() == typeof(TestSuite))
                    {
                        // Recursvive call
                        currentExecutionObject.CurrentTestObject         = testObject;
                        currentExecutionObject.ExecutionData.TestMethods = ((TestCollection)testObject).TestObjects;
                        currentExecutionObject = this.ReadByObject(currentExecutionObject);
                    }
                    else if (testObject.GetType() == typeof(TestFolder) && testObject.Parent.GetType() != typeof(TestSuite) && testObject.Parent != null)
                    {
                        string targetFolder = currentExecutionObject.ReportFolderTestCases;

                        if (!Directory.Exists(targetFolder))
                        {
                            Directory.CreateDirectory(targetFolder);
                        }

                        // Add new TestReportInfos without related TestSuites
                        var testReportInfo = new TestReportInfo(string.Empty);
                        currentExecutionObject.TestReportInfos.Add(testReportInfo);
                        currentExecutionObject.CurrentTestReportInfo = testReportInfo;

                        // Recursvive call
                        currentExecutionObject.CurrentTestObject         = testObject;
                        currentExecutionObject.ExecutionData.TestMethods = ((TestCollection)testObject).TestObjects;
                        currentExecutionObject = this.ReadByObject(currentExecutionObject);
                    }
                    else if (testObject.GetType() == typeof(TestCase))
                    {
                        this.reportFileName = string.Empty;
                        try
                        {
                            currentExecutionObject.TestCaseName    = testObject.DisplayName;
                            currentExecutionObject.CurrentTestCase = (TestCase)testObject;

                            /* Manipulation der Ausgabe bevor der Bericht abgeschlossen ist, weil die Ausführung
                             * abgebrochen werden können soll, ohne dass dabei die neue Formatierung "vergessen" wird.
                             * Neue Styles ersetzen die Ranorex Styles */
                            this.reportFileName = TestCaseReportHandler.PrepareReporting(ref currentExecutionObject);
                            InfrastructureHandler.ManipulateDetailReportLayout(this.reportFileName);
                            InfrastructureHandler.ReplaceReportStyleSheet(this.reportFileName, "EHReportDetails");
                            string       source   = Path.Combine(currentExecutionObject.DtmStudioTestData.DeviceTypeTestProject.ExecutionPath, "Report");
                            string       target   = currentExecutionObject.ReportFolderTestCases;
                            const string FileName = "EHReportDetails";
                            InfrastructureHandler.CopyCustomizedReportDefaultStyle(source, target, FileName);
                            InfrastructureHandler.DeleteRanorexStyle(currentExecutionObject.ReportFolderTestCases);
                            InfrastructureHandler.InsertHTMLCodeToReport(Path.Combine(currentExecutionObject.ReportFolderTestCases, "EHReportDetails.xsl"), currentExecutionObject.DtmStudioTestData.DeviceTypeProject.DeviceFunctions, currentExecutionObject.DtmStudioTestData.ReportData.FirmwareInformation.AdditionalInformation);

                            // Ende des Kommentars
                            if (testObject.IsActive)
                            {
                                Log.Info(this, "Execute active Testobject -> TestCase");
                                TestObjectHandler.Execute(currentExecutionObject, testObject.Guid);
                            }
                            else
                            {
                                Log.Info(this, "Log inactive Testobject -> TestCase");
                                TestReport.SetCurrentTestResult(TestResult.Skipped);
                            }
                        }
                        catch (Exception exception)
                        {
                            Log.ErrorEx(this, exception, "A Critical Error Occured");
                            Console.WriteLine("A Critical Error Occured: " + exception.Message);
                            TestReport.SetCurrentTestResult(TestResult.Failed);
                        }
                        finally
                        {
                            var testCaseInfo = new TestCaseInfo();
                            TestCaseReportHandler.FinishReporting(ref currentExecutionObject, ref testCaseInfo);
                            currentExecutionObject.DtmStudioTestData.ReportData.ResultOfTest = testCaseInfo.TestCaseResult.ToString();

                            // Nachbearbeitung notwendig, da Ranorex mit jedem Schritt die Styledaten neu hineinkopiert
                            InfrastructureHandler.InsertReportData(testCaseInfo.TestCasePathAndName, currentExecutionObject.DtmStudioTestData);
                            InfrastructureHandler.ManipulateDetailReportLayout(this.reportFileName);
                            InfrastructureHandler.ReplaceReportStyleSheet(this.reportFileName, "EHReportDetails");
                            InfrastructureHandler.DeleteRanorexStyle(currentExecutionObject.ReportFolderTestCases);
                            InfrastructureHandler.InsertHTMLCodeToReport(Path.Combine(currentExecutionObject.ReportFolderTestCases, "EHReportDetails.xsl"), currentExecutionObject.DtmStudioTestData.DeviceTypeProject.DeviceFunctions, currentExecutionObject.DtmStudioTestData.ReportData.FirmwareInformation.AdditionalInformation);

                            // Ende des Kommentars

                            // Datenbank Aufruf
                            InfrastructureHandler.ExportTestResult(testCaseInfo.TestCasePathAndName, currentExecutionObject.TestCaseName, currentExecutionObject.DtmStudioTestData);
                        }
                    }
                    else
                    {
                        Log.Info(this, "Unhandled Type: " + typeof(TestObject));
                        Console.WriteLine("Unhandled Type:" + typeof(TestObject));
                    }
                }

                return(currentExecutionObject);
            }

            Log.Error(this, "ExecutionObject is Null before execution");
            Console.WriteLine("ExecutionObject is Null before execution");
            return(null);
        }