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

{“Addition”: 16,“Subtraction”: 2,“Multiplication”: 63,“Division”: 1,“Array”: [“one”,“two”,“three”]}
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>>(); ;}}}
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));}}}
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));}}}

--

--

--

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Invest In The Metals Market by Employing An API&nbsp;

Hands-On Implementation of Apple’s In-App Purchases On Ruby Based Server

VMware Horizon 7 Overview

S.O.L.I.D Principles

What Is The Best IP Database API In 2022?

Top 10 AWS Cloud Managed Services Provider in 2020

𝐅𝐮𝐧 𝐰𝐢𝐭𝐡 𝐂𝐨𝐦𝐩𝐮𝐭𝐞𝐫 𝐕𝐢𝐬𝐢𝐨𝐧 𝐚𝐧𝐝 𝐈𝐦𝐚𝐠𝐞 𝐩𝐫𝐨𝐜𝐞𝐬𝐬𝐢𝐧𝐠

Revise vocabulary with a Rephraser

vocabulary with a Rephraser

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Devstringx Technologies

Devstringx Technologies

Devstringx Technologies is highly recommended IT company for custom software development, mobile app development and automation testing services

More from Medium

Deploy an Angular application with Azure app service

How to add Integration Tests to our Spring Boot backend

Part II-Integration of Swagger With Aspnetcore OData API

How To Create A Web Service Project In .NET Using Visual Studio