Search code examples
visual-studio-2010coded-ui-tests

How to clear Cache\session data while performing regression test using Coded UI


I am using Coded UI Keyword driven framework on VS2010 to run my regression test suite for a web application. Initially i recorded all the actions and created different test methods for each page and then made them keyword driven.

The problem i face is that on homepage there is pop up message that is displayed if the session or cache in browser has any last unsaved information about the user.

I am using a workaround/alternative

  1. Clear cache before test run is started manually on each of test agents.
  2. Closing the browser window each time single iteration is complete.

This approach i staking a lot of effort and some time when test fails the session data remains which leads to failure of all subsequent test case failure. Any programmatic approach\advise would be appreciated.


Solution

  • Clear the cache in TestInitialize, so that as soon as any test starts, the first step is to clear the cache then proceed with the testing.

        #region Additional test attributes
    
    
        //Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
        // First launch your browser...
            this.UIMap.LaunchBrowserParams.Url = Config.WebServer;
            this.UIMap.LaunchBrowser();
        // This will clear your cache and cookies
            BrowserWindow.ClearCache();
            BrowserWindow.ClearCookies();
        }
    
        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup()
        {
        // Since you're concerned about failed tests not clearing cache at the end
        // I wouldn't bother with clearing the cache at the MyTestCleanup step.
            this.UIMap.CloseBrowser();
    
        }
    
        #endregion
    

    EDITED: I edited the TestInitialize to launch the browser first. The browser has to be open before ClearCache and ClearCookies will do anything.