From 167d479bb1663bcb93fea905cbc419f5a6b5fbfb Mon Sep 17 00:00:00 2001 From: akharlov Date: Thu, 22 Aug 2024 22:06:18 +0500 Subject: [PATCH] Adding WaitForDownload test for .NET --- examples/dotnet/SeleniumDocs/BaseTest.cs | 1 + .../SeleniumDocs/BiDi/CDP/NetworkTest.cs | 50 +++++++++++++++++-- .../webdriver/bidi/cdp/network.en.md | 2 +- .../webdriver/bidi/cdp/network.ja.md | 2 +- .../webdriver/bidi/cdp/network.pt-br.md | 2 +- .../webdriver/bidi/cdp/network.zh-cn.md | 2 +- 6 files changed, 52 insertions(+), 7 deletions(-) diff --git a/examples/dotnet/SeleniumDocs/BaseTest.cs b/examples/dotnet/SeleniumDocs/BaseTest.cs index d09a6d72e9b3..686f1d98013e 100644 --- a/examples/dotnet/SeleniumDocs/BaseTest.cs +++ b/examples/dotnet/SeleniumDocs/BaseTest.cs @@ -39,6 +39,7 @@ protected void StartDriver(string browserVersion = "stable") { BrowserVersion = browserVersion }; + options.AddArgument("--no-sandbox"); driver = new ChromeDriver(options); } diff --git a/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs b/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs index 5ed1b236d272..6906c219e0f9 100644 --- a/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs +++ b/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs @@ -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; @@ -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(); INetwork networkInterceptor = driver.Manage().Network; - networkInterceptor.NetworkResponseReceived += (_, e) => + networkInterceptor.NetworkResponseReceived += (_, e) => { contentType.Add(e.ResponseHeaders["content-type"]); }; @@ -102,7 +106,7 @@ public async Task TransformNetworkRequest() Assert.AreEqual("two", driver.FindElement(By.Id("result")).Text); } - + [TestMethod] public async Task PerformanceMetrics() { @@ -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); + } } } \ No newline at end of file diff --git a/website_and_docs/content/documentation/webdriver/bidi/cdp/network.en.md b/website_and_docs/content/documentation/webdriver/bidi/cdp/network.en.md index 777c3c978709..af83d7f98647 100644 --- a/website_and_docs/content/documentation/webdriver/bidi/cdp/network.en.md +++ b/website_and_docs/content/documentation/webdriver/bidi/cdp/network.en.md @@ -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" >}} diff --git a/website_and_docs/content/documentation/webdriver/bidi/cdp/network.ja.md b/website_and_docs/content/documentation/webdriver/bidi/cdp/network.ja.md index 1b9819ae3a21..e59238632213 100644 --- a/website_and_docs/content/documentation/webdriver/bidi/cdp/network.ja.md +++ b/website_and_docs/content/documentation/webdriver/bidi/cdp/network.ja.md @@ -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" >}} diff --git a/website_and_docs/content/documentation/webdriver/bidi/cdp/network.pt-br.md b/website_and_docs/content/documentation/webdriver/bidi/cdp/network.pt-br.md index abcc6812d5a6..a0dbab163049 100644 --- a/website_and_docs/content/documentation/webdriver/bidi/cdp/network.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/bidi/cdp/network.pt-br.md @@ -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" >}} diff --git a/website_and_docs/content/documentation/webdriver/bidi/cdp/network.zh-cn.md b/website_and_docs/content/documentation/webdriver/bidi/cdp/network.zh-cn.md index 1932f83c8f77..121fe4133ca8 100644 --- a/website_and_docs/content/documentation/webdriver/bidi/cdp/network.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/bidi/cdp/network.zh-cn.md @@ -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" >}}