示例#1
0
        public override async Task RunTestAsync()
        {
            var projectDir = System.IO.Path.GetDirectoryName(ProjectFile);
            var name       = System.IO.Path.GetFileName(projectDir);

            if (string.Equals("mac", name, StringComparison.OrdinalIgnoreCase))
            {
                name = System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(projectDir));
            }
            var suffix = string.Empty;

            switch (Platform)
            {
            case TestPlatform.Mac_Modern:
                suffix = "-modern";
                break;

            case TestPlatform.Mac_Full:
                suffix = "-full";
                break;

            case TestPlatform.Mac_System:
                suffix = "-system";
                break;
            }
            if (ProjectFile.EndsWith(".sln", StringComparison.Ordinal))
            {
                Path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(ProjectFile), "bin", BuildTask.ProjectPlatform, BuildTask.ProjectConfiguration + suffix, name + ".app", "Contents", "MacOS", name);
            }
            else
            {
                var project = new XmlDocument();
                project.LoadWithoutNetworkAccess(ProjectFile);
                var outputPath   = project.GetOutputPath(BuildTask.ProjectPlatform, BuildTask.ProjectConfiguration).Replace('\\', '/');
                var assemblyName = project.GetAssemblyName();
                Path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(ProjectFile), outputPath, assemblyName + ".app", "Contents", "MacOS", assemblyName);
            }

            using (var resource = await NotifyAndAcquireDesktopResourceAsync()) {
                using (var proc = new Process()) {
                    proc.StartInfo.FileName = Path;
                    var            arguments    = new List <string> ();
                    IFileBackedLog xmlLog       = null;
                    var            useXmlOutput = Harness.InCI || true;
                    if (IsUnitTest)
                    {
                        var extension = useXmlOutput ? "xml" : "log";
                        var type      = useXmlOutput ? LogType.XmlLog : LogType.NUnitResult;
                        xmlLog = Logs.Create($"test-{Platform}-{Timestamp}.{extension}", type.ToString());
                        arguments.Add($"-transport:FILE");
                        arguments.Add($"--logfile:{xmlLog.FullPath}");
                        if (useXmlOutput)
                        {
                            arguments.Add("--enablexml");
                            arguments.Add("--xmlmode=wrapped");
                            arguments.Add("--xmlversion=nunitv3");
                        }
                    }
                    if (!Harness.GetIncludeSystemPermissionTests(Platform, false))
                    {
                        proc.StartInfo.EnvironmentVariables ["DISABLE_SYSTEM_PERMISSION_TESTS"] = "1";
                    }
                    proc.StartInfo.EnvironmentVariables ["MONO_DEBUG"] = "no-gdb-backtrace";
                    proc.StartInfo.Arguments = StringUtils.FormatArguments(arguments);
                    Jenkins.MainLog.WriteLine("Executing {0} ({1})", TestName, Mode);
                    var log = Logs.Create($"execute-{Platform}-{Timestamp}.txt", LogType.ExecutionLog.ToString());
                    ICrashSnapshotReporter snapshot = null;
                    if (!Jenkins.Harness.DryRun)
                    {
                        ExecutionResult = TestExecutingResult.Running;

                        snapshot = CrashReportSnapshotFactory.Create(log, Logs, isDevice: false, deviceName: null);
                        await snapshot.StartCaptureAsync();

                        ProcessExecutionResult result = null;
                        try {
                            var timeout = TimeSpan.FromMinutes(20);

                            result = await ProcessManager.RunAsync(proc, log, timeout);

                            if (result.TimedOut)
                            {
                                FailureMessage = $"Execution timed out after {timeout.TotalSeconds} seconds.";
                                log.WriteLine(FailureMessage);
                                ExecutionResult = TestExecutingResult.TimedOut;
                            }
                            else if (result.Succeeded)
                            {
                                ExecutionResult = TestExecutingResult.Succeeded;
                            }
                            else
                            {
                                ExecutionResult = TestExecutingResult.Failed;
                                FailureMessage  = result.ExitCode != 1 ? $"Test run crashed (exit code: {result.ExitCode})." : "Test run failed.";
                                log.WriteLine(FailureMessage);
                            }
                        } finally {
                            await snapshot.EndCaptureAsync(TimeSpan.FromSeconds(Succeeded ? 0 : result?.ExitCode > 1 ? 120 : 5));
                        }
                    }
                    Jenkins.MainLog.WriteLine("Executed {0} ({1})", TestName, Mode);

                    if (IsUnitTest)
                    {
                        var reporterFactory = new TestReporterFactory(ProcessManager);
                        var listener        = new Microsoft.DotNet.XHarness.iOS.Shared.Listeners.SimpleFileListener(xmlLog.FullPath, log, xmlLog, useXmlOutput);
                        var reporter        = reporterFactory.Create(Harness.HarnessLog, log, Logs, snapshot, listener, Harness.ResultParser, new AppBundleInformation("N/A", "N/A", "N/A", "N/A", true, null), RunMode.MacOS, Harness.XmlJargon, "no device here", TimeSpan.Zero);
                        var rv = await reporter.ParseResult();

                        ExecutionResult = rv.ExecutingResult;
                        FailureMessage  = rv.ExecutingResult == TestExecutingResult.Succeeded ? null : rv.ResultMessage;
                    }
                }
            }
        }
