Reputation: 3
I am looking for a way to be able to run the same coded ui test class with different input files, e.g. I have a test with end to end flow in the app, I want to be able to run this test with two different users doing different workflows once inside the app. I do not want to run both the tests each time (This is possible with having two rows in the data input csv). I wasn't able to find a way of doing this so far. Any help/guidance is appreciated.
Upvotes: 0
Views: 2830
Reputation: 1507
If it is a same test case then you should have same set of input parameters, create a class with your test arguments then serialize the list of class instance to XML file with different argument set. When running the test case you can deserialize the XML file (inside TestInitialize() block) and iterate though each class instance and pass the instance coded UI test method. you can call the test method as many time you want according to the count of class instances you have in the xml file. This is the method which I am using for the data driven testing on coded UI test.
Create a class with the test arguments
public class ClientDetails
{
public String ClientType { get; set; }
public String clientCode { get; set; }
public String Username { get; set; }
public String Password { get; set; }
}
Create some class instances and first serialize to a XML file
// location to store the XML file following relative path will store outside solution folder with
// TestResults folder
string xmlFileRelativePath = "../../../TestClientInfo.xml";
public List<ClientDetails> ListClientConfig = new List<ClientDetails>();
ClientDetails Client1 = new Classes.ClientDetails();
Client1.ClientType = "Standard";
Client1.clientCode = "xxx";
Client1.Username = "username";
Client1.Password = "password";
ClientDetails Client2 = new Classes.ClientDetails();
Client2.ClientType = "Easy";
Client2.clientCode = "xxxx";
Client2.Username = "username";
Client2.Password = "password";
ListClientConfig.Add(Client1);
ListClientConfig.Add(Client2);
XmlSerialization.genericSerializeToXML(ListClientConfig, xmlFileRelativePath );
Retrieve the stored XML objects within the test method or any where you prefer (better if inside inside TestInitialize() block)
[TestMethod]
public void CommonClientExecution()
{
List<ClientDetails> ListClientConfig = XmlSerialization.genericDeserializeFromXML(new ClientDetails(), xmlFileRelativePath );
foreach (var ClientDetails in ListClientConfig )
{
// you test logic here...
}
}
XML Serialization methods for serialize collection of objects
using System.Xml;
using System.Xml.Serialization;
class XmlSerialization
{
public static void genericSerializeToXML<T>(T TValue, string XmalfileStorageRelativePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
FileStream textWriter = new FileStream((string)System.IO.Path.GetFullPath(XmalfileStorageRelativePath), FileMode.OpenOrCreate, FileAccess.ReadWrite);
serializer.Serialize(textWriter, TValue);
textWriter.Close();
}
public static T genericDeserializeFromXML<T>(T value, string XmalfileStorageFullPath)
{
T Tvalue = default(T);
try
{
XmlSerializer deserializer = new XmlSerializer(typeof(T));
TextReader textReader = new StreamReader(XmalfileStorageFullPath);
Tvalue = (T)deserializer.Deserialize(textReader);
textReader.Close();
}
catch (Exception ex)
{
// MessageBox.Show(@"File Not Found", "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
return Tvalue;
}
}
Upvotes: 0
Reputation: 14076
I can think of three possibilities.
1.
You could arrange the CSV to have two groups of columns, eg
UserName1,Password1,DataAa1,DataBb1,UserName2,Password2,DataAa2,DataBb2
Within the test method change the data source accesses to use something like
string testCode = (...) ? "1" : "2";
... = TestContext.DataRow["UserName" + testCode].ToString();
... = TestContext.DataRow["Password" + testCode].ToString();
This requires something else to specify which data file to use. That could be done via an environment variable.
2.
Have three CSV files within the solution. Two of them are the CSV files for the two runs. For example SourceData1.csv
and SourceData2.csv
. The third file is SourceData.csv
and is named in the [DataSource(...)
attribute as "|DataDirectory|\\SourceData.csv"
. In the ".testsettings" file give the name of a batch file that chooses the wanted SourceData1.csv
or SourceData2.csv
file and uses xcopy
to copy that file and overwrite SourceData.csv
.
3.
Assuming that the test is currently written as
[TestMethod, DataSource(...)]
public void MyCodedUiTestMethod() {
...body of the test
}
Then change to having two test methods that call a third method. These two methods specify different CSV files and the called method accesses values from the whichever file is being read.
[TestMethod, DataSource(... SourceData1.csv ...)]
public void MyFirstCodedUiTestMethod() {
BodyOfTheTest();
}
[TestMethod, DataSource(... SourceData2.csv ...)]
public void MySecondCodedUiTestMethod() {
BodyOfTheTest();
}
public void BodyOfTheTest() {
...body of the test
... = TestContext.DataRow["UserName"].ToString();
... = TestContext.DataRow["Password"].ToString();
}
Note that TextContext
is visible in all methods of the class hence the TestContext.DataRow...
expressions can be written outside the methods that specify the [DataSource...]
attribute.
Upvotes: 1