How do I open the browser after finishing testing the encoded ui?
I am using Visual Studio 2012 Coded UI tests for a web application. I have a test to log into an application that launches a browser, finds a login dialog, enters credentials, and then clicks OK. I have an assertion that validates the correct url after login. This test works correctly. My problem is that it closes the browser after running the test. I need to keep the browser open so I can run the next test in my sequence. How to do it?
At the moment I don't have anything in the [TestCleanup ()] section. I'm guessing what I'm looking for goes here, but so far I haven't had enough luck figuring out what it should be.
source to share
I don't have original source where I found this solution :( You may have a method similar to the one below. This method needs to be called in TestSetup. Also declare a class level variable _browserWindow from tyep BrowserWindow
private void SetBrowser()
{
if(_browserWindow == null)
{
BrowserWindow.CurrentBrowser = "ie";
_browserWindow = BrowserWindow.Launch("http://www.google.com");
_browserWindow.CloseOnPlaybackCleanup = false;
_browserWindow.Maximized = !_browserWindow.Maximized;
}
else
{
BrowserWindow.CurrentBrowser = "ie";
_browserWindow = BrowserWindow.Locate("Google");
_browserWindow.Maximized = !_browserWindow.Maximized;
}
}
source to share
Ok, so I needed to run and log in before each test. I thought I wanted to start the browser and login first, and then every additional test. After reading more, I decided that I really wanted this logic to run as initialization code for each test. I did this by adding this code to the default [TestInitialize ()] generated when I started the coded ui project in Visual Studio 2012.
source to share
I found the following method to work with my coded user interface in Visual Studio 2015.
You will want to use [ClassInitialize] and open up your browser and point it according to where your [TestMethod] starts.
Use [ClassCleanup] to free up resources after all methods in the test class have been executed.
You can redirect test methods differently after initializing the class with the [TestInitialize] test and cleaning up with [TestCleanup]. Be careful with these because they will occur for every test method, and if it closes your browser instance, your next test will fail.
private static BrowserWindow browserWindow = null;
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Playback.Initialize();
browserWindow = BrowserWindow.Launch(new Uri("http://198.238.204.79/"));
}
[ClassCleanup]
public static void TestCleanup()
{
browserWindow.Close();
Playback.Cleanup();
}
source to share