How do I determine if my application is already running?

Best practices, code snippets for common functionality, examples, and guidelines.
bdenison99
Posts: 2
Joined: Fri Oct 04, 2013 10:35 pm

How do I determine if my application is already running?

Post by bdenison99 » Fri Oct 04, 2013 10:46 pm

We are using Ranorex 4.1.0 with Visual Studio 2010 and .NET 4.0 on a Windows Server 2008 R2 system.

We are trying to generate functional tests which will run through a very fixed process to validate each new build of one of our products. Once the functional tests are completed, we plan to have a long-running job which randomly starts some test cases to emulate what we hope will be more realistic sequences of operations beyond the functional tests. We think that breaking each test case into a separate recording module will make this easier to maintain and increase the flexibility, but it leaves us with an interesting challenge to solve - how to determine if our application is already running, or if it needs to be started before running the next test case.

As an example, consider the following test cases:
A - Launch Application
B - Log in using credential 'X'
C - Task 1
D - Log out
E - Log in using credential 'Y'
F - Task 2
G - Close Application

If our randomization engine decides to start with test case E, and the application is not running yet, we need to start it, otherwise we need to make our application the active / primary application.

Is this possible in Ranorex, or do I need to go figure out the user code to have Windows change which application is active, etc?

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How do I determine if my application is already running?

Post by krstcs » Mon Oct 07, 2013 1:32 pm

The easiest thing to do would be to modify the "Launch" module to be all user-code. You then put an "if" statement around all of it and have the if determine whether the app is running or not. And you probably want to test for "NOT" (use "!" before your test for logical "NOT") running in the if.

Simplistic pseudo-code -

Code: Select all

if (!myApp.IsRunning) {
  <Start app here>
}
You would then just call your start module every time and IT would determine if it needs to do anything.
Shortcuts usually aren't...

bdenison99
Posts: 2
Joined: Fri Oct 04, 2013 10:35 pm

Re: How do I determine if my application is already running?

Post by bdenison99 » Mon Oct 07, 2013 3:49 pm

I'm not clear on where the Launch code is at.

I found this:

void ITestModule.Run()
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;

Init();

Report.Log(ReportLevel.Info, "Application", "Run application '[My application path]' with arguments '' in normal mode.", new RecordItemIndex(0));
Host.Local.RunApplication("[My application path]", "", "", false);
Delay.Milliseconds(100);

Report.Log(ReportLevel.Info, "Delay", "Waiting for 10s.", new RecordItemIndex(1));
Delay.Duration(10000, false);
...

Is this the launcher module you were referring to?

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: How do I determine if my application is already running?

Post by krstcs » Mon Oct 07, 2013 4:26 pm

I was referring to the "Launch Application" recording module you listed in your original post.

In that module, you need to convert the launch actions to user code and then wrap them in the if statement I suggested.

For example, if you were running the GitHub windows application, your "Launch_Application.UserCode.cs" would have a method that looks like:

Code: Select all

public void Recording1_Run_Application()
{
	if (!repo.GitHub.SelfInfo.Exists()) {
		Report.Log(ReportLevel.Info, "Application", "Run application 'GitHub.exe' with arguments '' in normal mode.");
		Host.Local.RunApplication("GitHub.exe", "", "", false);
	}
}

You can look at the Ranorex API here: http://www.ranorex.com/Documentation/Ranorex/
Shortcuts usually aren't...