Skip to content

Commit

Permalink
Read test data from CSV file
Browse files Browse the repository at this point in the history
  • Loading branch information
bglubiak committed Feb 21, 2018
1 parent 28b8b1d commit 630a146
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,87 @@ public static IEnumerable<TestCaseData> ReadDataDriveFile(string folder, string
}
}

/// <summary>
/// Reads the Csv data drive file and set test name.
/// </summary>
/// <param name="file">Full path to Excel DataDriveFile file</param>
/// <param name="diffParam">The difference parameter.</param>
/// <param name="testName">Name of the test, use as prefix for test case name.</param>
/// <returns>
/// IEnumerable TestCaseData
/// </returns>
/// <exception cref="System.InvalidOperationException">Exception when wrong cell type in file</exception>
/// <exception cref="DataDrivenReadException">Exception when parameter not found in row</exception>
/// <example>How to use it: <code>
/// {
/// var a = TestContext.CurrentContext.TestDirectory;
/// a = string.Format(CultureInfo.CurrentCulture, "{0}{1}", a, @"\DataDriven\TestDataCsv.csv");
/// return DataDrivenHelper.ReadDataDriveFileCsv(a, new[] { "user", "password" }, "credentialCsv");
/// }
/// </code></example>
public static IEnumerable<TestCaseData> ReadDataDriveFileCsv(string file, string[] diffParam, string testName)
{
using (var fs = File.OpenRead(file))
using (var sr = new StreamReader(fs))
{
string line = string.Empty;
line = sr.ReadLine(); // naglowek
string[] columns = line.Split(
new char[] { ';' },
StringSplitOptions.None);

var columnsNumber = columns.Length;
var row = 1;

while (line != null)
{
line = sr.ReadLine();
if (line != null)
{
var testParams = new Dictionary<string, string>();

string[] split = line.Split(
new char[] { ';' },
StringSplitOptions.None);

for (int i = 0; i < columnsNumber; i++)
{
testParams.Add(columns[i], split[i]);
}

if (diffParam != null && diffParam.Any())
{
try
{
testName = TestCaseName(diffParam, testParams, testName);
}
catch (DataDrivenReadException e)
{
throw new DataDrivenReadException(
string.Format(
" Exception while reading Excel Data Driven file\n searched key '{0}' not found at sheet '{1}' \n for test {2} in file '{3}' at row {4}",
e.Message,
testName,
testName,
file,
row));
}
}
else
{
testName = testName + "_row(" + row + ")";
}

row = row + 1;

var data = new TestCaseData(testParams);
data.SetName(testName);
yield return data;
}
}
}
}

/// <summary>
/// Reads the data drive file without setting test name.
/// </summary>
Expand Down Expand Up @@ -250,5 +331,5 @@ private static string TestCaseName(string[] diffParam, Dictionary<string, string

return testCaseName;
}
}
}
}
15 changes: 13 additions & 2 deletions Objectivity.Test.Automation.Tests.NUnit/DataDriven/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

namespace Objectivity.Test.Automation.Tests.NUnit.DataDriven
{
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using global::NUnit.Framework;

/// <summary>
/// DataDriven methods for NUnit test framework
Expand All @@ -49,9 +53,16 @@ public static IEnumerable Links
get { return DataDrivenHelper.ReadDataDriveFile(ProjectBaseConfiguration.DataDrivenFile, "links"); }
}

public static IEnumerable LinksExcel
public static IEnumerable LinksExcel()
{
get { return DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "links"); }
return DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "links");
}

public static IEnumerable CredentialsCSV()
{
var a = TestContext.CurrentContext.TestDirectory;
a = string.Format(CultureInfo.CurrentCulture, "{0}{1}", a, @"\DataDriven\TestDataCsv.csv");
return DataDrivenHelper.ReadDataDriveFileCsv(a, new[] { "user", "password" }, "credentialCsv");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user;password;message
test111;test222;Your username is invalid!
tomsmith;SuperSecretPassword!;You logged into a secure area!
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="DataDriven\TestDataCsv.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="DataDriven\DataDriven.xlsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ public void FormAuthenticationPageExcelTest(IDictionary<string, string> paramete
() => Assert.AreEqual(parameters["message"], formFormAuthentication.GetMessage));
}

[Test]
[TestCaseSource(typeof(TestData), "CredentialsCSV")]
public void CSVTest(IDictionary<string, string> parameters)
{
new InternetPage(this.DriverContext).OpenHomePage().GoToFormAuthenticationPage();

var formFormAuthentication = new FormAuthenticationPage(this.DriverContext);
formFormAuthentication.EnterUserName(parameters["user"]);
formFormAuthentication.EnterPassword(parameters["password"]);
formFormAuthentication.LogOn();
Verify.That(
this.DriverContext,
() => Assert.AreEqual(parameters["message"], formFormAuthentication.GetMessage));
}

[Test]
[Category("PhantomJs")]
[TestCaseSource(typeof(TestData), "LinksSetTestName")]
Expand Down

0 comments on commit 630a146

Please sign in to comment.