Retrieve Test Data from Json file in C# Selenium Framework | Devstringx

Retrieve Test Data from Json

Retrieve Test Data from Json file in C# Selenium Framework | Devstringx

When we write an automation framework one of the many challenges is to use test data in our test scripts. One way is to hardcode the test data in our test scripts but this approach is not extensible, what if the test data change after some time?

Then we have to change the hardcoded data in the test scripts themselves and if the same test data is used in various test scripts changing the hardcoded data from each test script will be time-consuming and will result in test script failure if some of the test data is not updated correctly.

Another approach is to separate the test data from the test script and put the test data in an external file like Excel or JSON.

In this blog, I will show you how we can retrieve the data from JSON files and use that data in our test script.

I will be extending the CalculatorFramework.

Create a new folder TestData and create a Data.json file.

Data.json

{

“Addition”: 16,

“Subtraction”: 2,

“Multiplication”: 63,

“Division”: 1,

“Array”: [

“one”,

“two”,

“three”

]

}

In our JSON file Addition, Subtraction, Multiplication, Division, and Array are Keys and each key has a corresponding value. The value can be of any data type e.g., integer, float, Boolean, string, array, etc. Using the key, we can fetch the corresponding data. In the Data.json file, we have four integer values and one string array.

Create a new JsonHelper.cs file which we will use to get the data from JSON.

JsonHelper.cs

using Newtonsoft.Json.Linq;

using System;

using System.Collections.Generic;

using System.IO;

namespace CalculatorFramework.Utils

{

class JsonHelper

{

public static string GetProjectRootDirectory()

{

string currentDirectory = Directory.GetCurrentDirectory();

return currentDirectory.Split(“bin”)[0];

}

 

private static JObject GetTestDataJsonObject()

{

string path = Path.Combine(GetProjectRootDirectory(), “Testdata”, “Data.json”);

JObject jObject = JObject.Parse(File.ReadAllText(path));

return jObject;

}

public static int GetTestDataInt(string label)

{

var jObject = GetTestDataJsonObject();

return Int32.Parse(jObject[label].ToString());

}

public static List<string> GetTestDataArray(string label)

{

var jObject = GetTestDataJsonObject();

return jObject[label].ToObject<List<string>>(); ;

}

}

}

GetProjectRootDirectory() – Returns the project root directory path.

GetTestDataJsonObject() – Reads the json file and returns a Json Object.

GetTestDataInt() – Use the JSON object to get the value for the corresponding Key from the JSON file.

GetTestDataArray() – Use the JSON object to get the array value for the corresponding Key from the JSON file.

Read Also:- How To Get The Execution Time of Multiple Methods In Selenium Java?

AddSubtractTest.cs

using CalculatorFramework.Tests;

using CalculatorFramework.Utils;

using JCMSFramework.Utils;

using NUnit.Framework;

using System.Collections.Generic;

namespace CalculatorFramework

{

[Parallelizable]

public class AddSubtractTest : BaseTest

{

readonly List<string> list = JsonHelper.GetTestDataArray(“Array”);

[TestCase(9, 7)]

public void Addition(double a, double b)

{

string str1 = a.ToString();

string str2 = b.ToString();

string result = homePageStep.Add(str1, str2);

int expectedResult = JsonHelper.GetTestDataInt(“Addition”);

Assert.AreEqual(expectedResult, double.Parse(result), “Result is incorrect”);

ReportLog.Pass(“Addition of ” + a + ” and ” + b + ” is: ” + result);

ReportLog.Info(string.Join(“, “, list));

}

[TestCase(9, 7)]

public void Subtraction(double a, double b)

{

string str1 = a.ToString();

string str2 = b.ToString();

string result = homePageStep.Subtract(str1, str2);

int expectedResult = JsonHelper.GetTestDataInt(“Subtraction”);

Assert.AreEqual(expectedResult, double.Parse(result), “Result is incorrect”);

ReportLog.Pass(“Subtraction of ” + a + ” and ” + b + ” is: ” + result);

ReportLog.Info(string.Join(“, “, list));

}

}

}

Read Also:- Fetch Text from Image & PDF Using Selenium Java

MultiplyDivideTest.cs

using CalculatorFramework.Tests;

using CalculatorFramework.Utils;

using JCMSFramework.Utils;

using NUnit.Framework;

using System.Collections.Generic;

namespace CalculatorFramework

{

[Parallelizable]

public class MultiplyDivideTest : BaseTest

{

readonly List<string> list = JsonHelper.GetTestDataArray(“Array”);

[TestCase(9, 7)]

public void Multiplication(double a, double b)

{

string str1 = a.ToString();

string str2 = b.ToString();

string result = homePageStep.Multiply(str1, str2);

int expectedResult = JsonHelper.GetTestDataInt(“Multiplication”);

Assert.AreEqual(expectedResult, double.Parse(result), “Result is incorrect”);

ReportLog.Pass(“Multiplication of ” + a + ” and ” + b + ” is: ” + result);

ReportLog.Info(string.Join(“, “, list));

}

[TestCase(9, 7)]

public void Division(double a, double b)

{

string str1 = a.ToString();

string str2 = b.ToString();

string result = homePageStep.Divide(str1, str2);

int expectedResult = JsonHelper.GetTestDataInt(“Division”);

Assert.AreEqual(expectedResult, float.Parse(result), “Result is incorrect”);

ReportLog.Pass(“Division of ” + a + ” and ” + b + ” is: ” + result);

ReportLog.Info(string.Join(“, “, list));

}

}

}

Read Also:- Integrate Extent Report with in NUnit Selenium Framework Screenshots

Project Structure

Project Structure

Reports

Reports

 

 

 

 

 

 

 

 

 

 

Json file

Share this post