I'm working on Nunit's data-driven method and I'm stuck with this issue. I want to pass values from the JSON file as the parameters in the test method. 'I've done this much so far but it's giving me an error that says: can not convert from 'Newtonsoft.Json.LinqToken' to 'string
public class TestData: BasePage
{
static JObject? data = ReadJson(@"jsconfig1.json");
static JToken? url = data.SelectToken("url").Value<string>();
static JToken? username = data.SelectToken("username").Value<string>();
static JToken? password = data.SelectToken("password").Value<string>();
public void Login_Method()
{
driver.Url = url;
driver.FindElement(By.Id("username")).SendKeys(username);
driver.FindElement(By.Id("password")).SendKeys(password);
driver.FindElement(By.Id("login")).Click();
driver.Close();
}
Let's say that the jsconfig1.json
is like this:
var json = """
{
"url": "https://www.google.com",
"username": "guest",
"password": "pwd123"
}
""";
Then one way among others is to create a dto:
public record Config(string Url, string Username, string Password);
and then to deserialize using Text.Json
:
var config = JsonSerializer.Deserialize<Config>(json, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
string username = config.Username;
string password = config.Password;
Finally because the SendKeys
method requires string as input you can use the above strings in order to submit the user/pass values