-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
WeatherForecastTest.cs
85 lines (65 loc) · 2.74 KB
/
WeatherForecastTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
namespace WeatherForecast.Tests;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
public static class WeatherForecastTest
{
public sealed class Api : IClassFixture<WeatherForecastContainer>
{
private readonly WeatherForecastContainer _weatherForecastContainer;
public Api(WeatherForecastContainer weatherForecastContainer)
{
_weatherForecastContainer = weatherForecastContainer;
_weatherForecastContainer.SetBaseAddress();
}
[Fact]
[Trait("Category", nameof(Api))]
public async Task Get_WeatherForecast_ReturnsSevenDays()
{
// Given
const string path = "api/WeatherForecast";
// When
var response = await _weatherForecastContainer.GetAsync(path)
.ConfigureAwait(true);
var weatherForecastStream = await response.Content.ReadAsStreamAsync()
.ConfigureAwait(true);
var weatherForecast = await JsonSerializer.DeserializeAsync<IEnumerable<WeatherData>>(weatherForecastStream)
.ConfigureAwait(true);
// Then
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(7, weatherForecast!.Count());
}
}
public sealed class Web : IClassFixture<WeatherForecastContainer>
{
private static readonly ChromeOptions ChromeOptions = new ChromeOptions();
private readonly WeatherForecastContainer _weatherForecastContainer;
static Web()
{
ChromeOptions.AddArgument("headless");
ChromeOptions.AddArgument("ignore-certificate-errors");
}
public Web(WeatherForecastContainer weatherForecastContainer)
{
_weatherForecastContainer = weatherForecastContainer;
_weatherForecastContainer.SetBaseAddress();
}
[Fact]
[Trait("Category", nameof(Web))]
public void Get_WeatherForecast_ReturnsSevenDays()
{
// Given
string ScreenshotFileName() => $"{nameof(Get_WeatherForecast_ReturnsSevenDays)}_{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}.png";
using var chrome = new ChromeDriver(ChromeOptions);
// When
chrome.Navigate().GoToUrl(_weatherForecastContainer.BaseAddress);
chrome.GetScreenshot().SaveAsFile(Path.Combine(CommonDirectoryPath.GetSolutionDirectory().DirectoryPath, ScreenshotFileName()));
chrome.FindElement(By.TagName("fluent-button")).Click();
var wait = new WebDriverWait(chrome, TimeSpan.FromSeconds(10));
wait.Until(webDriver => 1.Equals(webDriver.FindElements(By.TagName("span")).Count));
chrome.GetScreenshot().SaveAsFile(Path.Combine(CommonDirectoryPath.GetSolutionDirectory().DirectoryPath, ScreenshotFileName()));
// Then
Assert.Equal(7, int.Parse(chrome.FindElement(By.TagName("span")).Text, NumberStyles.Integer, CultureInfo.InvariantCulture));
}
}
}