Manages a test run.
Example #1
0
        public override bool Execute()
        {
            // There's nothing to do if we have no source files
            if (String.IsNullOrEmpty(XapFile))
            {
                return true;
            }

            if (String.IsNullOrEmpty(ApplicationProductId) && !String.IsNullOrEmpty(ApplicationManifest))
            {
                ApplicationProductId = GetApplicationProductId();

                if (!String.IsNullOrEmpty(ApplicationProductId))
                {
                    Log.LogMessage("ProductId extracted from manifest: {0}", ApplicationProductId);
                }
            }

            if (String.IsNullOrEmpty(ApplicationProductId))
            {
                Log.LogError("ApplicationProductId was not supplied and could not be found");
                return false;
            }

            // Process the files
            bool succeeded = true;
            try
            {

                string testPath = XapFile;
                FileInfo testApplication = new FileInfo(testPath);

                // Make sure they didn't pass a directory as an item
                if (Directory.Exists(testPath))
                {
                    Log.LogError("Cannot move item {0} because it is a directory!", testPath);
                    return false;
                }

                // Make sure the source exists
                if (!testApplication.Exists)
                {
                    Log.LogError("Cannot process file {0} that does not exist!", testPath);
                    return false;
                }

                string testName = GetTestName(testApplication.Directory);
                if (!string.IsNullOrEmpty(TagExpression))
                {
                    testName += " (" + TagExpression + ")";
                }

                string name = TestResultsFile;
                if (string.IsNullOrEmpty(name))
                {
                    int k = 1;
                    name = "TestResults.trx";
                    while (File.Exists(Path.Combine(testApplication.DirectoryName, name)))
                    {
                        name = string.Format(CultureInfo.InvariantCulture, "TestResults{0}.trx", k++);
                    }
                }
                FileInfo log = new FileInfo(Path.Combine(testApplication.DirectoryName, name));

                Log.LogMessage("Begin unit testing");

                TestRunOptions tro = new TestRunOptions
                {
                    XapFile = testApplication.FullName,
                    ApplicationProductId = new Guid(ApplicationProductId),
                    UpdateApplication = UpdateApplication,
                    DeviceInfo = CreateDeviceInfo(),
                    TagExpression = TagExpression,
                    Log = log.FullName,
                    LocalPath = Path.GetDirectoryName(log.FullName)
                };
                tro.Page = testApplication.Name;

                TestRun tr = new TestRun(
                    new TestServiceOptions(),
                    tro);
                tr.Run();

                // Interpret results
                string pass = null;
                string total = null;

                if (log.Exists)
                {
                    DisplayTestResults(log, ref total, ref pass);

                    if (DeleteLogFiles)
                    {
                        log.Delete();
                    }
                }
                else
                {
                    Log.LogError(
                        "The log file {0} was never written by the test service for the {1} test.",
                        log.FullName,
                        testName);
                }

                if (tr.Total == 0)
                {
                    Log.LogWarning(
                        "There were 0 reported scenarios executed. Check that the tag expression is appropriate.");
                }
                else if (tr.Failures == 0)
                {
                    Log.LogMessage(
                        MessageImportance.High,
                        "Unit tests ({0}): {1}{2}",
                        testName,
                        pass != null ? " " + pass + " passing tests" : "",
                        total != null ? " " + total + " total tests" : "");
                }
                else
                {
                    succeeded = false;
                    LogFailureMessage(
                        "Unit test failures in test " +
                        testName +
                        ", " +
                        tr.Failures.ToString(CultureInfo.CurrentUICulture) +
                        " failed scenarios out of " +
                        tr.Total.ToString(CultureInfo.CurrentUICulture) +
                        " total scenarios executed.");
                }
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                succeeded = false;
            }

            return succeeded;
        }
        public override bool Execute()
        {
            // There's nothing to do if we have no source files
            if (TestPages == null || TestPages.Length == 0)
            {
                return true;
            }

            // Process the files
            bool succeeded = true;
            try
            {
                for (int i = 0; i < TestPages.Length; i++)
                {
                    string testPath = TestPages[i].ItemSpec;
                    FileInfo testApplication = new FileInfo(testPath);

                    // Make sure they didn't pass a directory as an item
                    if (Directory.Exists(testPath))
                    {
                        Log.LogError("Cannot move item {0} because it is a directory!", testPath);
                        succeeded = false;
                        continue;
                    }

                    // Make sure the source exists
                    if (!testApplication.Exists)
                    {
                        Log.LogError("Cannot process file {0} that does not exist!", testPath);
                        succeeded = false;
                        continue;
                    }

                    string testName = GetTestName(testApplication.Directory);
                    if (!string.IsNullOrEmpty(TagExpression))
                    {
                        testName += " (" + TagExpression + ")";
                    }

                    string name = TestResultsFile;
                    if (string.IsNullOrEmpty(name))
                    {
                        int k = 1;
                        name = "TestResults.trx";
                        while (File.Exists(Path.Combine(testApplication.DirectoryName, name)))
                        {
                            name = string.Format(CultureInfo.InvariantCulture, "TestResults{0}.trx", k++);
                        }
                    }
                    FileInfo log = new FileInfo(Path.Combine(testApplication.DirectoryName, name));

                    WebBrowserBrand wbb = WebBrowserFactory.ParseBrand(Browser);
                    TestRunOptions tro = new TestRunOptions
                    {
                        // StartupUri = testApplication.Name,
                        Browser = wbb,
                        LocalPath = testApplication.DirectoryName,
                        TagExpression = TagExpression,
                    };
                    tro.Page = testApplication.Name;
                    tro.Log = log.FullName;

                    if (wbb == WebBrowserBrand.Custom)
                    {
                        tro.SetCustomBrowser(Browser);
                    }
                    TestRun tr = new TestRun(
                        new TestServiceOptions(),
                        tro);
                    tr.Run();

                    // Interpret results
                    string pass = null;
                    string total = null;

                    if (log.Exists)
                    {
                        DisplayTestResults(log, ref total, ref pass);

                        if (DeleteLogFiles)
                        {
                            log.Delete();
                        }
                    }
                    else
                    {
                        Log.LogWarning(
                            "The log file {0} was never written by the test service for the {1} test.",
                            log.FullName,
                            testName);
                    }

                    if (tr.Total == 0)
                    {
                        Log.LogWarning("There were 0 reported scenarios executed. Check that the tag expression is appropriate.");
                    }
                    else if (tr.Failures == 0)
                    {
                        Log.LogMessage(
                            MessageImportance.High,
                            "Unit tests ({0}): {1}{2}",
                            testName,
                            pass != null ? " " + pass + " passing tests" : "",
                            total != null ? " " + total + " total tests" : "");
                    }
                    else
                    {
                        succeeded = false;
                        LogFailureMessage(
                            "Unit test failures in test " +
                            testName +
                            ", " +
                            tr.Failures.ToString(CultureInfo.CurrentUICulture) +
                            " failed scenarios out of " +
                            tr.Total.ToString(CultureInfo.CurrentUICulture) +
                            " total scenarios executed.");
                    }
                }
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                succeeded = false;
            }

            return succeeded;
        }