Skip to content

Commit 9518ffe

Browse files
committed
Adding WaitForDownload test for .NET
1 parent 0fde8cc commit 9518ffe

File tree

6 files changed

+52
-7
lines changed

6 files changed

+52
-7
lines changed

examples/dotnet/SeleniumDocs/BaseTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ protected void StartDriver(string browserVersion = "stable")
3939
{
4040
BrowserVersion = browserVersion
4141
};
42+
options.AddArgument("--no-sandbox");
4243
driver = new ChromeDriver(options);
4344
}
4445

examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.IO;
24
using System.Threading.Tasks;
35
using Microsoft.VisualStudio.TestTools.UnitTesting;
46
using OpenQA.Selenium;
57
using OpenQA.Selenium.DevTools;
68
using System.Linq;
9+
using System.Threading;
10+
using OpenQA.Selenium.DevTools.V127.Browser;
711
using OpenQA.Selenium.DevTools.V127.Network;
812
using OpenQA.Selenium.DevTools.V127.Performance;
913

@@ -37,14 +41,14 @@ public async Task BasicAuthentication()
3741
Assert.AreEqual("Congratulations! You must have the proper credentials.",
3842
driver.FindElement(By.TagName("p")).Text);
3943
}
40-
44+
4145
[TestMethod]
4246
public async Task RecordNetworkResponse()
4347
{
4448
var contentType = new List<string>();
4549

4650
INetwork networkInterceptor = driver.Manage().Network;
47-
networkInterceptor.NetworkResponseReceived += (_, e) =>
51+
networkInterceptor.NetworkResponseReceived += (_, e) =>
4852
{
4953
contentType.Add(e.ResponseHeaders["content-type"]);
5054
};
@@ -102,7 +106,7 @@ public async Task TransformNetworkRequest()
102106

103107
Assert.AreEqual("two", driver.FindElement(By.Id("result")).Text);
104108
}
105-
109+
106110
[TestMethod]
107111
public async Task PerformanceMetrics()
108112
{
@@ -147,5 +151,45 @@ public async Task SetCookie()
147151
Assert.AreEqual("gouda", cheese.Value);
148152
}
149153

154+
[TestMethod]
155+
public async Task WaitForDownload()
156+
{
157+
driver.Url = "https://www.selenium.dev/selenium/web/downloads/download.html";
158+
var session = ((IDevTools)driver).GetDevToolsSession();
159+
160+
var downloadPath = Path.GetTempPath();
161+
var downloadBehaviorCommandSettings = new SetDownloadBehaviorCommandSettings
162+
{
163+
Behavior = "allowAndName",
164+
BrowserContextId = null,
165+
DownloadPath = downloadPath,
166+
EventsEnabled = true
167+
};
168+
await session.SendCommand(downloadBehaviorCommandSettings);
169+
170+
var downloadCompleted = new ManualResetEvent(false);
171+
string? downloadId = null;
172+
bool downloaded = false;
173+
session.DevToolsEventReceived += (sender, args) =>
174+
{
175+
var downloadState = args.EventData["state"]?.ToString();
176+
if (args.EventName == "downloadProgress" &&
177+
(string.Equals(downloadState, "completed") ||
178+
string.Equals(downloadState, "canceled")))
179+
{
180+
downloadId = args.EventData["guid"].ToString();
181+
downloaded = downloadState.Equals("completed");
182+
downloadCompleted.Set();
183+
}
184+
};
185+
186+
driver.FindElement(By.Id("file-1")).Click();
187+
188+
Assert.IsTrue(downloadCompleted.WaitOne(TimeSpan.FromSeconds(10)));
189+
Assert.IsTrue(downloaded);
190+
var downloadedFilePath = Path.Combine(downloadPath, downloadId);
191+
Assert.IsTrue(File.Exists(downloadedFilePath));
192+
File.Delete(downloadedFilePath);
193+
}
150194
}
151195
}

website_and_docs/content/documentation/webdriver/bidi/cdp/network.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Both requests and responses can be recorded or transformed.
173173
{{< badge-implementation >}}
174174
{{% /tab %}}
175175
{{% tab header="CSharp" %}}
176-
{{< badge-implementation >}}
176+
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs#L158-L168" >}}
177177
{{% /tab %}}
178178
{{% tab header="Ruby" %}}
179179
{{< gh-codeblock path="/examples/ruby/spec/bidi/cdp/network_spec.rb#L82-L88" >}}

website_and_docs/content/documentation/webdriver/bidi/cdp/network.ja.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Both requests and responses can be recorded or transformed.
182182
{{< badge-implementation >}}
183183
{{% /tab %}}
184184
{{% tab header="CSharp" %}}
185-
{{< badge-implementation >}}
185+
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs#L158-L168" >}}
186186
{{% /tab %}}
187187
{{% tab header="Ruby" %}}
188188
{{< gh-codeblock path="/examples/ruby/spec/bidi/cdp/network_spec.rb#L82-L88" >}}

website_and_docs/content/documentation/webdriver/bidi/cdp/network.pt-br.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Both requests and responses can be recorded or transformed.
182182
{{< badge-implementation >}}
183183
{{% /tab %}}
184184
{{% tab header="CSharp" %}}
185-
{{< badge-implementation >}}
185+
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs#L158-L168" >}}
186186
{{% /tab %}}
187187
{{% tab header="Ruby" %}}
188188
{{< gh-codeblock path="/examples/ruby/spec/bidi/cdp/network_spec.rb#L82-L88" >}}

website_and_docs/content/documentation/webdriver/bidi/cdp/network.zh-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Both requests and responses can be recorded or transformed.
182182
{{< badge-implementation >}}
183183
{{% /tab %}}
184184
{{% tab header="CSharp" %}}
185-
{{< badge-implementation >}}
185+
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs#L158-L168" >}}
186186
{{% /tab %}}
187187
{{% tab header="Ruby" %}}
188188
{{< gh-codeblock path="/examples/ruby/spec/bidi/cdp/network_spec.rb#L82-L88" >}}

0 commit comments

Comments
 (0)