New way of reading configurations from file and deserializing
This commit is contained in:
@ -15,6 +15,7 @@ namespace EATestFramework.Driver
|
||||
_testSettings = testSettings;
|
||||
_browserDriver = browserDriver;
|
||||
driver = GetWebDriver();
|
||||
driver.Navigate().GoToUrl(_testSettings.ApplicationUrl);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -1,20 +1,39 @@
|
||||
using EATestFramework.Driver;
|
||||
using EATestFramework.Settings;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace EATestFramework.Extensions;
|
||||
public static class WebDriverInitializerExtension
|
||||
{
|
||||
public static IServiceCollection UseWebDriverInitializer(
|
||||
this IServiceCollection services,
|
||||
BrowserType browserType)
|
||||
this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton(new TestSettings
|
||||
{
|
||||
BrowserType = browserType
|
||||
});
|
||||
services.AddSingleton(ReadConfig());
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static TestSettings ReadConfig()
|
||||
{
|
||||
var configFile = File
|
||||
.ReadAllText(Path.GetDirectoryName(
|
||||
Assembly.GetExecutingAssembly().Location)
|
||||
+ "/appsettings.json");
|
||||
|
||||
var jsonSerializeOptions = new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
jsonSerializeOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
|
||||
var testSettings = JsonSerializer.Deserialize<TestSettings>(configFile, jsonSerializeOptions);
|
||||
|
||||
return testSettings;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,13 @@
|
||||
using EATestFramework.Driver;
|
||||
using System;
|
||||
|
||||
namespace EATestFramework.Settings
|
||||
{
|
||||
public class TestSettings
|
||||
{
|
||||
public BrowserType BrowserType { get; set; }
|
||||
public Uri ApplicationUrl { get; set; }
|
||||
|
||||
public int TimeoutInterval { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,7 +8,6 @@ namespace EATestFramework
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.UseWebDriverInitializer(BrowserType.Firefox);
|
||||
services.AddScoped<IBrowserDriver, BrowserDriver>();
|
||||
services.AddScoped<IDriverFixture, DriverFixture>();
|
||||
}
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture" Version="4.17.0" />
|
||||
<PackageReference Include="AutoFixture.Xunit2" Version="4.17.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
|
||||
<PackageReference Include="Selenium.Support" Version="4.1.0" />
|
||||
<PackageReference Include="Selenium.WebDriver" Version="4.1.0" />
|
||||
@ -30,4 +31,10 @@
|
||||
<ProjectReference Include="..\EATestFramework\EATestFramework.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
using EATestFramework.Driver;
|
||||
using EATestFramework.Extensions;
|
||||
using EATestProject.Pages;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace EATestProject
|
||||
{
|
||||
@ -9,7 +11,8 @@ namespace EATestProject
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.UseWebDriverInitializer(BrowserType.Firefox);
|
||||
|
||||
services.UseWebDriverInitializer();
|
||||
services.AddScoped<IHomePage, HomePage>();
|
||||
services.AddScoped<ICreateProductPage, CreateProductPage>();
|
||||
services.AddScoped<IBrowserDriver, BrowserDriver>();
|
||||
|
||||
@ -8,23 +8,15 @@ using Xunit;
|
||||
|
||||
namespace EATestProject
|
||||
{
|
||||
public class UnitTest1: IDisposable
|
||||
public class UnitTest1
|
||||
{
|
||||
private readonly IWebDriver _driver;
|
||||
private readonly IHomePage _homePage;
|
||||
private readonly ICreateProductPage _createProductPage;
|
||||
|
||||
public UnitTest1(IDriverFixture driver, IHomePage homePage, ICreateProductPage createProductPage)
|
||||
public UnitTest1( IHomePage homePage, ICreateProductPage createProductPage)
|
||||
{
|
||||
_homePage = homePage;
|
||||
_createProductPage = createProductPage;
|
||||
_driver = driver.Driver;
|
||||
_driver.Navigate().GoToUrl(new Uri("http://localhost:33084"));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_driver.Quit();
|
||||
}
|
||||
|
||||
[Theory,AutoData]
|
||||
@ -43,5 +35,21 @@ namespace EATestProject
|
||||
_homePage.CreateProduct();
|
||||
_createProductPage.EnterProductDetails(product);
|
||||
}
|
||||
|
||||
[Theory, AutoData]
|
||||
public void Test3(Product product)
|
||||
{
|
||||
|
||||
_homePage.CreateProduct();
|
||||
_createProductPage.EnterProductDetails(product);
|
||||
}
|
||||
|
||||
[Theory, AutoData]
|
||||
public void Test4(Product product)
|
||||
{
|
||||
|
||||
_homePage.CreateProduct();
|
||||
_createProductPage.EnterProductDetails(product);
|
||||
}
|
||||
}
|
||||
}
|
||||
5
EATestProject/appsettings.json
Normal file
5
EATestProject/appsettings.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"BrowserType": "chrome",
|
||||
"ApplicationUrl": "http://localhost:33084",
|
||||
"TimeoutInterval": 30
|
||||
}
|
||||
Reference in New Issue
Block a user