I am reading a selenium guidebook for c# and they show this:
class BaseTest
{
private static string VendorDirectory = System.IO.Directory.GetParent(
System.AppContext.BaseDirectory).
Parent.Parent.Parent.FullName
+ @"/vendor";
protected IWebDriver Driver;
public static string BaseUrl;
[SetUp]
protected void SetUp()
{
BaseUrl = System.Environment.GetEnvironmentVariable("BASE_URL") ??
"http://the-internet.herokuapp.com";
But it doesn't show how they are actually setting the environment variables. Is BASE_URL coming from appsettings.json? I'm not sure where they are getting it from. Right now, I have a class with all of the urls I am using throughout my tests like this:
public static class Urls
{
public static readonly string baseUrl = "https://localhost:5001/";
public static readonly string aboutUrl = $"{baseUrl}about";
public static readonly string citiesUrl = $"{baseUrl}cities";
public static readonly string countriesUrl = $"{baseUrl}countries";
}
I don't think this is the best way to do it and want to try to use environment variables instead but I am not sure how to do that. When I change from localhost to a production environment how I have it now will obviously break. How can I set the baseUrl so it knows what environment I am in?
EDITED My test solution is in a separate repo from my project solution. My test solution is a c# xunit test project. I added an appsettings.json file to my solution. It looks like this
{
"Base_Url": "https://testurl/",
"AllowedHosts": "*"
}
Inside one of my tests that uses the url, I am doing this
public static IConfiguration InitConfiguration()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
return config;
}
[Fact]
public void LoadFaqs()
{
using IWebDriver driver = new ChromeDriver();
var config = InitConfiguration();
var faqurl = config["Base_Url"] + "faqs";
driver.Navigate().GoToUrl(faqurl);
}
When I run my test, it is failing because it cannot find my appsettings.json file. I also tried putting it inside my test folder and it still couldn't find it. I'm not sure what I am doing wrong.