-
Notifications
You must be signed in to change notification settings - Fork 131
DataDriven tests from Xml files
Data driven tests from Xml files are not supported with NUnit by default, but you can use DataDrivenHelper class methods from our Test.Automation framework to read xml files and pass test data to your tests.
<?xml version="1.0" encoding="utf-8"?>
<tests>
<credential user="tomsmith" password="SuperSecretPassword!" message="You logged into a secure area!"/>
<credential user="wronguser" password="wrongpassword" message="Your username is invalid!"/>
<links number="3"/>
</tests>
Make sure that file properties "Copy to Output Directory" is set to "Copy always" and proper path (relative) is set to that file in App.config file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
...
<add key="DataDrivenFile" value="\DataDriven\DataDriven.xml" />
...
</appSettings>
</configuration>
namespace Objectivity.Test.Automation.Tests.NUnit.DataDriven
{
using System.Collections;
/// <summary>
/// DataDriven methods for NUnit test framework
/// </summary>
public static class TestData
{
public static IEnumerable Credentials
{
get { return DataDrivenHelper.ReadDataDriveFile(ProjectBaseConfiguration.DataDrivenFile, "credential", new[] { "user", "password" }, "credential"); }
}
public static IEnumerable LinksSetTestName
{
get { return DataDrivenHelper.ReadDataDriveFile(ProjectBaseConfiguration.DataDrivenFile, "links", new[] { "number" }, "Count_links"); }
}
}
}
Where calling the method ReadDataDriveFile()
DataDrivenHelper.ReadDataDriveFile(ProjectBaseConfiguration.DataDrivenFile, "links", new[] { "number" }, "Count_links");
from its definitions:
public static IEnumerable<TestCaseData> DataDrivenHelper.ReadDataDriveFile(string folder, string testData, string[] diffParam, [Optional] string testName);
folder - Full path to XML DataDriveFile file
testData - Name of the child element in xml file
diffParam - Values of listed parameters will be used in test case name
testName - Name of the test, use as prefix for test case name
Definition of class DataDrivenHelper can be found here
namespace Objectivity.Test.Automation.Tests.NUnit.Tests
{
using System.Collections.Generic;
using global::NUnit.Framework;
using Objectivity.Test.Automation.Common;
using Objectivity.Test.Automation.Tests.NUnit.DataDriven;
using Objectivity.Test.Automation.Tests.PageObjects.PageObjects.TheInternet;
[TestFixture]
[Parallelizable(ParallelScope.Fixtures)]
public class HerokuappTestsNUnit : ProjectTestBase
{
[Test]
[TestCaseSource(typeof(TestData), "Credentials")]
public void FormAuthenticationPageTest(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]
[TestCaseSource(typeof(TestData), "LinksSetTestName")]
public void CountLinksAtShiftingContentTest(IDictionary<string, string> parameters)
{
new InternetPage(this.DriverContext).OpenHomePage().GoToShiftingContentPage();
var links = new ShiftingContentPage(this.DriverContext);
Verify.That(this.DriverContext, () => Assert.AreEqual(parameters["number"], links.CountLinks()));
}
}
}
Where
[TestCaseSource(typeof(TestData), "Credentials")]
TestData is a a class for relation between data driven tests and xml file from 1.1.2
"Credentials" is a method from TestData class.
Names of test are set dynamically by the values of test data read from xml file e.g:
as many time as you need corresponding child element in xml file
<credential user="tomsmith" password="SuperSecretPassword!" message="You logged into a secure area!"/>
<credential user="wronguser" password="wrongpassword" message="Your username is invalid!"/>
public static IEnumerable Links
{
get { return DataDrivenHelper.ReadDataDriveFile(ProjectBaseConfiguration.DataDrivenFile, "links"); }
}
from its definitions:
public static IEnumerable<TestCaseData> DataDrivenHelper.ReadDataDriveFile(string folder, string testData);
folder - Full path to XML DataDriveFile file
testData - Name of the child element in xml file
and set test use that method "Links"
[Test]
[TestCaseSource(typeof(TestData), "Links")]
public void CountLinksAtShiftingContentTest(IDictionary<string, string> parameters)
{
new InternetPage(this.DriverContext).OpenHomePage().GoToShiftingContentPage();
var links = new ShiftingContentPage(this.DriverContext);
Verify.That(this.DriverContext, () => Assert.AreEqual(parameters["number"], links.CountLinks()));
}
Names of test remain unchanged e.g:
Data driven tests from Excel files are not supported with NUnit by default, but you can use DataDrivenHelper class methods from our Test.Automation framework to read Excel xlsx files and pass test data to your tests.
1.2.1 Just add Excel xlsx file with test data to your NUnit project e.g
Make sure that file properties "Copy to Output Directory" is set to "Copy always" and proper path (relative) is set to that file in App.config file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
...
<add key="DataDrivenFileXlsx" value="\DataDriven\DataDriven.xlsx"/>
...
</appSettings>
</configuration>
namespace Objectivity.Test.Automation.Tests.NUnit.DataDriven
{
using System.Collections;
/// <summary>
/// DataDriven methods for NUnit test framework
/// </summary>
public static class TestData
{
public static IEnumerable CredentialsExcel
{
get { return DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "credential", new[] { "user", "password" }, "credentialExcel"); }
}
public static IEnumerable LinksExcel
{
get { return DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "links"); }
}
}
}
Where calling the method ReadXlsxDataDriveFile()
DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "credential", new[] { "user", "password" }, "credentialExcel");
from its definitions:
public static IEnumerable<TestCaseData> ReadXlsxDataDriveFile(string path, string sheetName, [Optional] string[] diffParam, [Optional] string testName)
path - Full path to Excel DataDriveFile file
sheetName - Name of the sheet at xlsx file.
diffParam - Optional values of listed parameters will be used in test case name.
testName - Optional name of the test, use as prefix for test case name.
Definition of class DataDrivenHelper can be found here
namespace Objectivity.Test.Automation.Tests.NUnit.Tests
{
using System.Collections.Generic;
using global::NUnit.Framework;
using Objectivity.Test.Automation.Common;
using Objectivity.Test.Automation.Tests.NUnit.DataDriven;
using Objectivity.Test.Automation.Tests.PageObjects.PageObjects.TheInternet;
[TestFixture]
[Parallelizable(ParallelScope.Fixtures)]
public class HerokuappTestsNUnit : ProjectTestBase
{
[Test]
[TestCaseSource(typeof(TestData), "CredentialsExcel")]
public void FormAuthenticationPageExcelTest(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]
[TestCaseSource(typeof(TestData), "LinksExcel")]
public void CountLinksAndSetTestNameAtShiftingContentExcelTest(IDictionary<string, string> parameters)
{
new InternetPage(this.DriverContext).OpenHomePage().GoToShiftingContentPage();
var links = new ShiftingContentPage(this.DriverContext);
Verify.That(this.DriverContext, () => Assert.AreEqual(parameters["number"], links.CountLinks()));
}
}
}
Where
[TestCaseSource(typeof(TestData), "CredentialsExcel")]
TestData is a a class for relation between data driven tests and xlsx file from 1.2.2
"CredentialsExcel" is a method from TestData class.
Names of test are set dynamically by the values of test data read from xlsx file e.g:
as many time as you need corresponding row in xlsx file
public static IEnumerable LinksExcel
{
get { return DataDrivenHelper.ReadXlsxDataDriveFile(ProjectBaseConfiguration.DataDrivenFileXlsx, "links"); }
}
and set test use that method "LinksExcel"
[Test]
[TestCaseSource(typeof(TestData), "LinksExcel")]
public void CountLinksAndSetTestNameAtShiftingContentExcelTest(IDictionary<string, string> parameters)
{
new InternetPage(this.DriverContext).OpenHomePage().GoToShiftingContentPage();
var links = new ShiftingContentPage(this.DriverContext);
Verify.That(this.DriverContext, () => Assert.AreEqual(parameters["number"], links.CountLinks()));
}
Data driven tests from Xml or Csv files are supported with MSTest by default.
<?xml version="1.0" encoding="utf-8" ?>
<Rows>
<Links>
<number>5</number>
</Links>
<credential>
<user>tomsmith</user>
<password>SuperSecretPassword!</password>
<message>You logged into a secure area!</message>
</credential>
<credential>
<user>wronguser</user>
<password>wrongpassword</password>
<message>Your username is invalid!</message>
</credential>
</Rows>
namespace Objectivity.Test.Automation.Tests.MsTest.Tests
{
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Objectivity.Test.Automation.Common;
using Objectivity.Test.Automation.Common.Extensions;
using Objectivity.Test.Automation.Tests.PageObjects.PageObjects.TheInternet;
[TestClass]
public class HerokuappTestsMsTest : ProjectTestBase
{
[DeploymentItem("Objectivity.Test.Automation.MsTests\\DDT.xml"),
DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
"|DataDirectory|\\DDT.xml", "credential",
DataAccessMethod.Sequential), TestMethod]
public void FormAuthenticationPageTest()
{
new InternetPage(this.DriverContext).OpenHomePage().GoToFormAuthenticationPage();
var formFormAuthentication = new FormAuthenticationPage(this.DriverContext);
formFormAuthentication.EnterUserName((string)this.TestContext.DataRow["user"]);
formFormAuthentication.EnterPassword((string)this.TestContext.DataRow["password"]);
formFormAuthentication.LogOn();
Assert.AreEqual((string)this.TestContext.DataRow["message"], formFormAuthentication.GetMessage);
}
}
}
user, password, message
tomsmith,SuperSecretPassword!,You logged into a secure area!
wronguser,wrongpassword,Your username is invalid!
[DeploymentItem("Objectivity.Test.Automation.MsTests\\DDT.csv")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\DDT.csv", "DDT#csv", DataAccessMethod.Sequential)]
[TestMethod]
public void FormAuthenticationPageCsvDataDrivenTest()
{
var formFormAuthentication = new InternetPage(this.DriverContext)
.OpenHomePage()
.GoToFormAuthenticationPage();
formFormAuthentication.EnterUserName((string)this.TestContext.DataRow["user"]);
formFormAuthentication.EnterPassword((string)this.TestContext.DataRow["password"]);
formFormAuthentication.LogOn();
Verify.That(
this.DriverContext,
() => Assert.AreEqual((string)this.TestContext.DataRow["message"], formFormAuthentication.GetMessage));
}
- Home
- Getting started
- Parallel tests execution
- MsTest DataDriven tests from Xml and CSV files
- NUnit DataDriven tests from Xml, CSV and Excel files
- Comparing files by NUnit DataDriven tests
- Visual Testing
- Screen shots: full desktop, selenium. PageSource saving
- Verify-asserts without stop tests
- Downloading files
- Helpers
- Override browser profile preferences, install browser extensions, Headless mode
- Debugging Test.Automation framework
- Logging
- Performance measures
- Webdriver Extends
- More common locators
- Selenium-Grid-support
- Advanced Browser Capabilities and Options
- AngularJS synchronization
- Update App.config or appsettings.json
- Cross browser parallel test execution with testing-Cloud-Providers\SeleniumGrid
- Verifying Javascript Errors from browser
- Enabling Performance Log for Chrome
- Azure DevOps Support
- Edge browser Support
- Downloading and running Selenium Grid with Powershell
- Run Ocaramba tests with Docker container
- HTTP auth in Internet explorer
- ExtentReports Support