From 630a146ad6cae055a4c9241ce4deff651f2b438d Mon Sep 17 00:00:00 2001 From: Bartosz Glubiak Date: Wed, 21 Feb 2018 14:21:08 +0100 Subject: [PATCH] Read test data from CSV file --- .../DataDriven/DataDrivenHelper.cs | 83 ++++++++++++++++++- .../DataDriven/TestData.cs | 15 +++- .../DataDriven/TestDataCsv.csv | 3 + ...ctivity.Test.Automation.Tests.NUnit.csproj | 3 + .../Tests/HerokuappTestsDataDrivenNUnit.cs | 15 ++++ 5 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 Objectivity.Test.Automation.Tests.NUnit/DataDriven/TestDataCsv.csv diff --git a/Objectivity.Test.Automation.Tests.NUnit/DataDriven/DataDrivenHelper.cs b/Objectivity.Test.Automation.Tests.NUnit/DataDriven/DataDrivenHelper.cs index 85f82bc9d..a98ca466b 100644 --- a/Objectivity.Test.Automation.Tests.NUnit/DataDriven/DataDrivenHelper.cs +++ b/Objectivity.Test.Automation.Tests.NUnit/DataDriven/DataDrivenHelper.cs @@ -97,6 +97,87 @@ public static IEnumerable ReadDataDriveFile(string folder, string } } + /// + /// Reads the Csv data drive file and set test name. + /// + /// Full path to Excel DataDriveFile file + /// The difference parameter. + /// Name of the test, use as prefix for test case name. + /// + /// IEnumerable TestCaseData + /// + /// Exception when wrong cell type in file + /// Exception when parameter not found in row + /// How to use it: + /// { + /// var a = TestContext.CurrentContext.TestDirectory; + /// a = string.Format(CultureInfo.CurrentCulture, "{0}{1}", a, @"\DataDriven\TestDataCsv.csv"); + /// return DataDrivenHelper.ReadDataDriveFileCsv(a, new[] { "user", "password" }, "credentialCsv"); + /// } + /// + public static IEnumerable 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[] 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; + } + } + } + } + /// /// Reads the data drive file without setting test name. /// @@ -250,5 +331,5 @@ private static string TestCaseName(string[] diffParam, Dictionary /// DataDriven methods for NUnit test framework @@ -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"); } } } diff --git a/Objectivity.Test.Automation.Tests.NUnit/DataDriven/TestDataCsv.csv b/Objectivity.Test.Automation.Tests.NUnit/DataDriven/TestDataCsv.csv new file mode 100644 index 000000000..0b9a595a1 --- /dev/null +++ b/Objectivity.Test.Automation.Tests.NUnit/DataDriven/TestDataCsv.csv @@ -0,0 +1,3 @@ +user;password;message +test111;test222;Your username is invalid! +tomsmith;SuperSecretPassword!;You logged into a secure area! \ No newline at end of file diff --git a/Objectivity.Test.Automation.Tests.NUnit/Objectivity.Test.Automation.Tests.NUnit.csproj b/Objectivity.Test.Automation.Tests.NUnit/Objectivity.Test.Automation.Tests.NUnit.csproj index 91ddf90aa..4b74ce28b 100644 --- a/Objectivity.Test.Automation.Tests.NUnit/Objectivity.Test.Automation.Tests.NUnit.csproj +++ b/Objectivity.Test.Automation.Tests.NUnit/Objectivity.Test.Automation.Tests.NUnit.csproj @@ -115,6 +115,9 @@ Designer + + Always + Always diff --git a/Objectivity.Test.Automation.Tests.NUnit/Tests/HerokuappTestsDataDrivenNUnit.cs b/Objectivity.Test.Automation.Tests.NUnit/Tests/HerokuappTestsDataDrivenNUnit.cs index d022992e2..14d71f201 100644 --- a/Objectivity.Test.Automation.Tests.NUnit/Tests/HerokuappTestsDataDrivenNUnit.cs +++ b/Objectivity.Test.Automation.Tests.NUnit/Tests/HerokuappTestsDataDrivenNUnit.cs @@ -62,6 +62,21 @@ public void FormAuthenticationPageExcelTest(IDictionary paramete () => Assert.AreEqual(parameters["message"], formFormAuthentication.GetMessage)); } + [Test] + [TestCaseSource(typeof(TestData), "CredentialsCSV")] + public void CSVTest(IDictionary 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")]