Exemplo n.º 1
0
        public void Run(string strCallerId,
                        TestConfig testConfig,
                        IEnumerable <string> testCaseGroup,
                        string strExecutionLocation,
                        string strResultFile,
                        bool bRunningFailedTestCase = false,
                        int iNumberOfTimesRunningFailedTestCases = -1)
        {
            if (testCaseGroup == null || testCaseGroup.Count() <= 0)
            {
                this.Agent.LogUtil.LogWarning("Empty test case list found in test runner...");
                return;
            }

            ///////////////////////////
            if ((new Random().Next(1, 21) % 21) == 0)
            {
                if (!(new LicenseChecker()).DoesThisMachineHaveAValidLicense())
                {
                    return;
                }
            }
            ///////////////////////////

            List <ResourceSection> lstResourcesAllocated;
            var arrTestRunnerPath = testConfig.GetExecutableAlongWithCommandLineParametersOfAGivenProperty(ConfigProperty.TestRunner,
                                                                                                           testConfig.ExecutionLocation,
                                                                                                           this.Agent,
                                                                                                           out lstResourcesAllocated);
            ProcessStartInfo psiTestRunner = new ProcessStartInfo();

            psiTestRunner.FileName = "cmd.exe";

            if (testConfig.MakeTestRunnerAsChildProcessOfPTR.ToBool())
            {
                psiTestRunner.RedirectStandardInput = true;
                psiTestRunner.UseShellExecute       = false;
                psiTestRunner.CreateNoWindow        = true;
            }

            Process processTestRunner = null;
            object  objLocker         = testConfig;

            if (testConfig.LoadTestingProjectBinariesFromItsOwnLocationOnly.ToBool())
            {
                objLocker = this.Agent;
            }

            lock (objLocker)
            {
                string strTitle         = testConfig.GetTitleForCurrentThread(strExecutionLocation, bRunningFailedTestCase, iNumberOfTimesRunningFailedTestCases);
                string strTestContainer = Path.Combine(testConfig.TestingProjectLocation, testConfig.SemicolonSeparatedFilesHavingTestCases.FirstFile());
                psiTestRunner.Arguments = GetCommandLineParameterStringForTestCases(arrTestRunnerPath[0],
                                                                                    strTitle,
                                                                                    strTestContainer,
                                                                                    strResultFile,
                                                                                    testCaseGroup);

                if (!string.IsNullOrEmpty(testConfig.WorkingDirectoryOfTestRunner))
                {
                    psiTestRunner.WorkingDirectory = testConfig.WorkingDirectoryOfTestRunner;
                }

                ConfigProperty configProperty = bRunningFailedTestCase ? ConfigProperty.BeforeRerunConfigEditor : ConfigProperty.BeforeRunConfigEditor;

                if (!_processStartInfoOfConfigEditorByCallerId.ContainsKey(strCallerId))
                {
                    CacheProcessStartInfoOfConfigEditorForCurrentCaller(strCallerId, testConfig, configProperty);
                }

                var psi = _processStartInfoOfConfigEditorByCallerId[strCallerId];
                if (psi != null)
                {
                    //this.Agent.LogUtil.LogMessage("Testing project's configuration is being changed...");
                    foreach (string strFileToBeRestored in _lstFilesToBeRestored)
                    {
                        string strBackupOfFileToEdit = GenerateBackupFileName(strFileToBeRestored);
                        if (!File.Exists(strBackupOfFileToEdit))
                        {
                            File.Copy(strFileToBeRestored, strBackupOfFileToEdit, true);
                        }
                        else
                        {
                            File.Copy(strBackupOfFileToEdit, strFileToBeRestored, true);
                        }
                    }

                    //this.Agent.LogUtil.LogMessage("Modifying testing project's configurations...");
                    var process = Process.Start(psi);
                    //ChildProcessTracker.AddProcess(process);
                    process.WaitForExit();
                }

                this.Agent.InputOutputUtil.CreateDirectoryIfDoesNotExist(strExecutionLocation);

                try
                {
                    this.Agent.LogUtil.LogStatus("Test cases are being executed by following command:");
                    StringBuilder sbStatus = new StringBuilder(psiTestRunner.Arguments);
                    sbStatus.AppendLine();
                    this.Agent.LogUtil.LogMessage(sbStatus.ToString());
                }
                catch
                { }

                psiTestRunner.ErrorDialog = true;
                processTestRunner         = Process.Start(psiTestRunner);
                if (testConfig.MakeTestRunnerAsChildProcessOfPTR.ToBool())
                {
                    ChildProcessTracker.AddProcess(processTestRunner);
                }

                bool bWaitForSometimeBeforeRestoringAFileButDoThisOnlyOnce = true;
                foreach (string strFileToBeRestored in _lstFilesToBeRestored)
                {
                    string strBackupOfFileToEdit = GenerateBackupFileName(strFileToBeRestored);
                    if (File.Exists(strBackupOfFileToEdit))
                    {
                        if (bWaitForSometimeBeforeRestoringAFileButDoThisOnlyOnce)
                        {
                            bWaitForSometimeBeforeRestoringAFileButDoThisOnlyOnce = false;
                            //this.Agent.LogUtil.LogMessage("Testing project's configuration is being reset...");
                            System.Threading.Thread.Sleep(5000);
                        }
                        File.Copy(strBackupOfFileToEdit, strFileToBeRestored, true);
                        File.Delete(strBackupOfFileToEdit);
                    }
                }
            }

            processTestRunner.WaitForExit();
            //this.Agent.LogUtil.LogWarning("Exit Code: " + processTestRunner.ExitCode);
        }
