Page 1 of 1

Calling the Ranorex API inside the Proc you are testing

Posted: Fri May 11, 2012 2:21 pm
by KevinBoyleRG
Hi,

We use Ranorex for doing some long running integration style testing where we start our application from within our NUnit test fixture (using Process.Start) and then continue in the test to use the Ranorex API to test things in the process that we have just started. This works fine but can take a while to run as our application has a dependency on databases etc.

What we had hoped to do was be able to test individual windows directly within an NUnit test without needing to start our application as another process.

This doesn't seem to be working, and I am not sure why. Is this something that is supported by the Ranorex API or does it always need to be automating an external process. I can attach a sample solution if needed, but basically our tests are currently of the form:

Code: Select all

[Test, STAThread]
        public void Test()
        {
            var userControl1 = new UserControl1();

            userControl1.Loaded += (sender, args) =>
                {
                    Task.Factory.StartNew(() =>
                    {
                        foreach (Form unknown in Host.Local.FindChildren<Form>())
                        {
                            Trace.WriteLine(unknown.Title);
                        }

                        Trace.WriteLine("TITLE OF PID" + Host.Local.GetApplicationForm(Process.GetCurrentProcess()).Title);
                    });
                };
            Application app = new Application();
            app.Run(userControl1);
        }

Can the Ranorex API be used in this way?

Re: Calling the Ranorex API inside the Proc you are testing

Posted: Fri May 11, 2012 4:15 pm
by KevinBoyleRG
Interestingly, I have discovered that this works fine with White (http://white.codeplex.com/), so this shows that WPF UIAutomation can do it, still struggling to get Ranorex to work with this though.

Re: Calling the Ranorex API inside the Proc you are testing

Posted: Mon May 14, 2012 2:28 pm
by Support Team
This works with Ranorex, too. You just have to ensure that the thread executing the Ranorex code is using the STA ApartmentState - which is not the default for Task threads. And you need to instruct Ranorex to not filter the current process, which is filtered by default.

To do that you can either create your own thread instead of using tasks...
Ranorex.Core.PluginManager.Instance.LoadDefaultPlugins();
Ranorex.Plugin.Win32Flavor.Instance.RemoveFilteredProcess(Process.GetCurrentProcess().Id);

var thread = new Thread(() =>
...);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
...or use the StaTaskScheduler included in the ParallelExtensionsExtras package:
http://blogs.msdn.com/b/pfxteam/archive ... 90421.aspx
http://code.msdn.microsoft.com/ParExtSamples

Regards,
Alex
Ranorex Team

Re: Calling the Ranorex API inside the Proc you are testing

Posted: Mon May 14, 2012 3:12 pm
by KevinBoyleRG
Hi Alex,

Thanks for the reply. I tried setting the thread's apartment state to be STA and I see no difference in behaviour.

I am happy to attach the sample solution that I have created that demonstrates this issue. I am using Ranorex 3.1.0 if that makes any difference.

Thanks,
Kevin

Re: Calling the Ranorex API inside the Proc you are testing

Posted: Mon May 14, 2012 3:45 pm
by Support Team
Sorry, I forgot to add two code lines. The current process is by default filtered by Ranorex that's why you need to instruct Ranorex to not filter the current process. Add the following two lines before starting the thread:
Ranorex.Core.PluginManager.Instance.LoadDefaultPlugins();
Ranorex.Plugin.Win32Flavor.Instance.RemoveFilteredProcess(Process.GetCurrentProcess().Id);
Regards
Alex
Ranorex Team

Re: Calling the Ranorex API inside the Proc you are testing

Posted: Mon May 14, 2012 4:24 pm
by KevinBoyleRG
Hey Alex,

Just tried that and it works great. Thanks a lot for the help!

Thanks,
Kevin