C# SUT无法在编码的测试UI中保存文件

C# SUT无法在编码的测试UI中保存文件,c#,winforms,coded-ui-tests,C#,Winforms,Coded Ui Tests,我是visual studio 2013中编码ui测试的新手 XmlReaderSettings settings = new XmlReaderSettings(); string executableFolderPath = Environment.CurrentDirectory; string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;

我是visual studio 2013中编码ui测试的新手

            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);
我正在测试程序的一部分,它读取文件、执行操作并保存该文件。对于手动测试,没有问题。通过编码的ui测试,程序可以读取文件,但无法保存

            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);
存在非托管异常System.IO.IOException:进程无法访问文件“…\tempData.xml”,因为另一个进程正在使用该文件

            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);
当我在“异常”框中单击“继续”并重新单击保存文件的按钮时,将不再出现异常,并保存文件。很奇怪

            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);
Tt就好像我的测试在SUT使用该文件时访问了该文件

            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);
我的测试:

        [TestInitialize]
        public void RunApp()
        {
            _sut = ApplicationUnderTest.Launch(Settings.Default.pathSut);
        }


        [TestMethod]
        public void CompleteExport()
        {
            this.UiMap.ExportDataComplete(); //Read and save the file
            this.UIMap.AssertSuccessExport();
        }
            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);
以及我保存文件的方法:

public void TrySave()
{

string executableFolderPath = Environment.CurrentDirectory;
string path = executableFolderPath + "\\" + Resources.xmlFile;
bool result = this.ValidateXml();
if (result)
    XmlFic.Save(path);
}
            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);
我如何解决这个问题?

我的错:

            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);
我忘了:

            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);
reader.Close();
奇怪的行为,为什么它在手动测试中工作?也许GC有时间处理它,而没有时间进行自动化测试?问题解决了,但如果有人有解释

            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);

谢谢

您需要使用代码片段提供更多信息,解释如何访问和保存文件。执行操作意味着什么?正在测试的应用程序是否正在修改文件内容?另外,生成异常的位置是应用程序还是Visual Studio在运行编码的UI测试时生成的?@AdrianHHH是应用程序修改文件并保存它。例外情况来自下的系统test@CarbineCoder我已经编辑了这篇文章。但我不知道这是否有助于解决问题,因为它在手动测试中运行良好。AssertSuccessExport做什么?它是否查看正在创建的文件?如果是这样,则故障可能是编码UI在保存保存时正在访问文件,即编码UI和应用程序之间存在竞争条件。
            XmlReaderSettings settings = new XmlReaderSettings();
            string executableFolderPath = Environment.CurrentDirectory;
            string pathXsd = executableFolderPath + "\\" + Resources.pathXsd;
            string pathXml = executableFolderPath + "\\" + Resources.xmlFile;
            settings.Schemas.Add("", pathXsd);
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(pathXml, settings);
            XmlFic = new XmlDocument();
            XmlFic.Load(reader);