Calling the Ranorex API inside the Proc you are testing

Class library usage, coding and language questions.
KevinBoyleRG
Posts: 4
Joined: Fri May 11, 2012 2:12 pm

Calling the Ranorex API inside the Proc you are testing

Post by KevinBoyleRG » Fri May 11, 2012 2:21 pm

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?

KevinBoyleRG
Posts: 4
Joined: Fri May 11, 2012 2:12 pm

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

Post by KevinBoyleRG » Fri May 11, 2012 4:15 pm

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.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

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

Post by Support Team » Mon May 14, 2012 2:28 pm

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
.
Image

KevinBoyleRG
Posts: 4
Joined: Fri May 11, 2012 2:12 pm

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

Post by KevinBoyleRG » Mon May 14, 2012 3:12 pm

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

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

Post by Support Team » Mon May 14, 2012 3:45 pm

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
.
Image

KevinBoyleRG
Posts: 4
Joined: Fri May 11, 2012 2:12 pm

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

Post by KevinBoyleRG » Mon May 14, 2012 4:24 pm

Hey Alex,

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

Thanks,
Kevin