Пример #1
0
        public void WhenTestMethodsAreAddedToTheProjectWhichUseCUITeToTestTheSampleApplicationS()
        {
            // HTML
            Assembly assembly = Assembly.GetExecutingAssembly();

            string htmlTestsCsFilePath = Path.Combine(this.testProjectPath, "HtmlTests.cs");
            AssemblyResourceLoader.ExtractEmbeddedResource(assembly, "CUITe.IntegrationTests.NuGet.HtmlTests.cs", htmlTestsCsFilePath);
            this.testProject.Project.ProjectItems.AddFromFile(htmlTestsCsFilePath);

            string tempWebPageCsFilePath = Path.Combine(this.testProjectPath, "TempWebPage.cs");
            AssemblyResourceLoader.ExtractEmbeddedResource(assembly, "CUITe.IntegrationTests.NuGet.TempWebPage.cs", tempWebPageCsFilePath);
            this.testProject.Project.ProjectItems.AddFromFile(tempWebPageCsFilePath);

            if (this.isSilverlightNuGetPackage)
            {
                // Microsoft.VisualStudio.TestTools.UITest.Extension.Silverlight.dll
                string microsoftVisualStudioTestToolsUITestExtensionSilverlightDllFilePath = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86),
                    "microsoft shared",
                    "VSTT",
                    string.Format("{0}.0", this.visualStudioVersionNumber),
                    "UITestExtensionPackages",
                    "Microsoft.VisualStudio.TestTools.UITest.Extension.Silverlight.dll");

                Assert.IsTrue(File.Exists(microsoftVisualStudioTestToolsUITestExtensionSilverlightDllFilePath),
                    string.Format("File doesn't exist: {0}", microsoftVisualStudioTestToolsUITestExtensionSilverlightDllFilePath));

                this.testProject.References.Add(microsoftVisualStudioTestToolsUITestExtensionSilverlightDllFilePath);

                // SilverlightControlTests.cs
                string silverlightControlTestsCsFilePath = Path.Combine(this.testProjectPath, "SilverlightControlTests.cs");
                AssemblyResourceLoader.ExtractEmbeddedResource(assembly, "CUITe.IntegrationTests.NuGet.SilverlightControlTests.cs", silverlightControlTestsCsFilePath);
                this.testProject.Project.ProjectItems.AddFromFile(silverlightControlTestsCsFilePath);

                // TestPage.cs
                string testPageCsFilePath = Path.Combine(this.testProjectPath, "TestPage.cs");
                AssemblyResourceLoader.ExtractEmbeddedResource(assembly, "CUITe.IntegrationTests.NuGet.TestPage.cs", testPageCsFilePath);
                this.testProject.Project.ProjectItems.AddFromFile(testPageCsFilePath);

                // CassiniDev 4.0
                // wait for 10 seconds otherwise the following error may appear in a message box for which can't be dismissed programmatically when trying to install multiple NuGet packages:
                // "Package Manager Console busy at the moment..."
                System.Threading.Thread.Sleep(10000);

                this.InstallNuGetPackage(this.testProject, "CassiniDev", ProjectName, "CassiniDev4-lib", version: "4.0.0");

                // Sut.Silverlight.html
                const string silverlightHtmlFileName = "Sut.Silverlight.html";
                string sutSilverlightHtmlFilePath = Path.Combine(this.testProjectPath, silverlightHtmlFileName);
                string sutSilverlightHtmlFileResourceName = string.Format("CUITe.IntegrationTests.NuGet.{0}", silverlightHtmlFileName);
                Trace.WriteLine(string.Format("Extracting embedded resource '{0}' to '{1}'", sutSilverlightHtmlFileResourceName, sutSilverlightHtmlFilePath));
                AssemblyResourceLoader.ExtractEmbeddedResource(assembly, sutSilverlightHtmlFileResourceName, sutSilverlightHtmlFilePath);
                ProjectItem sutSilverlightHtmlProjectItem = this.testProject.Project.ProjectItems.AddFromFile(sutSilverlightHtmlFilePath);
                this.SetCopyToOutputDirectory(sutSilverlightHtmlProjectItem);

                // Sut.Silverlight.xap
                const string silverlightXapFileName = "Sut.Silverlight.xap";
                string sutSilverlightXapFilePath = Path.Combine(this.testProjectPath, silverlightXapFileName);
                string sutSilverlightXapFileResourceName = string.Format("CUITe.IntegrationTests.NuGet.VisualStudio{0}.{1}", this.visualStudioVersion, silverlightXapFileName);
                Trace.WriteLine(string.Format("Extracting embedded resource '{0}' to '{1}'", sutSilverlightXapFileResourceName, sutSilverlightXapFilePath));
                AssemblyResourceLoader.ExtractEmbeddedResource(assembly, sutSilverlightXapFileResourceName, sutSilverlightXapFilePath);
                ProjectItem sutSilverlightXapProjectItem = this.testProject.Project.ProjectItems.AddFromFile(sutSilverlightXapFilePath);
                this.SetCopyToOutputDirectory(sutSilverlightXapProjectItem);

                const string localTestSettingsFileName = "Local.testsettings";
                this.testSettingsFilePath = Path.Combine(this.solutionDirectory.DirectoryPath, localTestSettingsFileName);

                const string namespaceUri = "http://microsoft.com/schemas/VisualStudio/TeamTest/2010";

                if (this.visualStudioVersionNumber != "10")
                {
                    // Visual Studio 2010 will automatically create a Local.testsettings file but with deployment disabled
                    // add new test settings solution item
                    /*Project solutionItemsProject =*/ this.solution.AddSolutionFolder("Solution Items");

                    XNamespace ns = XNamespace.Get(namespaceUri);

                    XDocument doc = new XDocument(
                        new XDeclaration("1.0", "UTF-8", ""),
                        new XElement(ns + "TestSettings",
                            new XAttribute("name", "Local"),
                            new XAttribute("id", Guid.NewGuid().ToString()),
                            new XElement(ns + "Description", "These are default test settings for a local test run."),
                            new XElement(ns + "Deployment",
                                new XAttribute("enabled", "true"))));
                    doc.Save(this.testSettingsFilePath);

                    // if we try to add the file to the solution using automation, the file will automatically open
                    // when specifically adding a test settings file, a modal wizard dialog will automatically be displayed that needs to be manually closed
                    // solutionItemsProject.ProjectItems.AddFromFile(this.testSettingsFilePath);
                    // so update the sln file directly

                    // select test settings file
                    System.Diagnostics.Process visualStudioProcess = System.Diagnostics.Process.GetProcessesByName("devenv").Single(x => x.MainWindowTitle.Contains(SolutionName));
                    VisualStudioScreen visualStudio = Screen.FromProcess<VisualStudioScreen>(visualStudioProcess);
                    visualStudio.TestMenuItem.Click();
                    visualStudio.TestSettingsMenuItem.Click();
                    visualStudio.SelectTestSettingsFileMenuItem.Click();

                    Playback.PlaybackSettings.WaitForReadyTimeout = 1000; // 60000

                    OpenTestSettingsFileWindow openTestSettingsFileWindow  = new OpenTestSettingsFileWindow();
                    openTestSettingsFileWindow.FileName.EditableItem = this.testSettingsFilePath;
                    openTestSettingsFileWindow.OpenButton.Click();
                }

                // For troubleshooting purposes, enable logging in App.config:
                string appConfigFilePath = Path.Combine(this.testProjectPath, "App.config");
                Assert.IsTrue(!File.Exists(appConfigFilePath));
                const string appConfigFileResourceName = "CUITe.IntegrationTests.NuGet.App.config";
                Trace.WriteLine(string.Format("Extracting embedded resource '{0}' to '{1}'", appConfigFileResourceName, appConfigFilePath));
                AssemblyResourceLoader.ExtractEmbeddedResource(assembly, appConfigFileResourceName, appConfigFilePath);
                this.testProject.Project.ProjectItems.AddFromFile(appConfigFilePath);
                //File.WriteAllLines(appConfigFilePath, new[]
                //{
                //    "<configuration>",
                //    "  <system.diagnostics>",
                //    "    <switches>",
                //    "      <add name=\"EqtTraceLevel\" value=\"4\" />",
                //    "    </switches>",
                //    "  </system.diagnostics>",
                //    "</configuration>"
                //});
                //xmlDocument.Load(appConfigFilePath);

                //string xpath = "/configuration";
                //XmlNode configurationNode = xmlDocument.SelectSingleNode(xpath);
                //if (configurationNode == null)
                //{
                //    throw new Exception(string.Format("Failed to select single node with xpath '{0}' in file '{1}'", xpath, appConfigFilePath));
                //}

                //xpath = "/system.diagnostics";
                //XmlNode systemDiagnosticsNode = configurationNode.SelectSingleNode(xpath);
                //if (systemDiagnosticsNode == null)
                //{
                //    throw new Exception(string.Format("Failed to select single node with xpath '{0}' in file '{1}'", xpath, appConfigFilePath));
                //}

                //xpath = "/switches";
                //XmlNode switchesNode = systemDiagnosticsNode.SelectSingleNode(xpath);
                //if (switchesNode == null)
                //{
                //    throw new Exception(string.Format("Failed to select single node with xpath '{0}' in file '{1}'", xpath, appConfigFilePath));
                //}

                //xpath = "/add[@name='EqTaceLevel']";
                //XmlNode addNode = switchesNode.SelectSingleNode(xpath);
                //if (addNode == null)
                //{
                //    throw new Exception(string.Format("Failed to select single node with xpath '{0}' in file '{1}'", xpath, appConfigFilePath));
                //}

                //if (addNode.Attributes == null)
                //{
                //    throw new Exception(string.Format("Xml node attributes at xpath '{0}' in file '{1}' is null", xpath, appConfigFilePath));
                //}

                //addNode.Attributes["value"].Value = "4";
                //xmlDocument.Save(appConfigFilePath);

                // add Sut.Silverlight.xap as a deployment item
                // enable deployment
                // Change the following:
                // <TestSettings>
                //   <Deployment enabled="false" />
                // ...
                // To this:
                // <TestSettings>
                //   <Deployment enabled="true">
                //     <DeploymentItem filename="TestProject\Sut.Silverlight.xap" />
                //   </Deployment>
                // ...
                XmlDocument xmlDocument = new XmlDocument();
                XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);
                xmlNamespaceManager.AddNamespace("mstt", namespaceUri);
                xmlDocument.Load(this.testSettingsFilePath);

                XmlNode testSettingsXmlNode = xmlDocument.SelectSingleNode("//mstt:TestSettings", xmlNamespaceManager);
                Assert.IsNotNull(testSettingsXmlNode);
                XmlNode deploymentXmlNode = testSettingsXmlNode.SelectSingleNode("//mstt:Deployment", xmlNamespaceManager);
                Assert.IsNotNull(deploymentXmlNode);
                Assert.IsNotNull(deploymentXmlNode.Attributes);
                deploymentXmlNode.Attributes["enabled"].Value = "true";

                XmlElement deploymentItem = xmlDocument.CreateElement(string.Empty, "DeploymentItem", namespaceUri);
                deploymentItem.SetAttribute("filename", string.Format(@"{0}\{1}", ProjectName, silverlightXapFileName));
                deploymentXmlNode.AppendChild(deploymentItem);
                xmlDocument.Save(this.testSettingsFilePath);

                // force solution to reload
                this.dte.ExecuteCommand("File.SaveAll");
                this.dte.Solution.Close();
                this.dte.Solution.Open(this.solutionFilePath);
            }
        }