Skip to content

Commit e76e23c

Browse files
authored
Bidi: Page goback and goforward (#2838)
1 parent fd48af1 commit e76e23c

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -285,21 +285,6 @@
285285
"FAIL"
286286
]
287287
},
288-
{
289-
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
290-
"testIdPattern": "[navigation.spec] navigation Page.goBack *",
291-
"platforms": [
292-
"darwin",
293-
"linux",
294-
"win32"
295-
],
296-
"parameters": [
297-
"webDriverBiDi"
298-
],
299-
"expectations": [
300-
"FAIL"
301-
]
302-
},
303288
{
304289
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
305290
"testIdPattern": "[navigation.spec] *timeout*",

lib/PuppeteerSharp/Bidi/BidiPage.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System;
2424
using System.Collections.Concurrent;
2525
using System.Collections.Generic;
26+
using System.Linq;
2627
using System.Threading.Tasks;
2728
using PuppeteerSharp.Bidi.Core;
2829
using PuppeteerSharp.Helpers;
@@ -105,10 +106,10 @@ internal BidiPage(BidiBrowserContext browserContext, BrowsingContext browsingCon
105106
public override Task<FileChooser> WaitForFileChooserAsync(WaitForOptions options = null) => throw new NotImplementedException();
106107

107108
/// <inheritdoc />
108-
public override Task<IResponse> GoBackAsync(NavigationOptions options = null) => throw new NotImplementedException();
109+
public override Task<IResponse> GoBackAsync(NavigationOptions options = null) => GoAsync(-1, options);
109110

110111
/// <inheritdoc />
111-
public override Task<IResponse> GoForwardAsync(NavigationOptions options = null) => throw new NotImplementedException();
112+
public override Task<IResponse> GoForwardAsync(NavigationOptions options = null) => GoAsync(1, options);
112113

113114
/// <inheritdoc />
114115
public override Task SetBurstModeOffAsync() => throw new NotImplementedException();
@@ -211,6 +212,28 @@ internal static BidiPage From(BidiBrowserContext browserContext, BrowsingContext
211212
/// <inheritdoc />
212213
protected override Task ExposeFunctionAsync(string name, Delegate puppeteerFunction) => throw new NotImplementedException();
213214

215+
private async Task<IResponse> GoAsync(int delta, NavigationOptions options)
216+
{
217+
var waitForNavigationTask = WaitForNavigationAsync(options);
218+
var navigationTask = BidiMainFrame.BrowsingContext.TraverseHistoryAsync(delta);
219+
220+
try
221+
{
222+
await Task.WhenAll(waitForNavigationTask, navigationTask).ConfigureAwait(false);
223+
}
224+
catch (Exception ex)
225+
{
226+
if (ex.Message.Contains("no such history entry"))
227+
{
228+
return null;
229+
}
230+
231+
throw new NavigationException(ex.Message, ex);
232+
}
233+
234+
return waitForNavigationTask.Result;
235+
}
236+
214237
private void Initialize()
215238
{
216239
}

lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ internal WindowRealm CreateWindowRealm(string sandbox = null)
141141
return realm;
142142
}
143143

144+
internal async Task TraverseHistoryAsync(int delta)
145+
=> await Session.Driver.BrowsingContext.TraverseHistoryAsync(new TraverseHistoryCommandParameters(Id, delta)).ConfigureAwait(false);
146+
144147
protected virtual void OnBrowsingContextCreated(BidiBrowsingContextEventArgs e) => BrowsingContextCreated?.Invoke(this, e);
145148

146149
private void Initialize()

0 commit comments

Comments
 (0)