Find MenuItem fails except in simple console app

Oct 14, 2011 at 4:20 PM

I have the following simple code in a dll:

using White.Core;

namespace notepad_tests {
    public class SimpleTest {
        public void DoIt() {
            var application = Application.Launch(@"C:\Windows\notepad.exe");
                var mainWindow = application.GetWindow("Untitled - Notepad");
                var menuBar = mainWindow.MenuBar;
                var saveAsMenu = menuBar.MenuItem("File", "Save As...");

                System.Threading.Thread.Sleep(1000);
                application.Kill();
        }
    }
}

If I call it from the following simple console app, it works:

using notepad_tests;

namespace TestConsole {
    class Program {
        static void Main(string[] args) {
            var sut = new SimpleTest();
            sut.DoIt();
        }
    }
}

If I call it from this NUnit test, it fails:

using notepad_tests;
using NUnit.Framework;

namespace tests {
    [TestFixture, RequiresSTA] public class nunittest {
        [Test] public void doit() {
            var sut = new SimpleTest();
            sut.DoIt();
        }
    }
}

with this exception:

   Bricks.BricksException : Error invoking MenuBar.MenuItem  ----> White.Core.Factory.UIItemSearchException : Could not find Menu Name=Save As...
   at Bricks.RuntimeFramework.ReflectedObject.Invoke(MethodInfo methodInfo, Object[] arguments)
   at Bricks.DynamicProxy.DynamicProxyInterceptors.Process(IInvocation invocation, InterceptContext interceptedContext)
   at White.Core.Interceptors.CoreInterceptor.Intercept(IInvocation invocation)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.MenuBarProxy.MenuItem(String[] path)
   at notepad_tests.SimpleTest.DoIt() in C:\Users\mikes\Desktop\code\White\source\notepad_tests\SimpleTest.cs:line 9
   at tests.nunittest.doit() in C:\Users\mikes\Desktop\code\White\source\tests\nunittest.cs:line 8
--UIItemSearchException
   at White.Core.UIItems.MenuItems.Menus.Find(SearchCriteria[] path)
   at White.Core.UIItems.MenuItems.Menus.Find(String[] path)
   at White.Core.UIItems.WindowStripControls.MenuBar.MenuItem(String[] path)

This error occurs with NUnit console, NUnit GUI, Resharper test runner and also in fitSharp.

I have tried using ApartmentState STA and /nothread as mentioned elsewhere in this forum with no success.

My environment is Window 7, .NET 4.0