From 34b5dd8ec98438c6f3ae1d773fc825cc0330b0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Thu, 5 Dec 2024 18:31:27 -0300 Subject: [PATCH] Bidi: Page goback and goforward --- .../TestExpectations.local.json | 15 ----------- lib/PuppeteerSharp/Bidi/BidiPage.cs | 27 +++++++++++++++++-- .../Bidi/Core/BrowsingContext.cs | 3 +++ 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json b/lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json index 15856ef69..f3a7c31df 100644 --- a/lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json +++ b/lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json @@ -270,21 +270,6 @@ "FAIL" ] }, - { - "comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one", - "testIdPattern": "[navigation.spec] navigation Page.goBack *", - "platforms": [ - "darwin", - "linux", - "win32" - ], - "parameters": [ - "webDriverBiDi" - ], - "expectations": [ - "FAIL" - ] - }, { "comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one", "testIdPattern": "[navigation.spec] *timeout*", diff --git a/lib/PuppeteerSharp/Bidi/BidiPage.cs b/lib/PuppeteerSharp/Bidi/BidiPage.cs index ca113c89d..bc9cd47f7 100644 --- a/lib/PuppeteerSharp/Bidi/BidiPage.cs +++ b/lib/PuppeteerSharp/Bidi/BidiPage.cs @@ -23,6 +23,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using PuppeteerSharp.Bidi.Core; using PuppeteerSharp.Helpers; @@ -105,10 +106,10 @@ internal BidiPage(BidiBrowserContext browserContext, BrowsingContext browsingCon public override Task WaitForFileChooserAsync(WaitForOptions options = null) => throw new NotImplementedException(); /// - public override Task GoBackAsync(NavigationOptions options = null) => throw new NotImplementedException(); + public override Task GoBackAsync(NavigationOptions options = null) => GoAsync(-1, options); /// - public override Task GoForwardAsync(NavigationOptions options = null) => throw new NotImplementedException(); + public override Task GoForwardAsync(NavigationOptions options = null) => GoAsync(1, options); /// public override Task SetBurstModeOffAsync() => throw new NotImplementedException(); @@ -211,6 +212,28 @@ internal static BidiPage From(BidiBrowserContext browserContext, BrowsingContext /// protected override Task ExposeFunctionAsync(string name, Delegate puppeteerFunction) => throw new NotImplementedException(); + private async Task GoAsync(int delta, NavigationOptions options) + { + var waitForNavigationTask = WaitForNavigationAsync(options); + var navigationTask = BidiMainFrame.BrowsingContext.TraverseHistoryAsync(delta); + + try + { + await Task.WhenAll(waitForNavigationTask, navigationTask).ConfigureAwait(false); + } + catch (Exception ex) + { + if (ex.Message.Contains("no such history entry")) + { + return null; + } + + throw new NavigationException(ex.Message, ex); + } + + return waitForNavigationTask.Result; + } + private void Initialize() { } diff --git a/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs b/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs index b6b0ae65d..68de760e6 100644 --- a/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs +++ b/lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs @@ -141,6 +141,9 @@ internal WindowRealm CreateWindowRealm(string sandbox = null) return realm; } + internal async Task TraverseHistoryAsync(int delta) + => await Session.Driver.BrowsingContext.TraverseHistoryAsync(new TraverseHistoryCommandParameters(Id, delta)).ConfigureAwait(false); + protected virtual void OnBrowsingContextCreated(BidiBrowsingContextEventArgs e) => BrowsingContextCreated?.Invoke(this, e); private void Initialize()