TestStack.White和CCleaner

时间:2016-03-11 20:47:29

标签: c# white

我对TestStack(白色)UI自动化库非常陌生,我在“挂钩”这个过程方面遇到了一些问题。我正试图挂钩CCleaner,但我一直在

  

“TestStack.White.AutomationException”类型的未处理异常   发生在TestStack.White.dll

中      

其他信息:无法找到标题为Piriform的窗口   在等待30秒后,CCleaner在流程1156中:

我目前的代码是:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using TestStack.White;
using TestStack.White.Factory;
using TestStack.White.UIItems.Finders;
using TestStack.White.InputDevices;
using TestStack.White.UIItems.WindowItems;

namespace NightWipe
{
    class Program
    {
        private const string ExeSourceFile = @"C:\Program Files\CCleaner\CCleaner.exe";
        private static TestStack.White.Application _application;
        private static TestStack.White.UIItems.WindowItems.Window _mainWindow;

        static void Main(string[] args)
        {
            clean();
        }

        public static string clean()
        {
            var psi = new ProcessStartInfo(ExeSourceFile);
            _application = TestStack.White.Application.AttachOrLaunch(psi);

            _mainWindow = _application.GetWindow("Piriform CCleaner");
            _mainWindow.WaitWhileBusy();


            return "";
        }
    }
}

我想也许是因为CCleaner启动了另一个进程(不是CCleaner.exe)而是CCleaner64.exe看到here这个过程的名称,我可以假设是64位操作系统?无论如何,我尝试过的名字包括:“CCleaner”,“CCleaner64”;但是这引起了同样的例外。

我正在使用微软的Inspect,这是它为我带来的(大图):  Inspect's information。知道我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

问题是CCleaner作为WIN32应用程序可见。所以 GetWindow()不起作用。您可以尝试以下代码:

    public void CCleanerSample()
    {        
        var application = Application.AttachOrLaunch(new ProcessStartInfo(@"C:\Program Files\CCleaner\CCleaner.exe"));

        AutomationElement ccleanerAutomationElement = null;

        Console.Write("Waiting till WIN32 app is launching");
        while (ccleanerAutomationElement == null)
        {
            ccleanerAutomationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children,
                new PropertyCondition(AutomationElement.NameProperty, "Piriform CCleaner"));
            Thread.Sleep(1000);
            Console.Write(".");
        }
        Console.WriteLine(" Done");

        var mainWindow = new Win32Window(ccleanerAutomationElement, WindowFactory.Desktop, InitializeOption.NoCache,
            new WindowSession(application.ApplicationSession, InitializeOption.NoCache));
    }