-
Notifications
You must be signed in to change notification settings - Fork 14
/
NetworkHandlingTests.cs
127 lines (119 loc) · 6.74 KB
/
NetworkHandlingTests.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using Aquality.Selenium.Browsers;
using Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms;
using NUnit.Framework;
using OpenQA.Selenium;
using System.IO;
using System.Linq;
namespace Aquality.Selenium.Tests.Integration
{
internal class NetworkHandlingTests : UITest
{
private const string LogPath = "../../../Log/log.log";
[Test]
public void Should_BePossibleTo_SetBasicAuthentication()
{
var welcomeForm = new WelcomeForm();
welcomeForm.Open();
var basicAuthForm = new BasicAuthForm();
Assert.DoesNotThrowAsync(async () => await AqualityServices.Browser.RegisterBasicAuthenticationAndStartMonitoring(BasicAuthForm.Domain, BasicAuthForm.User, BasicAuthForm.Password),
"Should be possible to set basic authentication async");
basicAuthForm.Open();
Assert.That(basicAuthForm.IsCongratulationsPresent, "Basic authentication should work");
}
[Test]
public void Should_BePossibleTo_ClearBasicAuthentication()
{
var welcomeForm = new WelcomeForm();
welcomeForm.Open();
var basicAuthForm = new BasicAuthForm();
Assert.DoesNotThrowAsync(async () => await AqualityServices.Browser.RegisterBasicAuthenticationAndStartMonitoring(BasicAuthForm.Domain, BasicAuthForm.User, BasicAuthForm.Password),
"Should be possible to set basic authentication async");
AqualityServices.Browser.Network.ClearAuthenticationHandlers();
basicAuthForm.Open();
Assert.That(basicAuthForm.IsCongratulationsPresent, Is.False, "Basic authentication should not work after the handler is cleared");
Assert.DoesNotThrowAsync(async () => await AqualityServices.Browser.Network.StopMonitoring(), "Should be possible to stop network monitoring");
}
[Test]
public void Should_BePossibleTo_AddAndClearRequestHandler()
{
const string somePhrase = "delicious cheese!";
AqualityServices.Browser.Network.AddRequestHandler(
new NetworkRequestHandler
{
RequestMatcher = req => true,
ResponseSupplier = req => new HttpResponseData { Body = somePhrase, StatusCode = 200 }
});
Assert.DoesNotThrowAsync(() => AqualityServices.Browser.Network.StartMonitoring());
var welcomeForm = new WelcomeForm();
welcomeForm.Open();
Assert.That(AqualityServices.Browser.Driver.PageSource, Does.Contain(somePhrase), "Request should be intercepted");
AqualityServices.Browser.Network.ClearRequestHandlers();
welcomeForm.Open();
Assert.That(AqualityServices.Browser.Driver.PageSource, Does.Not.Contain(somePhrase), "Request should not be intercepted");
}
[Test]
public void Should_BePossibleTo_AddAndClearResponseHandler()
{
const string somePhrase = "delicious cheese!";
AqualityServices.Browser.Network.AddResponseHandler(
new NetworkResponseHandler
{
ResponseMatcher = res => true,
ResponseTransformer = res => new HttpResponseData { Body = somePhrase, StatusCode = 200 }
});
Assert.DoesNotThrowAsync(async() => await AqualityServices.Browser.Network.StartMonitoring());
var welcomeForm = new WelcomeForm();
welcomeForm.Open();
Assert.That(AqualityServices.Browser.Driver.PageSource, Does.Contain(somePhrase), "Response should be intercepted");
AqualityServices.Browser.Network.ClearResponseHandlers();
welcomeForm.Open();
Assert.That(AqualityServices.Browser.Driver.PageSource, Does.Not.Contain(somePhrase), "Response should not be intercepted");
}
[Test]
public void Should_BePossibleTo_SubscribeToRequestSentEvent_AndUnsubscribeFromIt()
{
var welcomeForm = new WelcomeForm();
welcomeForm.Open();
var counter = 0;
void eventHandler(object sender, NetworkRequestSentEventArgs args) => ++counter;
AqualityServices.Browser.Network.NetworkRequestSent += eventHandler;
Assert.DoesNotThrowAsync(async () => await AqualityServices.Browser.Network.StartMonitoring());
welcomeForm.Open();
Assert.That(counter, Is.GreaterThan(0), "Should be possible to subscribe to Request Sent event");
var oldValue = counter;
AqualityServices.Browser.Network.NetworkRequestSent -= eventHandler;
welcomeForm.Open();
Assert.That(counter, Is.EqualTo(oldValue), "Should be possible to unsubscribe from Request Sent event");
}
[Test]
public void Should_BePossibleTo_SubscribeToResponseReceivedEvent_AndUnsubscribeFromIt()
{
var welcomeForm = new WelcomeForm();
welcomeForm.Open();
var counter = 0;
void eventHandler(object sender, NetworkResponseReceivedEventArgs args) => ++counter;
AqualityServices.Browser.Network.NetworkResponseReceived += eventHandler;
Assert.DoesNotThrowAsync(async () => await AqualityServices.Browser.Network.StartMonitoring());
welcomeForm.Open();
Assert.That(counter, Is.GreaterThan(0), "Should be possible to subscribe to Response Received event");
var oldValue = counter;
AqualityServices.Browser.Network.NetworkResponseReceived -= eventHandler;
welcomeForm.Open();
Assert.That(counter, Is.EqualTo(oldValue), "Should be possible to unsubscribe from Response Received event");
}
[Test]
[Parallelizable(ParallelScope.None)]
public void Should_BePossibleTo_EnableHttpExchangeLogging_AndDisableIt()
{
var someForm = new DropdownForm();
someForm.Open();
var logMessage1 = File.ReadAllLines(LogPath).LastOrDefault();
Assert.That(string.IsNullOrEmpty(logMessage1), Is.False, "Some message should appear in log file and should not be empty");
Assert.DoesNotThrowAsync(async () => await AqualityServices.Browser.EnableHttpExchangeLoggingAndStartMonitoring(), "Should be possible to enable HTTP exchange logging");
AqualityServices.Browser.Driver.Navigate().Refresh();
var logMessage2 = File.ReadAllLines(LogPath).LastOrDefault();
Assert.That(string.IsNullOrEmpty(logMessage2), Is.False, "Some message should appear in log file and should not be empty");
Assert.That(logMessage2, Is.Not.EqualTo(logMessage1), "HTTP logging message should be in file, although no Aquality-actions performed");
}
}
}