diff --git a/examples/dotnet/SeleniumDocs/BaseTest.cs b/examples/dotnet/SeleniumDocs/BaseTest.cs index ad061a66f9d8..d83ffd597789 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 85f115809f50..dbf7d4c43914 100644 --- a/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs +++ b/examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs @@ -1,13 +1,19 @@ +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.V129.Browser; using OpenQA.Selenium.DevTools.V129.Network; using OpenQA.Selenium.DevTools.V129.Performance; + namespace SeleniumDocs.BiDi.CDP { [TestClass] @@ -37,14 +43,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 +108,7 @@ public async Task TransformNetworkRequest() Assert.AreEqual("two", driver.FindElement(By.Id("result")).Text); } - + [TestMethod] public async Task PerformanceMetrics() { @@ -147,5 +153,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" >}}