Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f84d8f8

Browse files
nvborisenkotitusfortner
authored andcommitted
Simplify and add examples for bidi network intercepring
1 parent df4c63e commit f84d8f8

File tree

5 files changed

+107
-35
lines changed

5 files changed

+107
-35
lines changed

‎examples/dotnet/SeleniumDocs/ChromeDevTools/NetworkInterceptor.cs‎

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,76 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using OpenQA.Selenium;
3+
using System.Threading.Tasks;
34

45
namespace SeleniumDocs.ChromeDevTools
56
{
67
[TestClass]
78
public class NetworkInterceptorTest : BaseChromeTest
89
{
910
[TestMethod]
10-
public void InterceptNetworkForAuthentication()
11+
public asyncTask InterceptNetworkForAuthentication()
1112
{
1213
var handler = new NetworkAuthenticationHandler()
1314
{
1415
UriMatcher = _ => true,
1516
Credentials = new PasswordCredentials("admin", "admin")
1617
};
18+
1719
INetwork networkInterceptor = driver.Manage().Network;
1820
networkInterceptor.AddAuthenticationHandler(handler);
19-
networkInterceptor.StartMonitoring().Wait();
21+
22+
await networkInterceptor.StartMonitoring();
2023
driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/basic_auth");
21-
networkInterceptor.StopMonitoring().Wait();
24+
awaitnetworkInterceptor.StopMonitoring();
2225

2326
Assert.AreEqual("Congratulations! You must have the proper credentials.", driver.FindElement(By.TagName("p")).Text);
2427

2528
}
2629

2730
[TestMethod]
28-
public void InterceptNetworkResponse()
31+
public asyncTask InterceptNetworkResponse()
2932
{
30-
var handler = new NetworkResponseHandler();
31-
handler.ResponseMatcher = httpresponse => true;
32-
handler.ResponseTransformer = http =>
33+
var handler = new NetworkResponseHandler()
3334
{
34-
var response = new HttpResponseData();
35-
response.StatusCode = 200;
36-
response.Body = "Creamy, delicious cheese!";
37-
return response;
35+
ResponseMatcher = httpresponse => true,
36+
ResponseTransformer = http => new()
37+
{
38+
StatusCode = 200,
39+
Body = "Creamy, delicious cheese!"
40+
}
3841
};
3942

4043
INetwork networkInterceptor = driver.Manage().Network;
4144
networkInterceptor.AddResponseHandler(handler);
42-
networkInterceptor.StartMonitoring().Wait();
45+
46+
await networkInterceptor.StartMonitoring();
4347
driver.Navigate().GoToUrl("http://google.com");
44-
networkInterceptor.StopMonitoring().Wait();
48+
awaitnetworkInterceptor.StopMonitoring();
4549

46-
Assert.IsTrue(driver.PageSource.Contains("delicious cheese"));
50+
StringAssert.Contains(driver.PageSource,"delicious cheese");
4751
}
4852

4953
[TestMethod]
50-
public void InterceptNetworkRequest()
54+
public asyncTask InterceptNetworkRequest()
5155
{
52-
var handler = new NetworkRequestHandler();
53-
handler.RequestMatcher= httprequest =>true;
54-
handler.ResponseSupplier= http =>
55-
{
56-
varresponse=newHttpResponseData();
57-
response.StatusCode = 200;
58-
response.Body = "Creamy, delicious cheese!";
59-
returnresponse;
60-
};
56+
var handler = new NetworkRequestHandler()
57+
{
58+
RequestMatcher= httprequest =>true,
59+
ResponseSupplier= http =>new()
60+
{
61+
StatusCode = 200,
62+
Body = "Creamy, delicious cheese!"
63+
}
64+
};
6165

6266
INetwork networkInterceptor = driver.Manage().Network;
6367
networkInterceptor.AddRequestHandler(handler);
6468

65-
networkInterceptor.StartMonitoring().Wait();
69+
awaitnetworkInterceptor.StartMonitoring();
6670
driver.Navigate().GoToUrl("https://google.com");
67-
networkInterceptor.StopMonitoring().Wait();
71+
awaitnetworkInterceptor.StopMonitoring();
6872

69-
Assert.IsTrue(driver.PageSource.Contains("delicious cheese"));
73+
StringAssert.Contains(driver.PageSource,"delicious cheese");
7074
}
7175

7276
}

‎website_and_docs/content/documentation/webdriver/bidirectional/bidi_api.en.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,25 @@ it with the following examples.
470470
{{< tab header="Python" >}}
471471
Currently unavailable in python due the inability to mix certain async and sync commands
472472
{{< /tab >}}
473-
{{< tab header="CSharp" text=true >}}
474-
{{< badge-code >}}
473+
{{< tab header="CSharp" >}}
474+
var handler = new NetworkRequestHandler()
475+
{
476+
RequestMatcher = httprequest => true,
477+
ResponseSupplier = http => new()
478+
{
479+
StatusCode = 200,
480+
Body = "Creamy, delicious cheese!"
481+
}
482+
};
483+
484+
INetwork networkInterceptor = driver.Manage().Network;
485+
networkInterceptor.AddRequestHandler(handler);
486+
487+
await networkInterceptor.StartMonitoring();
488+
driver.Navigate().GoToUrl("https://google.com");
489+
await networkInterceptor.StopMonitoring();
490+
491+
StringAssert.Contains(driver.PageSource, "delicious cheese");
475492
{{< /tab >}}
476493
{{< tab header="Ruby" >}}
477494
require 'selenium-webdriver'

‎website_and_docs/content/documentation/webdriver/bidirectional/bidi_api.ja.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,25 @@ it with the following examples.
479479
{{< tab header="Python" >}}
480480
Currently unavailable in python due the inability to mix certain async and sync commands
481481
{{< /tab >}}
482-
{{< tab header="CSharp" text=true >}}
483-
{{< badge-code >}}
482+
{{< tab header="CSharp" >}}
483+
var handler = new NetworkRequestHandler()
484+
{
485+
RequestMatcher = httprequest => true,
486+
ResponseSupplier = http => new()
487+
{
488+
StatusCode = 200,
489+
Body = "Creamy, delicious cheese!"
490+
}
491+
};
492+
493+
INetwork networkInterceptor = driver.Manage().Network;
494+
networkInterceptor.AddRequestHandler(handler);
495+
496+
await networkInterceptor.StartMonitoring();
497+
driver.Navigate().GoToUrl("https://google.com");
498+
await networkInterceptor.StopMonitoring();
499+
500+
StringAssert.Contains(driver.PageSource, "delicious cheese");
484501
{{< /tab >}}
485502
{{< tab header="Ruby" >}}
486503
require 'selenium-webdriver'

‎website_and_docs/content/documentation/webdriver/bidirectional/bidi_api.pt-br.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,25 @@ com os exemplos a seguir.
464464
{{< tab header="Python" >}}
465465
Currently unavailable in python due the inability to mix certain async and sync commands
466466
{{< /tab >}}
467-
{{< tab header="CSharp" text=true >}}
468-
{{< badge-code >}}
467+
{{< tab header="CSharp" >}}
468+
var handler = new NetworkRequestHandler()
469+
{
470+
RequestMatcher = httprequest => true,
471+
ResponseSupplier = http => new()
472+
{
473+
StatusCode = 200,
474+
Body = "Creamy, delicious cheese!"
475+
}
476+
};
477+
478+
INetwork networkInterceptor = driver.Manage().Network;
479+
networkInterceptor.AddRequestHandler(handler);
480+
481+
await networkInterceptor.StartMonitoring();
482+
driver.Navigate().GoToUrl("https://google.com");
483+
await networkInterceptor.StopMonitoring();
484+
485+
StringAssert.Contains(driver.PageSource, "delicious cheese");
469486
{{< /tab >}}
470487
{{< tab header="Ruby" >}}
471488
require 'selenium-webdriver'

‎website_and_docs/content/documentation/webdriver/bidirectional/bidi_api.zh-cn.md‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,25 @@ it with the following examples.
481481
{{< tab header="Python" >}}
482482
Currently unavailable in python due the inability to mix certain async and sync commands
483483
{{< /tab >}}
484-
{{< tab header="CSharp" text=true >}}
485-
{{< badge-code >}}
484+
{{< tab header="CSharp" >}}
485+
var handler = new NetworkRequestHandler()
486+
{
487+
RequestMatcher = httprequest => true,
488+
ResponseSupplier = http => new()
489+
{
490+
StatusCode = 200,
491+
Body = "Creamy, delicious cheese!"
492+
}
493+
};
494+
495+
INetwork networkInterceptor = driver.Manage().Network;
496+
networkInterceptor.AddRequestHandler(handler);
497+
498+
await networkInterceptor.StartMonitoring();
499+
driver.Navigate().GoToUrl("https://google.com");
500+
await networkInterceptor.StopMonitoring();
501+
502+
StringAssert.Contains(driver.PageSource, "delicious cheese");
486503
{{< /tab >}}
487504
{{< tab header="Ruby" >}}
488505
require 'selenium-webdriver'

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /