在构建服务器上获取Windows Store单元测试的自动开发人员许可证

时间:2012-11-10 12:49:03

标签: .net unit-testing windows-8 windows-store-apps

对于Windows应用商店应用程序,运行单元测试需要开发人员许可证,这对个人计算机上的个别开发人员来说很好。

但是对于构建服务器上的持续集成过程,我们确实需要能够自动获取许可证。

Microsoft提供了一个命令行工具,但它仍然会创建一个交互式窗口,这会破坏该过程。

TailoredDeploy.exe AcquireDeveloperLicense

有没有人知道自动获取开发者许可证的任何其他方式?如果需要手动设置,那么产生新的构建节点将会变得很糟糕。每个人的VPN。

1 个答案:

答案 0 :(得分:4)

我被建议使用UI Automation,这是我到目前为止所想到的:

using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;

class Program
{
    static void Main(string[] args)
    {
        var processStartInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\TailoredDeploy.exe", "AcquireDeveloperLicense");

        var process = Process.Start(processStartInfo);
        process.WaitForInputIdle();

        Thread.Sleep(1000);

        var agreementWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children,
            new PropertyCondition(AutomationElement.ProcessIdProperty, process.Id));

        var iAgreeButton = agreementWindow.FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                                          .FindAll(TreeScope.Children, Condition.TrueCondition)[2];

        var buttonPattern = iAgreeButton.GetCurrentPattern(AutomationPattern.LookupById(InvokePattern.Pattern.Id)) as InvokePattern;
        buttonPattern.Invoke();

        Thread.Sleep(10000);

        var processes = Process.GetProcessesByName("dllhost");

        var credentialsWindows = AutomationElement.RootElement.FindAll(TreeScope.Children,
            new PropertyCondition(AutomationElement.ProcessIdProperty, processes[0].Id));

        if (credentialsWindows[0].Current.Name == "Developer License")
        {
            var credentialsPane = credentialsWindows[0].FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                                                       .FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                                                       .FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                                                       .FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                                                       .FindAll(TreeScope.Children, Condition.TrueCondition);

            var usernameTextBox = credentialsPane[3].GetCurrentPattern(AutomationPattern.LookupById(ValuePattern.Pattern.Id)) as ValuePattern;
            usernameTextBox.SetValue("username@outlook.com");

            var passwordTextBox = credentialsPane[5].GetCurrentPattern(AutomationPattern.LookupById(ValuePattern.Pattern.Id)) as ValuePattern;
            passwordTextBox.SetValue("password");

            var loginButton = credentialsPane[8].GetCurrentPattern(AutomationPattern.LookupById(InvokePattern.Pattern.Id)) as InvokePattern;
            loginButton.Invoke();

            Thread.Sleep(10000);

            var finishUpProcesses = Process.GetProcessesByName("TailoredDeploy");

            var finishWindow = AutomationElement.RootElement.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.ProcessIdProperty, finishUpProcesses[0].Id));

            var lastItems = finishWindow.FindAll(TreeScope.Children, Condition.TrueCondition)[0]
                                        .FindAll(TreeScope.Children, Condition.TrueCondition);

            var closeButton = lastItems[3].GetCurrentPattern(AutomationPattern.LookupById(InvokePattern.Pattern.Id)) as InvokePattern;
            closeButton.Invoke();
        }
    }
}

要使用此功能,需要app.manifest才能在提升模式下运行:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    </application>
  </compatibility>
</asmv1:assembly>