Skip to content

Commit

Permalink
Adding WaitForDownload test for .NET
Browse files Browse the repository at this point in the history
  • Loading branch information
halex2005 committed Aug 22, 2024
1 parent 9d1f0ed commit 878b226
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/dotnet/SeleniumDocs/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected void StartDriver(string browserVersion = "stable")
{
BrowserVersion = browserVersion
};
options.AddArgument("--no-sandbox");
driver = new ChromeDriver(options);
}

Expand Down
50 changes: 47 additions & 3 deletions examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.DevTools;
using System.Linq;
using System.Threading;
using OpenQA.Selenium.DevTools.V127.Browser;
using OpenQA.Selenium.DevTools.V127.Network;
using OpenQA.Selenium.DevTools.V127.Performance;

Expand Down Expand Up @@ -37,14 +41,14 @@ public async Task BasicAuthentication()
Assert.AreEqual("Congratulations! You must have the proper credentials.",
driver.FindElement(By.TagName("p")).Text);
}

[TestMethod]
public async Task RecordNetworkResponse()
{
var contentType = new List<string>();

INetwork networkInterceptor = driver.Manage().Network;
networkInterceptor.NetworkResponseReceived += (_, e) =>
networkInterceptor.NetworkResponseReceived += (_, e) =>
{
contentType.Add(e.ResponseHeaders["content-type"]);
};
Expand Down Expand Up @@ -102,7 +106,7 @@ public async Task TransformNetworkRequest()

Assert.AreEqual("two", driver.FindElement(By.Id("result")).Text);
}

[TestMethod]
public async Task PerformanceMetrics()
{
Expand Down Expand Up @@ -147,5 +151,45 @@ public async Task SetCookie()
Assert.AreEqual("gouda", cheese.Value);
}

[TestMethod]
public async Task WaitForDownload()
{
driver.Url = "https://www.selenium.dev/selenium/web/downloads/download.html";
var session = ((IDevTools)driver).GetDevToolsSession();

var downloadPath = Path.GetTempPath();
var downloadBehaviorCommandSettings = new SetDownloadBehaviorCommandSettings
{
Behavior = "allowAndName",
BrowserContextId = null,
DownloadPath = downloadPath,
EventsEnabled = true
};
await session.SendCommand(downloadBehaviorCommandSettings);

var downloadCompleted = new ManualResetEvent(false);
string? downloadId = null;
bool downloaded = false;
session.DevToolsEventReceived += (sender, args) =>
{
var downloadState = args.EventData["state"]?.ToString();
if (args.EventName == "downloadProgress" &&
(string.Equals(downloadState, "completed") ||
string.Equals(downloadState, "canceled")))
{
downloadId = args.EventData["guid"].ToString();
downloaded = downloadState.Equals("completed");
downloadCompleted.Set();
}
};

driver.FindElement(By.Id("file-1")).Click();

Assert.IsTrue(downloadCompleted.WaitOne(TimeSpan.FromSeconds(10)));
Assert.IsTrue(downloaded);
var downloadedFilePath = Path.Combine(downloadPath, downloadId);
Assert.IsTrue(File.Exists(downloadedFilePath));
File.Delete(downloadedFilePath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Both requests and responses can be recorded or transformed.
{{< badge-implementation >}}
{{% /tab %}}
{{% tab header="CSharp" %}}
{{< badge-implementation >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs#L158-L168" >}}
{{% /tab %}}
{{% tab header="Ruby" %}}
{{< gh-codeblock path="/examples/ruby/spec/bidi/cdp/network_spec.rb#L82-L88" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Both requests and responses can be recorded or transformed.
{{< badge-implementation >}}
{{% /tab %}}
{{% tab header="CSharp" %}}
{{< badge-implementation >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs#L158-L168" >}}
{{% /tab %}}
{{% tab header="Ruby" %}}
{{< gh-codeblock path="/examples/ruby/spec/bidi/cdp/network_spec.rb#L82-L88" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Both requests and responses can be recorded or transformed.
{{< badge-implementation >}}
{{% /tab %}}
{{% tab header="CSharp" %}}
{{< badge-implementation >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs#L158-L168" >}}
{{% /tab %}}
{{% tab header="Ruby" %}}
{{< gh-codeblock path="/examples/ruby/spec/bidi/cdp/network_spec.rb#L82-L88" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Both requests and responses can be recorded or transformed.
{{< badge-implementation >}}
{{% /tab %}}
{{% tab header="CSharp" %}}
{{< badge-implementation >}}
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs#L158-L168" >}}
{{% /tab %}}
{{% tab header="Ruby" %}}
{{< gh-codeblock path="/examples/ruby/spec/bidi/cdp/network_spec.rb#L82-L88" >}}
Expand Down

0 comments on commit 878b226

Please sign in to comment.