Title: SUT cannot save file in coded test UI
Tags: c#;winforms;coded-ui-tests
Question: I'm new to coded ui tests in visual studio 2013.
I'm trying to test a part of program which read a file, perform operations, and save that file. With manual tests, there are no problems. With coded ui test, the program can read the file but cannot save it.
There is an unmanaged exception "System.IO.IOException: the process cannot access the file '...\tempData.xml', because it is being used by another process."
When I click "continue" in the exception box, and i re-click on the button which save the file, there is no more exception and the file is saved. Very weird.
Tt's as if my test accessed the file while it is used by the SUT
My test:
[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();
}
And my method which save the file:
public void TrySave()
string executableFolderPath = Environment.CurrentDirectory;
string path = executableFolderPath + "\\" + Resources.xmlFile;
bool result = this.ValidateXml();
if (result)
XmlFic.Save(path);
How i resolve that?
Comment: What does "perform operations" mean? Is the application being tested modifying the files contents? Also, where is the exception being generated, it is from the application or from Visual Studio as it runs the Coded UI test?

Comment: What does `AssertSuccessExport` do? Does it look at the file that is being created? If so then the fault may be that Coded UI is accessing the file while the save is being saved, ie a race condition between Coded UI and the application.

Comment: @AdrianHHH Yes the application modify the file and save it. The exception is from the system under test

Comment: @CarbineCoder I've edit the post. But i don't know if that can help to solve the problem, because it works fine in manual tests.

Comment: @AdrianHHH The exception is generated before. And it verify the name of window which is show after.

Comment: @CarbineCoder 'XmlFic.Save(path)' saves in the default bin folder. And i've permissions to save there. The question is that : why it works in manual tests and not in automated tests? Using the same compilated file.

Comment: You need to provide more info with code-snippet which explains how you are accesing and saving the file.

Comment: Creating and saving files in unit test is a bad idea. Also check which path `XmlFic.Save(path)` saves to? It will be different from the default bin folder, then check if you have enough permission given for modifying the folder under that location.

Here is the accepted answer: My bad :
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);
I forget :
reader.Close();
Weird behavior, why it was working in manual tests? Maybe the GC had time to dispose it, and doesn't have the time in automated tests? Problem solved, but if anyone has an explanation...
Thanks


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 8, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:03/16/15', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<c#><winforms><coded-ui-tests>', 'Title': 'SUT cannot save file in coded test UI', 'ViewCount': 159, 'id': 29074046, 'user_AboutMe': '', 'user_AccountId': 5884052.0, 'user_DisplayName': 'ZwoRmi', 'user_Reputation': 1053.0, 'user_Views': 98.0, 'user_id': 4632606.0}}