/// <summary>
        /// Saves the specified settings.
        /// </summary>
        public void SaveSettings()
        {
            //Get the various properties
            if (this.chkLogResults.IsChecked.HasValue)
            {
                Properties.Settings.Default.LogResults = this.chkLogResults.IsChecked.Value;
            }
            if (this.chkRunAsAdmin.IsChecked.HasValue)
            {
                Properties.Settings.Default.RunAsAdmin = this.chkRunAsAdmin.IsChecked.Value;
            }

            Properties.Settings.Default.BlockedRegex = this.txtBlockedRegex.Text.Trim();
            Properties.Settings.Default.CautionRegex = this.txtCautionRegex.Text.Trim();
            Properties.Settings.Default.FailRegex    = this.txtFailRegex.Text.Trim();
            Properties.Settings.Default.PassRegex    = this.txtPassRegex.Text.Trim();
            if (this.cboDefaultStatus.SelectedValue is AutomatedTestRun4.TestStatusEnum)
            {
                AutomatedTestRun4.TestStatusEnum defaultStatus = (AutomatedTestRun4.TestStatusEnum) this.cboDefaultStatus.SelectedValue;
                Properties.Settings.Default.DefaultStatus = (int)defaultStatus;
            }

            //Save the properties and reload
            Properties.Settings.Default.Save();
            this.LoadSettings();
        }
        /// <summary>
        /// Called when the Regex test button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTestRegex_Click(object sender, RoutedEventArgs e)
        {
            //Save the settings first
            SaveSettings();

            //Get the sample text
            string results = this.txtTestMessage.Text;

            //We use the Regexes to determine the status
            AutomatedTestRun4.TestStatusEnum status = (AutomatedTestRun4.TestStatusEnum)Properties.Settings.Default.DefaultStatus;
            Regex passRegex    = new Regex(Properties.Settings.Default.PassRegex, RegexOptions.IgnoreCase);
            Regex failRegex    = new Regex(Properties.Settings.Default.FailRegex, RegexOptions.IgnoreCase);
            Regex cautionRegex = new Regex(Properties.Settings.Default.CautionRegex, RegexOptions.IgnoreCase);
            Regex blockedRegex = new Regex(Properties.Settings.Default.BlockedRegex, RegexOptions.IgnoreCase);

            //Check passed
            if (passRegex.IsMatch(results))
            {
                status = AutomatedTestRun4.TestStatusEnum.Passed;
            }
            if (cautionRegex.IsMatch(results))
            {
                status = AutomatedTestRun4.TestStatusEnum.Caution;
            }
            if (failRegex.IsMatch(results))
            {
                status = AutomatedTestRun4.TestStatusEnum.Failed;
            }
            if (blockedRegex.IsMatch(results))
            {
                status = AutomatedTestRun4.TestStatusEnum.Blocked;
            }

            MessageBox.Show(String.Format("This test result would be recorded as '{0}'", status), "Regular Expression Test", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        /// <summary>
        /// Loads the saved settings
        /// </summary>
        private void LoadSettings()
        {
            //Populate the combo box
            this.cboDefaultStatus.ItemsSource = Enum.GetValues(typeof(AutomatedTestRun4.TestStatusEnum));

            //Load the various properties
            this.chkLogResults.IsChecked = Properties.Settings.Default.LogResults;
            this.chkRunAsAdmin.IsChecked = Properties.Settings.Default.RunAsAdmin;
            this.txtBlockedRegex.Text    = Properties.Settings.Default.BlockedRegex;
            this.txtCautionRegex.Text    = Properties.Settings.Default.CautionRegex;
            this.txtFailRegex.Text       = Properties.Settings.Default.FailRegex;
            this.txtPassRegex.Text       = Properties.Settings.Default.PassRegex;
            if (Enum.IsDefined(typeof(AutomatedTestRun4.TestStatusEnum), Properties.Settings.Default.DefaultStatus))
            {
                AutomatedTestRun4.TestStatusEnum defaultStatus = (AutomatedTestRun4.TestStatusEnum)Properties.Settings.Default.DefaultStatus;
                this.cboDefaultStatus.SelectedValue = defaultStatus;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// This is the main method that is used to start automated test execution
        /// </summary>
        /// <param name="automatedTestRun">The automated test run object</param>
        /// <param name="projectId">The id of the project</param>
        /// <returns>Either the populated test run or an exception</returns>
        public AutomatedTestRun4 StartExecution(AutomatedTestRun4 automatedTestRun, int projectId)
        {
            //Set status to OK
            base.status = EngineStatus.OK;

            try
            {
                if (Properties.Settings.Default.TraceLogging)
                {
                    LogEvent("Starting test execution", EventLogEntryType.Information);
                }
                DateTime startDate = DateTime.Now;

                /*
                 * TODO: Instantiate the code/API used to access the external testing system
                 */

                //See if we have any parameters we need to pass to the automation engine
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                if (automatedTestRun.Parameters == null)
                {
                    if (Properties.Settings.Default.TraceLogging)
                    {
                        LogEvent("Test Run has no parameters", EventLogEntryType.Information);
                    }
                }
                else
                {
                    if (Properties.Settings.Default.TraceLogging)
                    {
                        LogEvent("Test Run has parameters", EventLogEntryType.Information);
                    }

                    foreach (TestRunParameter testRunParameter in automatedTestRun.Parameters)
                    {
                        string parameterName = testRunParameter.Name.ToLowerInvariant();
                        if (!parameters.ContainsKey(parameterName))
                        {
                            //Make sure the parameters are lower case
                            if (Properties.Settings.Default.TraceLogging)
                            {
                                LogEvent("Adding test run parameter " + parameterName + " = " + testRunParameter.Value, EventLogEntryType.Information);
                            }
                            parameters.Add(parameterName, testRunParameter.Value);
                        }
                    }
                }

                //See if we have an attached or linked test script
                if (automatedTestRun.Type == AutomatedTestRun4.AttachmentType.URL)
                {
                    //The "URL" of the test is actually the full file path of the file that contains the test script
                    //Some automation engines need additional parameters which can be provided by allowing the test script filename
                    //to consist of multiple elements separated by a specific character.
                    //Conventionally, most engines use the pipe (|) character to delimit the different elements

                    //To make it easier, we have certain shortcuts that can be used in the path
                    //This allows the same test to be run on different machines with different physical folder layouts
                    string path = automatedTestRun.FilenameOrUrl;
                    path = path.Replace("[MyDocuments]", Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments));
                    path = path.Replace("[CommonDocuments]", Environment.GetFolderPath(System.Environment.SpecialFolder.CommonDocuments));
                    path = path.Replace("[DesktopDirectory]", Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory));
                    path = path.Replace("[ProgramFiles]", Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles));
                    path = path.Replace("[ProgramFilesX86]", Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86));

                    //First make sure that the file exists
                    if (File.Exists(path))
                    {
                        if (Properties.Settings.Default.TraceLogging)
                        {
                            LogEvent("Executing " + Constants.EXTERNAL_SYSTEM_NAME + " test located at " + path, EventLogEntryType.Information);
                        }

                        /*
                         * TODO: Add the external-tool specific code to actually run the test using these values:
                         *  -path   - The path of the test case to execute
                         *  -parameters - A dictionary of parameters to use (if the engine supports parameters)
                         */
                    }
                    else
                    {
                        throw new FileNotFoundException("Unable to find a " + Constants.EXTERNAL_SYSTEM_NAME + " test at " + path);
                    }
                }
                else
                {
                    //We have an embedded script which we need to send to the test execution engine
                    //If the automation engine doesn't support embedded/attached scripts, throw the following exception:

                    /*
                     * throw new InvalidOperationException("The " + Constants.EXTERNAL_SYSTEM_NAME + " automation engine only supports linked test scripts");
                     *
                     */

                    //First we need to get the test script
                    if (automatedTestRun.TestScript == null || automatedTestRun.TestScript.Length == 0)
                    {
                        throw new ApplicationException("The provided " + Constants.EXTERNAL_SYSTEM_NAME + " test script is empty, aborting test execution");
                    }
                    string testScript = Encoding.UTF8.GetString(automatedTestRun.TestScript);

                    /*
                     * TODO: Add the external-tool specific code to actually run the test script using these values:
                     *  -testScript - The text of the actual test script to execute
                     *  -parameters - A dictionary of parameters to use (if the engine supports parameters)
                     */
                }

                //Capture the time that it took to run the test
                DateTime endDate = DateTime.Now;

                //Now extract the test results

                /*
                 * TODO: Need to write the code to actually extract the results from the external testing tool
                 *      and transform them into the format expected by SpiraTest and RemoteLaunch.
                 *          - externalTestStatus
                 *          - externalTestSummary
                 *          - externalTestDetailedResults
                 */
                string externalTestStatus          = "Passed";                                          //TODO: Replace with real values
                string externalTestSummary         = "5 passed, 4 errors, 3 warnings, 2 informational"; //TODO: Replace with real values
                string externalTestDetailedResults = "";                                                //TODO: Replace with real values

                //Populate the Test Run object with the results
                if (String.IsNullOrEmpty(automatedTestRun.RunnerName))
                {
                    automatedTestRun.RunnerName = this.ExtensionName;
                }
                automatedTestRun.RunnerTestName = Path.GetFileNameWithoutExtension(automatedTestRun.FilenameOrUrl);

                //Convert the status for use in SpiraTest

                /*
                 * TODO: Change the CASE statement to match the statuses that the external tool uses
                 */
                AutomatedTestRun4.TestStatusEnum executionStatus = AutomatedTestRun4.TestStatusEnum.NotRun;
                switch (externalTestStatus)
                {
                case "PASSED":
                    executionStatus = AutomatedTestRun4.TestStatusEnum.Passed;
                    break;

                case "BLOCKED":
                    executionStatus = AutomatedTestRun4.TestStatusEnum.Blocked;
                    break;

                case "FAILED":
                    executionStatus = AutomatedTestRun4.TestStatusEnum.Failed;
                    break;

                case "CAUTION":
                    executionStatus = AutomatedTestRun4.TestStatusEnum.Caution;
                    break;
                }

                //Specify the start/end dates
                automatedTestRun.StartDate = startDate;
                automatedTestRun.EndDate   = endDate;

                //The result log
                automatedTestRun.ExecutionStatus  = executionStatus;
                automatedTestRun.RunnerMessage    = externalTestSummary;
                automatedTestRun.RunnerStackTrace = externalTestDetailedResults;

                //The format of the stack trace
                automatedTestRun.Format = AutomatedTestRun4.TestRunFormat.HTML;
                automatedTestRun.Format = AutomatedTestRun4.TestRunFormat.PlainText;

                /*
                 * TODO: Comment out the format that's not being used
                 */

                //Populate any test steps on the test run
                automatedTestRun.TestRunSteps = new List <TestRunStep4>();
                int position = 1;

                /*
                 * TODO: Use the following code in a for...next loop to add test runs for each returned test operation
                 */

                //Create the test step
                TestRunStep4 testRunStep = new TestRunStep4();
                testRunStep.Description    = "Description";
                testRunStep.ExpectedResult = "ExpectedResult";
                testRunStep.ActualResult   = "ActualResult";
                testRunStep.SampleData     = "SampleData";

                //TODO: Convert the status to the appropriate enumeration value
                testRunStep.ExecutionStatusId = (int)AutomatedTestRun4.TestStatusEnum.Passed;

                //Add the test step
                testRunStep.Position = position++;
                automatedTestRun.TestRunSteps.Add(testRunStep);

                //Populate any screenshots being added to the test run
                automatedTestRun.Screenshots = new List <TestRunScreenshot4>();

                /*
                 * TODO: Use the following code in a for...next loop to add attachments for each captured screenshot
                 * Replace the byte[] image = null with actual code for retrieving and populating the screenshot image
                 */
                TestRunScreenshot4 screenshot = new TestRunScreenshot4();
                byte[]             image      = null;
                screenshot.Data        = image;
                screenshot.Filename    = "Screenshot.png";
                screenshot.Description = "Description of screenshot";
                automatedTestRun.Screenshots.Add(screenshot);

                //Report as complete
                base.status = EngineStatus.OK;
                return(automatedTestRun);
            }
            catch (Exception exception)
            {
                //Log the error and denote failure
                LogEvent(exception.Message + " (" + exception.StackTrace + ")", EventLogEntryType.Error);

                //Report as completed with error
                base.status = EngineStatus.Error;
                throw exception;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// This is the main method that is used to start automated test execution
        /// </summary>
        /// <param name="automatedTestRun">The automated test run object</param>
        /// <param name="projectId">The id of the project</param>
        /// <returns>Either the populated test run or an exception</returns>
        public AutomatedTestRun4 StartExecution(AutomatedTestRun4 automatedTestRun, int projectId)
        {
            //Set status to OK
            base.status = EngineStatus.OK;
            string externalTestDetailedResults = "";
            string sCompletePath = "";

            try
            {
                if (Properties.Settings.Default.TraceLogging)
                {
                    LogEvent("Starting test execution", EventLogEntryType.Information);
                }
                DateTime startDate = DateTime.Now;

                //See if we have any parameters we need to pass to the automation engine
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                if (automatedTestRun.Parameters == null)
                {
                    if (Properties.Settings.Default.TraceLogging)
                    {
                        LogEvent("Test Run has no parameters", EventLogEntryType.Information);
                    }
                }
                else
                {
                    if (Properties.Settings.Default.TraceLogging)
                    {
                        LogEvent("Test Run has parameters", EventLogEntryType.Information);
                    }

                    foreach (TestRunParameter testRunParameter in automatedTestRun.Parameters)
                    {
                        string parameterName = testRunParameter.Name.Trim();
                        if (!parameters.ContainsKey(parameterName))
                        {
                            //Make sure the parameters are lower case
                            if (Properties.Settings.Default.TraceLogging)
                            {
                                LogEvent("Adding test run parameter " + parameterName + " = " + testRunParameter.Value, EventLogEntryType.Information);
                            }
                            parameters.Add(parameterName, testRunParameter.Value);
                        }
                    }
                }

                string runnerTestName = "Unknown";

                //See if we have an attached or linked test script
                if (automatedTestRun.Type == AutomatedTestRun4.AttachmentType.URL)
                {
                    //The "URL" of the test is actually the full file path of the file that contains the test script
                    //Some automation engines need additional parameters which can be provided by allowing the test script filename
                    //to consist of multiple elements separated by a specific character.
                    //Conventionally, most engines use the pipe (|) character to delimit the different elements
                    string[] elements = automatedTestRun.FilenameOrUrl.Split('|');

                    //To make it easier, we have certain shortcuts that can be used in the path
                    //This allows the same test to be run on different machines with different physical folder layouts
                    string path = elements[0];
                    runnerTestName = Path.GetFileNameWithoutExtension(path);
                    string args = "";
                    if (elements.Length > 1)
                    {
                        args = elements[1];
                    }
                    path = path.Replace("[MyDocuments]", Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments));
                    path = path.Replace("[CommonDocuments]", Environment.GetFolderPath(System.Environment.SpecialFolder.CommonDocuments));
                    path = path.Replace("[DesktopDirectory]", Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory));
                    path = path.Replace("[ProgramFiles]", Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles));
                    path = path.Replace("[ProgramFilesX86]", Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86));

                    //First make sure that the file exists
                    if (File.Exists(path))
                    {
                        if (Properties.Settings.Default.TraceLogging)
                        {
                            LogEvent("Executing " + Constants.EXTERNAL_SYSTEM_NAME + " test located at " + path, EventLogEntryType.Information);
                        }

                        //Get the working dir
                        string workingDir = Path.GetDirectoryName(path);

                        //Create the folder that will be used to store the output file
                        string sResultFile = "TS" + automatedTestRun.TestSetId + "_TC" + automatedTestRun.TestCaseId;
                        string directory   = Properties.Settings.Default.ResultPath + "\\" + GetTimestamp(DateTime.Now) + "_" + sResultFile + "\\";
                        sCompletePath = directory + sResultFile + ".rxlog";
                        CreateDirectory(new DirectoryInfo(directory));

                        // start the process and wait for exit
                        ProcessStartInfo startInfo = new ProcessStartInfo();
                        StringBuilder    builder   = new StringBuilder("/rf:\"" + sCompletePath + "\"");

                        //Convert the parameters into Ranorex format
                        //  /param:Parameter1="New Value"
                        foreach (KeyValuePair <string, string> parameter in parameters)
                        {
                            builder.Append(" /param:" + parameter.Key + "=\"" + parameter.Value + "\"");
                        }

                        //Add on any user-specified arguments
                        if (!String.IsNullOrWhiteSpace(args))
                        {
                            builder.Append(" " + args);
                        }

                        startInfo.Arguments              = builder.ToString();
                        startInfo.FileName               = path;
                        startInfo.WorkingDirectory       = workingDir;
                        startInfo.RedirectStandardOutput = true;
                        startInfo.UseShellExecute        = false;
                        Process p = Process.Start(startInfo);
                        externalTestDetailedResults  = String.Format("Executing: {0} in '{1}' with arguments '{2}'\n", startInfo.FileName, startInfo.WorkingDirectory, startInfo.Arguments);
                        externalTestDetailedResults += p.StandardOutput.ReadToEnd();
                        p.WaitForExit();
                        p.Close();
                    }
                    else
                    {
                        throw new FileNotFoundException("Unable to find a " + Constants.EXTERNAL_SYSTEM_NAME + " test at " + path);
                    }
                }
                else
                {
                    //We have an embedded script which we need to send to the test execution engine
                    //If the automation engine doesn't support embedded/attached scripts, throw the following exception:
                    throw new InvalidOperationException("The " + Constants.EXTERNAL_SYSTEM_NAME + " automation engine only supports linked test scripts");
                }

                //Capture the time that it took to run the test
                DateTime endDate = DateTime.Now;

                //Now extract the test results
                //Ranorex saves the XML data in a .rxlog.data file
                XmlDocument doc = new XmlDocument();
                doc.Load(sCompletePath + ".data");

                // Select the first book written by an author whose last name is Atwood.

                XmlNode result             = doc.DocumentElement.SelectSingleNode("/report/activity");
                string  externalTestStatus = result.Attributes["result"].Value;

                string      externalTestSummary = "";
                XmlNodeList errorMessages       = doc.DocumentElement.SelectNodes(".//errmsg");
                if (errorMessages.Count > 0)
                {
                    externalTestSummary = "";
                    foreach (XmlNode error in errorMessages)
                    {
                        externalTestSummary = externalTestSummary + error.InnerText + "\n";
                    }
                }
                else
                {
                    externalTestSummary = externalTestStatus;
                }

                //Populate the Test Run object with the results
                if (String.IsNullOrEmpty(automatedTestRun.RunnerName))
                {
                    automatedTestRun.RunnerName = this.ExtensionName;
                }
                automatedTestRun.RunnerTestName = Path.GetFileNameWithoutExtension(runnerTestName);

                //Convert the status for use in SpiraTest
                AutomatedTestRun4.TestStatusEnum executionStatus = AutomatedTestRun4.TestStatusEnum.Passed;
                switch (externalTestStatus)
                {
                case "Success":
                    executionStatus = AutomatedTestRun4.TestStatusEnum.Passed;
                    break;

                case "Failed":
                case "Error":
                    executionStatus = AutomatedTestRun4.TestStatusEnum.Failed;
                    break;

                case "Warn":
                    executionStatus = AutomatedTestRun4.TestStatusEnum.Caution;
                    break;

                default:
                    executionStatus = AutomatedTestRun4.TestStatusEnum.Blocked;
                    break;
                }

                //Specify the start/end dates
                automatedTestRun.StartDate = startDate;
                automatedTestRun.EndDate   = endDate;

                //The result log
                automatedTestRun.ExecutionStatus  = executionStatus;
                automatedTestRun.RunnerMessage    = externalTestSummary;
                automatedTestRun.RunnerStackTrace = externalTestDetailedResults;
                automatedTestRun.Format           = AutomatedTestRun4.TestRunFormat.PlainText;

                //Now get the detailed activity as 'test steps'
                //Also override the status if we find any failures or warnings
                XmlNodeList resultItems = doc.SelectNodes("/report/activity[@type='root']//activity/item");
                if (resultItems != null && resultItems.Count > 0)
                {
                    automatedTestRun.TestRunSteps = new List <TestRunStep4>();
                    int position = 1;
                    foreach (XmlNode xmlItem in resultItems)
                    {
                        string  category   = xmlItem.Attributes["category"].Value;
                        string  level      = xmlItem.Attributes["level"].Value;
                        string  message    = "";
                        XmlNode xmlMessage = xmlItem.SelectSingleNode("message");
                        if (xmlMessage != null)
                        {
                            message = xmlMessage.InnerText;
                        }

                        //Create the test step
                        TestRunStep4 testRunStep = new TestRunStep4();
                        testRunStep.Description    = category;
                        testRunStep.ExpectedResult = "";
                        testRunStep.ActualResult   = message;
                        testRunStep.SampleData     = "";

                        //Convert the status to the appropriate enumeration value
                        testRunStep.ExecutionStatusId = (int)AutomatedTestRun4.TestStatusEnum.NotRun;
                        switch (level)
                        {
                        case "Success":
                            testRunStep.ExecutionStatusId = (int)AutomatedTestRun4.TestStatusEnum.Passed;
                            break;

                        case "Info":
                            testRunStep.ExecutionStatusId = (int)AutomatedTestRun4.TestStatusEnum.NotApplicable;
                            break;

                        case "Warn":
                            testRunStep.ExecutionStatusId = (int)AutomatedTestRun4.TestStatusEnum.Caution;
                            if (automatedTestRun.ExecutionStatus == AutomatedTestRun4.TestStatusEnum.Passed ||
                                automatedTestRun.ExecutionStatus == AutomatedTestRun4.TestStatusEnum.NotRun ||
                                automatedTestRun.ExecutionStatus == AutomatedTestRun4.TestStatusEnum.NotApplicable)
                            {
                                automatedTestRun.ExecutionStatus = AutomatedTestRun4.TestStatusEnum.Caution;
                            }
                            break;

                        case "Failure":
                        case "Error":
                            testRunStep.ExecutionStatusId = (int)AutomatedTestRun4.TestStatusEnum.Failed;
                            if (automatedTestRun.ExecutionStatus != AutomatedTestRun4.TestStatusEnum.Failed)
                            {
                                automatedTestRun.ExecutionStatus = AutomatedTestRun4.TestStatusEnum.Failed;
                            }
                            break;
                        }

                        //Add the test step
                        testRunStep.Position = position++;
                        automatedTestRun.TestRunSteps.Add(testRunStep);
                    }
                }

                //Report as complete
                base.status = EngineStatus.OK;
                return(automatedTestRun);
            }
            catch (Exception exception)
            {
                //Log the error and denote failure
                LogEvent(exception.Message + " (" + exception.StackTrace + ")", EventLogEntryType.Error);

                //Report as completed with error
                base.status = EngineStatus.Error;
                throw exception;
            }
        }