Exemplo n.º 2
0
        private IEnumerable <string> GetSemicolonSeparatedAssembliesHavingTestCases(TestConfig tempTestConfig)
        {
            string        strXmlHavingPathsOfAssemblies;
            XmlDocument   xmlDoc = new XmlDocument();
            List <string> lstAssembliesHavingTestCases = new List <string>();
            string        strTestingProjectLocation    = tempTestConfig.TestingProjectLocation.GetFullPath(Agent.InputOutputUtil);

            bool bLogMessage = false;

            if (File.Exists(strTestingProjectLocation))
            {
                strXmlHavingPathsOfAssemblies = strTestingProjectLocation;
                xmlDoc.Load(strXmlHavingPathsOfAssemblies);
            }
            else if (Directory.Exists(strTestingProjectLocation))
            {
                bLogMessage = true;
                Agent.LogUtil.LogMessage(string.Format("Fetching all the files having test cases for the testing configuration {0}...",
                                                       tempTestConfig.Identifier));

                strXmlHavingPathsOfAssemblies = Path.Combine(strTestingProjectLocation, CONSTANTS.FileHavingTestAssemblies);

                DeleteXmlFileHavingPathsOfAssembliesIfItContainsInvalidOrNoPath(strXmlHavingPathsOfAssemblies);

                if (!File.Exists(strXmlHavingPathsOfAssemblies))
                {
                    ProcessStartInfo       psi = new ProcessStartInfo();
                    List <ResourceSection> lstResourcesAllocated;
                    var arrPathWithParameters = tempTestConfig.GetExecutableAlongWithCommandLineParametersOfAGivenProperty(ConfigProperty.TestCasesExtractor,
                                                                                                                           tempTestConfig.ExecutionLocation,
                                                                                                                           this.Agent,
                                                                                                                           out lstResourcesAllocated);
                    psi.FileName  = arrPathWithParameters[0];
                    psi.Arguments = "\"" + strXmlHavingPathsOfAssemblies + "\" ";

                    psi.RedirectStandardInput = true;
                    psi.UseShellExecute       = false;
                    psi.CreateNoWindow        = true;

                    var process = Process.Start(psi);
                    ChildProcessTracker.AddProcess(process);
                    process.WaitForExit();
                }
                xmlDoc.Load(strXmlHavingPathsOfAssemblies);
                //File.Delete(strXmlHavingPathsOfAssemblies);
            }
            else
            {
                throw new InvalidConfigurationException(string.Format("{0}.{1} is not a valid path",
                                                                      tempTestConfig.Identifier,
                                                                      ConfigProperty.TestingProjectLocation));
            }

            tempTestConfig[ConfigProperty.SemicolonSeparatedFilesHavingTestCases.ToString()] = "";
            try
            {
                foreach (XmlNode testAssemblyNode in xmlDoc.GetElementsByTagName("TestAssembly"))
                {
                    string strAssemblyHavingTestCases = testAssemblyNode.Attributes["Path"].Value;
                    if (!File.Exists(strAssemblyHavingTestCases))
                    {
                        strAssemblyHavingTestCases = strAssemblyHavingTestCases.GetFullPath(Agent.InputOutputUtil);
                    }

                    if (!File.Exists(strAssemblyHavingTestCases))
                    {
                        throw new InvalidConfigurationException(string.Format("The path \"{0}\" specified in \"{1}\" (the value of {2}.{3}) is not a valid path",
                                                                              strAssemblyHavingTestCases,
                                                                              strXmlHavingPathsOfAssemblies,
                                                                              tempTestConfig.Identifier,
                                                                              ConfigProperty.TestingProjectLocation));
                    }
                    else
                    {
                        strAssemblyHavingTestCases = Path.GetFullPath(strAssemblyHavingTestCases);
                        if (bLogMessage)
                        {
                            Agent.LogUtil.LogStatus("Found a file having test cases: " + strAssemblyHavingTestCases);
                        }
                    }

                    strAssemblyHavingTestCases = strAssemblyHavingTestCases.Replace(strTestingProjectLocation, "").Trim('\\');
                    lstAssembliesHavingTestCases.Add(strAssemblyHavingTestCases);
                    tempTestConfig[ConfigProperty.SemicolonSeparatedFilesHavingTestCases.ToString()] =
                        tempTestConfig[ConfigProperty.SemicolonSeparatedFilesHavingTestCases.ToString()] + strAssemblyHavingTestCases + ";";
                }
            }
            catch (Exception exp)
            {
                Agent.LogUtil.LogError($"Either no test assembly is specified in \"{strXmlHavingPathsOfAssemblies}\" or the format of the file is not correct");
                throw exp;
            }

            return(lstAssembliesHavingTestCases);
        }