Reputation: 55
I'm working on automation tests which I'm still new to writing the scripts. I'll try to be as short as possible. I'm building everything in .NET CORE. Selenium C# in Visual Studio
Basically what I would like to know is how to pull string from appsettings.json file to my test class file?
Basically what I have inside my appsettings.json file ->
"BaseURL": "https://google.com/",
What I have in my test clase is:
[TestCase]
public void LoginToGmail()
*currently Im using hardcoded version of*
driver.url = "https://google.com/"*
IWebElement GmailLogo = driver.FindElement(By.Xpath.......);
Assert.IsTrue(GmailLogo.Displayed);
But I would like to use something like driver.url = "BaseURL" <- (string from the .json file)
How do you pull it from the .json file?
Any kind of help would be appreciated!
Upvotes: 0
Views: 3346
Reputation: 55
Thank you @SteveBeck for the link, helped me. Also thanks to @Greg Burghardt. I managed to set it up like this: created new class for the read Json file and browser startup/close.
using Newtonsoft.Json;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.IO;
public class NewSetup
{
public class GetSetUpFile
{
public string BaseURL { get; set; }
public string SetupJson { get; set; }
private readonly string _strSetupFile;
public GetSetUpFile(string jsonfile)
{
_strSetupFile = jsonfile;
}
public void ReadSetUpFile()
{
SetupJson = File.ReadAllText(_strSetupFile);
}
public class StartBrowserDriver
{
protected IWebDriver driver;
[SetUp]
public void StartBrowser()
{
var setup = new GetSetUpFile(@"C:\\PATH\\\TO\\\appsettings.json");
setup.ReadSetUpFile();
//Deserialize json objects
setup = JsonConvert.DeserializeObject<GetSetUpFile>(setup.SetupJson);
var url = setup.BaseURL;
var DesiredURL = url;
driver = new ChromeDriver("C:\\Program Files\\Google\\Driver");
driver.Navigate().GoToUrl(DesiredURL);
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
}
[TearDown]
public void CloseBrowser()
{
driver.Close();
driver.Quit();
}
}
}
}
Navigate back to your main test class:
class User_is_able_to_login_to_Gmail : StartAndCloseBrowserDriver
{
[TestCase]
public void public void LoginToGmail();
IWebElement GmailLogo = driver.FindElement(By.Xpath.......);
Assert.IsTrue(GmailLogo.Displayed);
GmailLogo.Click();
\\\\continue\\\\
The test runs & works. If someone has some pointers about the build, please let me know :) Thanks to everyone!
Upvotes: 0
Reputation: 128
Any JSON can be deserialised into a class using C#, this is not limited to an appsettings.json file and you may find it useful to store data sets in json format for testing purposes.
Upvotes: 1
Reputation: 18783
You can use the ConfigurationBuilder class to read the config file. Assuming this structure for appsettings.json:
{
"BaseURL": "https://..."
}
Reading and using the configuration becomes:
var settings = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
driver.Url = settings["BaseURL"];
Important: Be sure the "Copy to Output Directory" property on appsettings.json is set to "Always" or "If Newer" (right-click on appsettings.json in Solution Explorer and choose "Properties"):
Upvotes: 1