How do I keep the browser open after a coded ui test finishes?

橙三吉。 提交于 2019-12-20 01:58:12

问题


I'm using Visual Studio 2012 Coded UI tests for a web application. I have a test for logging into the app which starts the browser, locates the login dialogue, enters credentials, and then clicks ok. I have an assertion which checks for the correct url after the login. This test appears to function correctly. My problem is that it closes the browser after the test runs. I need to keep the browser open, so I can run the next test in my sequence. How do I do this?

At the moment, I don't have anything in my [TestCleanup()] section. I'm assuming that what I'm looking for goes here, but so far I haven't had a lot of luck figuring out what that is supposed to be.


回答1:


I don't have the original source where I found this solution :( You can have a method like the one showed below. This method needs to be called in TestSetup. Also declare a class level variable _browserWindow of the 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;
        }

    }



回答2:


Ok, so what I needed to have happen was the launch and login before each test. I thought what I wanted was to run the browser and login test first, and then each additional test. After reading more, I've decided what I actually wanted was to run this logic as initialization code for each test. I've done that by adding this code to the default [TestInitialize()] generated when I started the coded ui project in Visual Studio 2012.




回答3:


I have found the following method to work for my data driven coded UI test in Visual Studio 2015.

You will want to use [ClassInitialize] and get your browser open and direct it according to where your [TestMethod] begins.

Use [ClassCleanup] to release the resources after all the methods in the test class have been executed.

You can redirect test methods different after the class has been initialized by using the [TestInitialize] and clean-up test using the [TestCleanup]. Be careful with those though because they will occur for each test method and if it closes your browser instance your following 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();
}


来源:https://stackoverflow.com/questions/14426202/how-do-i-keep-the-browser-open-after-a-coded-ui-test-finishes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!