使用CodedUITest方法从快捷方式执行.exe

本文关键字:执行 exe 快捷方式 CodedUITest 方法 使用 | 更新日期: 2023-09-27 18:12:18

我试图添加一些东西到我的一个测试,但我不能成功。基本上我的测试是进入桌面,双击。exe文件,然后关闭它。它可以工作,但是我需要从一个快捷方式执行。exe文件,例如,我的桌面上有这个。exe文件的快捷方式,我想访问它。

这是我的。cs代码(它的一部分),它启动我的uimap方法

[TestMethod]
    public void LaunchPadTestMethod()
    {
        // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
        this.UIMap.LaunchPadOpen_Close();
    }

这是UImap。cs方法

public partial class UIMap
    {

        /// <summary>
        /// LaunchPadOpen_Close - Use 'LaunchPadOpen_CloseParams' to pass parameters into this method.
        /// </summary>
        public void LaunchPadOpen_Close()
        {
            #region Variable Declarations
            WinButton uIExitButton = this.UILoginWindow.UIExitWindow.UIExitButton;
            #endregion
            // Launch '%USERPROFILE%'Desktop'financialApp'financialApp'bin'Debug'financialApp.exe'
            ApplicationUnderTest uILoginWindow = ApplicationUnderTest.Launch(this.LaunchPadOpen_CloseParams.UILoginWindowExePath, this.LaunchPadOpen_CloseParams.UILoginWindowAlternateExePath);
            // Click 'Exit' button
            Mouse.Click(uIExitButton, new Point(39, 16));
        }
        public virtual LaunchPadOpen_CloseParams LaunchPadOpen_CloseParams
        {
            get
            {
                if ((this.mLaunchPadOpen_CloseParams == null))
                {
                    this.mLaunchPadOpen_CloseParams = new LaunchPadOpen_CloseParams();
                }
                return this.mLaunchPadOpen_CloseParams;
            }
        }
        private LaunchPadOpen_CloseParams mLaunchPadOpen_CloseParams;
    }
    /// <summary>
    /// Parameters to be passed into 'LaunchPadOpen_Close'
    /// </summary>
    [GeneratedCode("Coded UITest Builder", "12.0.21005.1")]
    public class LaunchPadOpen_CloseParams
    {

        public  class GestCollURL
        {
            //------------------------------------------Class for getting current URL--------------------
            public static String getCurrentGestCollUrl()
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                //MessageBox.Show(path);
                path = path + @"'financialApp.exe";
                // MessageBox.Show(path);
                return path;
            }
            //--------------------------------------------------------------------------------------------
        }
        #region Fields
        /// <summary>
        /// Launch '%USERPROFILE%'Desktop'financialApp'financialApp'bin'Debug'financialApp.exe'
        /// </summary>
        // ORIGINAL !!!!!!!!!!!!!!!!!
        //public string UILoginWindowExePath = "C:''Users''ExtremeSwat''Desktop''financialApp''financialApp''bin''Debug''financialApp.exe" +
        //    "";

        public string UILoginWindowExePath =  GestCollURL.getCurrentGestCollUrl()+
            "";
        /// <summary>
        /// Launch '%USERPROFILE%'Desktop'financialApp'financialApp'bin'Debug'financialApp.exe'
        /// </summary>
        ///
        //ORIGINAL!!!!!!!!!!!!!!!!!!
       // public string UILoginWindowAlternateExePath = "%USERPROFILE%''Desktop''financialApp''financialApp''bin''Debug''financialApp.exe";
        public string UILoginWindowAlternateExePath = GestCollURL.getCurrentGestCollUrl() + "";
        #endregion
}
}

我已经创建了一个类,其中我有一个静态方法,我找到当前的桌面URL,然后我连接我需要的可执行文件。它可以工作,但我需要执行快捷方式,例如financialApp.exe

的快捷方式

编辑1

将。exe更改为。ink或不更改将导致测试失败

编辑3 -最终工作版本,感谢西蒙的建议这个代码块动态生成我想要的url
 public class GestCollURL
    {
        public static String getCurrentGestCollUrl()
        {
          string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            //MessageBox.Show(path);
            path = path + @"'financialApp.lnk";
            return path;
        }
    }
这段代码重载了Launch方法
public static ApplicationUnderTest Launch(ProcessStartInfo startInfo)
        {
            ApplicationUnderTest abba = new ApplicationUnderTest();
            Process launchPad = new Process();
            launchPad.StartInfo.UseShellExecute = startInfo.UseShellExecute;
            launchPad = Process.Start(TestingStuff.LaunchPadOpen_CloseParams.GestCollURL.getCurrentGestCollUrl());
            return abba;
        }

我就是这么称呼它的

(in the same class)
ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = true;
            Launch(startInfo);

使用CodedUITest方法从快捷方式执行.exe

你应该使用这个Launch overload: ApplicationUnderTest。启动方法(ProcessStartInfo)。它允许您精确地指定您想要如何启动被测应用程序。

只要确保你将UseShellExecute设置为true,因为链接和快捷方式是shell(资源管理器)对象,不能像普通的ex那样启动。

当您使用操作系统shell启动进程时,您可以启动任何文档(它是与关联的任何注册文件类型)(具有默认打开操作的可执行文件)并执行操作通过使用Process对象对文件进行处理,例如打印。当参数UseShellExecute为false,则只能启动可执行文件进程对象。

它可以只使用简单的ProcessStartInfo构造函数,以路径作为参数,这取决于您的上下文。