Henrique Breda
Henrique Breda

Reputation: 11

VS2012 Coded UI - CloseOnPlaybackCleanup doesn't work with ApplicationUnderTest

Executed a test in VS2012 and during the execution, the script called a program (Notepad). I need this program doesn't close when a test finished. Someone help me with this problem please? Sample code:

  [TestMethod]
  public void TestVoid()
  {
  }

  [TestInitialize]
  public void MyTestInitialize()
  {
        Process[] processes = Process.GetProcessesByName("Notepad");
        if (processes.Length > 0)
        {
              _app = ApplicationUnderTest.FromProcess(processes[0]);
        }
        else
        {
              _app = ApplicationUnderTest.Launch(@"C:\Windows\System32\notepad.exe");
              _app.CloseOnPlaybackCleanup = false;
        }
  }

  [TestCleanup]
  public void MyTestCleanup()
  {
  }

Upvotes: 1

Views: 976

Answers (3)

Avinash Pande
Avinash Pande

Reputation: 1538

  • BrowserWindow and ApplicationUnderTest class has one property named as CloseOnPlaybackCleanup.
    When CloseOnPlaybackCleanup=false the browser/application stays open -when launched using BrowserWindow.Launch. or ApplicationUnderTest.Launch("String path/path to .exe").

    Example:
    AUT = ApplicationUnderTest.Launch(@"C:\Program Files (x86)\SIMS\FMSSQL\Finance.exe"); AUT.CloseOnPlaybackCleanup = false;

Upvotes: 1

JL.
JL.

Reputation: 81342

On way to do this is to ensure the application is running prior to actually starting a test run.

This can be done in numerous ways, but if you're kicking things off from a labs environment use this:

enter image description here

Then you can set your application under launch from process like this:

public static ApplicationUnderTest LaunchApplicationUnderTest(string applicationPath,
            bool closeOnPlaybackCleanup)
        {
            Process[] processes = Process.GetProcessesByName("Notepad");

            if (processes.Length > 0)
            {
               /// You can also launch app here using standard .net launching techniques
                _application = ApplicationUnderTest.FromProcess(processes[0]);
            }
            else
            {
                _application = ApplicationUnderTest.Launch(applicationPath);
                _application.CloseOnPlaybackCleanup = closeOnPlaybackCleanup;
            }

            return _application;
        }

Personally I would just rule out the script and launch application where I've commented - change the function a bit then always get application FromProcess.

The logic is that if the app was already open it will remain open if CloseOnPlaybackCleanup is set to false.

Also keep the Application under test as a global static.

Upvotes: 0

Ryan Cox
Ryan Cox

Reputation: 958

I like my ApplicationUnderTest to be a class or global variable to be used from start to finish. So, I create the variable here:

public static class GlobalVariables
{
    public static ApplicationUnderTest App;
}

And then I initialize it in my TestInitialize():

[CodedUITest]
public class WinFormTests
{
    [TestInitialize()]
    public void MyTestInitialize()
    {
         GlobalVariable.App = ApplicationUnderTest.Launch(@"C:\RyansConjobulator.exe");
    }

    [TestMethod]
    public void TextBoxValueToResultField()
    {
        Keyboard.SendKeys(TextBoxInput, "blah blah blah");
        Mouse.Click(textButton.TextBoxButtonInput);
        Assert.IsTrue(resultEdit.ResultEdit.DisplayText.Contains("blah blah blah"));
    }

}

Now, I can access it throughout my test and clean it up at the end:

 [TestCleanup()]
 public void MyTestCleanup()
 {
     app.Close();
 }

Upvotes: 0

Related Questions