Search code examples
c#white-framework

TestStack.White and CCleaner


I've very new to the TestStack (White) UI Automation library and I'm having a bit of an issue in terms of "hooking" the process. I'm trying to hook CCleaner, but I keep getting

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:

My current code is:

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 "";
        }
    }
}

I thought that maybe it was the name of the process since CCleaner starts another process (not CCleaner.exe) but CCleaner64.exe as seen here, which I can assume is for 64 bit operating systems maybe? Anyway I tried names including: "CCleaner", "CCleaner64"; but this threw the same exact exception.

I'm using Inspect by Microsoft and this is what it pulls for me (large image): Inspect's information. Any idea what I'm doing wrong here?


Solution

  • The problem is that CCleaner is visible as WIN32 app. So GetWindow() doesn't work. You can try this code:

    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));
    }