如何使用Coded UI执行回归测试时清除Cache \ session数据

时间:2013-08-30 06:07:25

标签: visual-studio-2010 coded-ui-tests

我在VS2010上使用Coded UI关键字驱动框架来运行我的Web应用程序回归测试套件。最初我记录了所有操作并为每个页面创建了不同的测试方法,然后将它们作为关键字驱动。

我面临的问题是,如果浏览器中的会话或缓存中有关于用户的最新未保存信息,则会在主页上显示弹出消息。

我正在使用解决方法/替代方案

  1. 在每个测试代理上手动启动测试运行之前清除缓存。
  2. 每次单次迭代完成后关闭浏览器窗口。
  3. 这种方法需要付出很多努力,并且在测试失败时会有一些时间会话数据仍然会导致所有后续测试用例失败。任何程序化的方法\建议将不胜感激。

1 个答案:

答案 0 :(得分:2)

清除TestInitialize中的缓存,以便在任何测试开始后,第一步是清除缓存然后继续测试。

    #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:我编辑了TestInitialize以首先启动浏览器。在ClearCache和ClearCookies做任何事情之前,浏览器必须打开。