示例#2
0
        public override async Task RunTestAsync()
        {
            var projectDir = System.IO.Path.GetDirectoryName(ProjectFile);
            var name       = System.IO.Path.GetFileName(projectDir);

            if (string.Equals("mac", name, StringComparison.OrdinalIgnoreCase))
            {
                name = System.IO.Path.GetFileName(System.IO.Path.GetDirectoryName(projectDir));
            }
            var suffix = string.Empty;

            switch (Platform)
            {
            case TestPlatform.Mac_Modern:
                suffix = "-modern";
                break;

            case TestPlatform.Mac_Full:
                suffix = "-full";
                break;

            case TestPlatform.Mac_System:
                suffix = "-system";
                break;
            }
            if (ProjectFile.EndsWith(".sln", StringComparison.Ordinal))
            {
                Path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(ProjectFile), "bin", BuildTask.ProjectPlatform, BuildTask.ProjectConfiguration + suffix, name + ".app", "Contents", "MacOS", name);
            }
            else
            {
                var project = new XmlDocument();
                project.LoadWithoutNetworkAccess(ProjectFile);
                string outputPath;
                if (TestProject?.IsDotNetProject == true)
                {
                    outputPath = await Harness.AppBundleLocator.LocateAppBundle(project, ProjectFile, TestTarget.None, BuildTask.ProjectConfiguration);
                }
                else
                {
                    outputPath = project.GetOutputPath(BuildTask.ProjectPlatform, BuildTask.ProjectConfiguration).Replace('\\', '/');
                }
                var assemblyName = project.GetAssemblyName();
                Path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(ProjectFile), outputPath, assemblyName + ".app", "Contents", "MacOS", assemblyName);
            }

            using (var resource = await NotifyAndAcquireDesktopResourceAsync()) {
                using (var proc = new Process()) {
                    proc.StartInfo.FileName = Path;
                    var            arguments    = new List <string> ();
                    IFileBackedLog xmlLog       = null;
                    var            useXmlOutput = Harness.InCI || true;
                    if (IsUnitTest)
                    {
                        var extension = useXmlOutput ? "xml" : "log";
                        var type      = useXmlOutput ? LogType.XmlLog : LogType.NUnitResult;
                        xmlLog = Logs.Create($"test-{Platform}-{Timestamp}.{extension}", type.ToString());
                        arguments.Add($"-transport:FILE");
                        proc.StartInfo.EnvironmentVariables ["NUNIT_TRANSPORT"] = "FILE";
                        arguments.Add($"--logfile:{xmlLog.FullPath}");
                        proc.StartInfo.EnvironmentVariables ["NUNIT_LOG_FILE"] = xmlLog.FullPath;
                        if (useXmlOutput)
                        {
                            arguments.Add("--enablexml");
                            proc.StartInfo.EnvironmentVariables ["NUNIT_ENABLE_XML_OUTPUT"] = "true";
                            arguments.Add("--xmlmode=wrapped");
                            proc.StartInfo.EnvironmentVariables ["NUNIT_ENABLE_XML_MODE"] = "wrapped";
                            arguments.Add("--xmlversion=nunitv3");
                            proc.StartInfo.EnvironmentVariables ["NUNIT_XML_VERSION"] = "nunitv3";
                        }
                        arguments.Add("--autostart");
                        proc.StartInfo.EnvironmentVariables ["NUNIT_AUTOSTART"] = "true";
                        arguments.Add("--autoexit");
                        proc.StartInfo.EnvironmentVariables ["NUNIT_AUTOEXIT"] = "true";
                    }
                    if (!Harness.GetIncludeSystemPermissionTests(Platform, false))
                    {
                        proc.StartInfo.EnvironmentVariables ["DISABLE_SYSTEM_PERMISSION_TESTS"] = "1";
                    }
                    proc.StartInfo.EnvironmentVariables ["MONO_DEBUG"] = "no-gdb-backtrace";
                    proc.StartInfo.EnvironmentVariables.Remove("DYLD_FALLBACK_LIBRARY_PATH");                      // VSMac might set this, and the test may end up crashing
                    proc.StartInfo.Arguments = StringUtils.FormatArguments(arguments);
                    Jenkins.MainLog.WriteLine("Executing {0} ({1})", TestName, Mode);
                    var log = Logs.Create($"execute-{Platform}-{Timestamp}.txt", LogType.ExecutionLog.ToString());
                    ICrashSnapshotReporter snapshot = null;
                    if (!Jenkins.Harness.DryRun)
                    {
                        ExecutionResult = TestExecutingResult.Running;

                        snapshot = CrashReportSnapshotFactory.Create(log, Logs, isDevice: false, deviceName: null);
                        await snapshot.StartCaptureAsync();

                        ProcessExecutionResult result = null;
                        try {
                            var timeout = TimeSpan.FromMinutes(20);

                            result = await ProcessManager.RunAsync(proc, log, timeout);

                            if (result.TimedOut)
                            {
                                FailureMessage = $"Execution timed out after {timeout.TotalSeconds} seconds.";
                                log.WriteLine(FailureMessage);
                                ExecutionResult = TestExecutingResult.TimedOut;
                            }
                            else if (result.Succeeded)
                            {
                                ExecutionResult = TestExecutingResult.Succeeded;
                            }
                            else
                            {
                                ExecutionResult = TestExecutingResult.Failed;
                                FailureMessage  = result.ExitCode != 1 ? $"Test run crashed (exit code: {result.ExitCode})." : "Test run failed.";
                                log.WriteLine(FailureMessage);
                            }
                        } finally {
                            await snapshot.EndCaptureAsync(TimeSpan.FromSeconds(Succeeded ? 0 : result?.ExitCode > 1 ? 120 : 5));
                        }
                    }
                    Jenkins.MainLog.WriteLine("Executed {0} ({1})", TestName, Mode);

                    if (IsUnitTest)
                    {
                        var reporterFactory = new TestReporterFactory(ProcessManager);
                        var listener        = new Microsoft.DotNet.XHarness.iOS.Shared.Listeners.SimpleFileListener(xmlLog.FullPath, log, xmlLog, useXmlOutput);
                        var reporter        = reporterFactory.Create(Harness.HarnessLog, log, Logs, snapshot, listener, Harness.ResultParser, new AppBundleInformation("N/A", "N/A", "N/A", "N/A", true, null), RunMode.MacOS, Harness.XmlJargon, "no device here", TimeSpan.Zero);
                        var rv = await reporter.ParseResult();

                        if (ExecutionResult == TestExecutingResult.Succeeded)
                        {
                            // The process might have crashed, timed out at exit, or otherwise returned a non-zero exit code when all the unit tests passed, in which we shouldn't override the execution result here,
                            ExecutionResult = rv.ExecutingResult;
                        }

                        // Set or replace the failure message, depending on whether there already is a failure message or not.
                        if (string.IsNullOrEmpty(FailureMessage))
                        {
                            FailureMessage = rv.ResultMessage;
                        }
                        else if (!string.IsNullOrEmpty(rv.ResultMessage))
                        {
                            FailureMessage += "\n" + rv.ResultMessage;
                        }
                    }
                }
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            try
            {
                ConsoleCmdLine   ccl            = new ConsoleCmdLine();
                CmdLineString    outputFileName = new CmdLineString("output-file", false, "Output File Name - defaults to SeleniumJasmineResults.xml");
                CmdLineString    chromeBaseDir  = new CmdLineString("chrome-path", false, @"Path to ChromeDriver.exe - defaults to c:\selenium\chromedriver_win32_2.2");
                CmdLineString    ieBaseDir      = new CmdLineString("ie-path", false, @"Path to IEDriverServer.exe - defaults to c:\selenium\IEDriverServer_x86_2.34.0");
                CmdLineString    inputUrlList   = new CmdLineString("input-url-file", false, "Input file containing urls to test - defaults to SpecRunnerList.txt");
                CmdLineParameter runChrome      = new CmdLineParameter("chrome", false, "Run Selenium with the chrome driver");
                CmdLineParameter runIE          = new CmdLineParameter("ie", false, "Run Selenium with the ie driver");
                CmdLineParameter runFireFox     = new CmdLineParameter("firefox", false, "Run Selenium with the FireFox driver");

                CmdLineParameter timeout     = new CmdLineParameter("timeout", false, "Timeout value (seconds) to wait for the tests to finish. defaults to 90.");
                CmdLineParameter reporter    = new CmdLineParameter("reporter", false, "Reporter type : jenkins | teamcity.  Defaults to teamcity reporter ");
                CmdLineParameter resultInput = new CmdLineParameter("reporter-input", false, "Reporter type : trivialreporter | logreporter.  Use trivial reporter for jasmine < 2.0 , use logreporter otherwise. ");


                ccl.RegisterParameter(outputFileName);
                ccl.RegisterParameter(chromeBaseDir);
                ccl.RegisterParameter(ieBaseDir);
                ccl.RegisterParameter(inputUrlList);
                ccl.RegisterParameter(runChrome);
                ccl.RegisterParameter(runIE);
                ccl.RegisterParameter(runFireFox);
                ccl.RegisterParameter(timeout);
                ccl.RegisterParameter(reporter);
                ccl.RegisterParameter(resultInput);
                ccl.Parse(args);


                string strOutputFileName     = !string.IsNullOrEmpty(outputFileName.Value) ? outputFileName.Value : "SeleniumTestRunner.xml";
                string strChromeBaseDir      = !string.IsNullOrEmpty(chromeBaseDir.Value) ? chromeBaseDir.Value : @"E:\source\github\selenium-jasmine-runner\chromedriver_win32_2.2";
                string strIEBaseDir          = !string.IsNullOrEmpty(ieBaseDir.Value) ? ieBaseDir.Value : @"E:\source\github\selenium-jasmine-runner\IEDriverServer_x64_2.34.0";
                string strSpecRunnerListFile = !string.IsNullOrEmpty(inputUrlList.Value) ? inputUrlList.Value : "SpecRunnerList.txt";
                string strReporter           = !string.IsNullOrEmpty(reporter.Value) ? reporter.Value : "teamcity";

                string strInput = !string.IsNullOrEmpty(resultInput.Value) ? resultInput.Value : "logreporter";

                short timeoutValue = 90;
                if (!string.IsNullOrEmpty(timeout.Value))
                {
                    Int16.TryParse(timeout.Value, out timeoutValue);
                }

                TestSuites testSuites = new TestSuites(TestReporterFactory.GetTestReporter(strReporter));

                List <string> strFileList = new List <string> ();
                using (FileStream fs = new FileStream(strSpecRunnerListFile, FileMode.Open, FileAccess.Read))
                {
                    StreamReader streamReader = new StreamReader(fs);

                    string strLine = streamReader.ReadLine();

                    while (!string.IsNullOrEmpty(strLine))
                    {
                        strFileList.Add(strLine);
                        strLine = streamReader.ReadLine();
                    }
                }

                if (runFireFox.Exists)
                {
                    using (var firefoxDriver = new OpenQA.Selenium.Firefox.FirefoxDriver())
                    {
                        foreach (string strSpecRunner in strFileList)
                        {
                            string strPageName = strSpecRunner.Substring(strSpecRunner.LastIndexOf('/') + 1);
                            if (strPageName.IndexOf('.') > 0)
                            {
                                strPageName = strPageName.Substring(0, strPageName.IndexOf('.'));
                            }

                            if (strInput == "logreporter")
                            {
                                RunAllJasmineTestsAndReport_LogReporter(firefoxDriver, timeoutValue, strSpecRunner,
                                                                        strPageName,
                                                                        "Chrome", ref testSuites);
                            }
                            else
                            {
                                RunAllJasmineTestsAndReportTrivialReporter(firefoxDriver, timeoutValue, strSpecRunner,
                                                                           strPageName, "FireFox", ref testSuites);
                            }
                        }
                    }
                }

                if (runChrome.Exists)
                {
                    using (var driver = new OpenQA.Selenium.Chrome.ChromeDriver(strChromeBaseDir))
                    {
                        foreach (string strSpecRunner in strFileList)
                        {
                            string strPageName = strSpecRunner.Substring(strSpecRunner.LastIndexOf('/') + 1);
                            if (strPageName.IndexOf('.') > 0)
                            {
                                strPageName = strPageName.Substring(0, strPageName.IndexOf('.'));
                            }

                            if (strInput == "logreporter")
                            {
                                RunAllJasmineTestsAndReport_LogReporter(driver, timeoutValue, strSpecRunner, strPageName,
                                                                        "Chrome", ref testSuites);
                            }
                            else
                            {
                                RunAllJasmineTestsAndReportTrivialReporter(driver, timeoutValue, strSpecRunner,
                                                                           strPageName, "Chrome", ref testSuites);
                            }
                        }
                    }
                }

                if (runIE.Exists)
                {
                    InternetExplorerOptions options = new InternetExplorerOptions();
                    options.EnableNativeEvents = false;
                    options.EnsureCleanSession = true;

                    using (var driver = new OpenQA.Selenium.IE.InternetExplorerDriver(strIEBaseDir))

                    {
                        foreach (string strSpecRunner in strFileList)
                        {
                            string strPageName = strSpecRunner.Substring(strSpecRunner.LastIndexOf('/') + 1);
                            if (strPageName.IndexOf('.') > 0)
                            {
                                strPageName = strPageName.Substring(0, strPageName.IndexOf('.'));
                            }

                            if (strInput == "logreporter")
                            {
                                RunAllJasmineTestsAndReport_LogReporter(driver, timeoutValue, strSpecRunner, strPageName, "IE", ref testSuites);
                            }
                            else
                            {
                                RunAllJasmineTestsAndReportTrivialReporter(driver, timeoutValue, strSpecRunner, strPageName, "IE", ref testSuites);
                            }
                        }
                    }
                }



                Console.WriteLine("-----------");

                if (strInput != "logreporter")
                {
                    testSuites.WriteToConsole();

                    using (FileStream fs = new FileStream(strOutputFileName, FileMode.Create, FileAccess.Write))
                    {
                        StreamWriter streamWriter = new StreamWriter(fs);

                        testSuites.WriteToStream(streamWriter);

                        streamWriter.Flush();
                    }

                    using (StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()))
                    {
                        sw.AutoFlush = true;
                        testSuites.WriteToStream(sw);
                        sw.Flush();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("");
                Console.WriteLine("Unhandled Exception " + ex.ToString());
                Console.WriteLine("");
                Console.WriteLine(ex.ToString());
                throw ex;
            }
        }