How to pass login inputs dynamically through JSON file in C# selenium?

Viewed 419

I have created automation script to test WEB UI using selenium C#. I dont want to hardcode the login details and want to send those values through JSON input. How do I convert Json inputs in C#.

 IWebDriver webdriver = new ChromeDriver(@"D:\Selenium\Chrome_90V");
        Loginpage LoginP = new Loginpage(webdriver);
        Homepage homep = new Homepage(webdriver);
        DataJson Js = new DataJson();
        
        webdriver.Navigate().GoToUrl("https://login.microsoftonline.com");
        //timewait
        TimeSpan timeSpan = TimeSpan.FromSeconds(20);
        webdriver.Manage().Timeouts().ImplicitWait = timeSpan;
        //emailidinput
        LoginP.setemail("**abcdefgh@gmail.com**");
        LoginP.clicknext();


       

How do I tell my code to use JSON inputs through class.

1 Answers

I found solution for this and would like to post it here, if someone else is looking for this.

First create Json file in and put the values in array as: { "key":"value"; "email": "abcedfgh@gmail.com";}

Next create a new Cs file and convert json file to Csharp class Login { public string Key{ get; set; } public string email { get; set; } }

next create a new Cs file to parse the data class DataSourceParser { public static Login populateLogin() { string Loginjson = File.ReadAllText(@""); Login login = JsonConvert.DeserializeObject(Loginjson); return login; } }

Now update your test script mentioned in my query as: Login login = DataSourceParser. PopulateLogin(); instead of "DataJson Js = new DataJson();"

verify all string details appears at login.

instead of providing mailid use LoginP.setemail**(login.emailid);**

Related