TestStack.White 和 CCleaner

TestStack.White and CCleaner

我对 TestStack(白色)UI 自动化库还很陌生,我在 "hooking" 过程方面遇到了一些问题。我正在尝试连接 CCleaner,但我一直收到

An unhandled exception of type 'TestStack.White.AutomationException' occurred in TestStack.White.dll

Additional information: Couldn't find window with title Piriform CCleaner in process 1156, after waiting for 30 seconds:

我当前的代码是:

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";但这引发了完全相同的异常。

我正在使用 Microsoft 的 Inspect,这就是它为我提供的功能(大图): Inspect's information。知道我在这里做错了什么吗?

问题是 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));
}