Skip to content

Commit 38a94b6

Browse files
committed
[dotnet] implement navigation commands with WebDriver BiDi
1 parent dc502b6 commit 38a94b6

File tree

2 files changed

+63
-14
lines changed

2 files changed

+63
-14
lines changed

Diff for: dotnet/src/webdriver/Navigator.cs

+47-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System;
2020
using System.Collections.Generic;
2121
using System.Threading.Tasks;
22+
using WebDriverBiDi.BrowsingContext;
2223

2324
namespace OpenQA.Selenium
2425
{
@@ -28,6 +29,7 @@ namespace OpenQA.Selenium
2829
internal class Navigator : INavigation
2930
{
3031
private WebDriver driver;
32+
private string browsingContextId;
3133

3234
/// <summary>
3335
/// Initializes a new instance of the <see cref="Navigator"/> class
@@ -36,6 +38,8 @@ internal class Navigator : INavigation
3638
public Navigator(WebDriver driver)
3739
{
3840
this.driver = driver;
41+
// TODO: store the value of the current window's context id on the driver object
42+
this.browsingContextId = driver.CurrentWindowHandle;
3943
}
4044

4145
/// <summary>
@@ -52,7 +56,17 @@ public void Back()
5256
/// <returns>A task object representing the asynchronous operation.</returns>
5357
public async Task BackAsync()
5458
{
55-
await this.driver.InternalExecuteAsync(DriverCommand.GoBack, null).ConfigureAwait(false);
59+
if (this.driver.BiDiDriver != null)
60+
{
61+
var traverseHistoryCommandParameters =
62+
new TraverseHistoryCommandParameters(this.browsingContextId, -1);
63+
await this.driver.BiDiDriver.BrowsingContext.TraverseHistoryAsync(traverseHistoryCommandParameters)
64+
.ConfigureAwait(false);
65+
}
66+
else
67+
{
68+
await this.driver.InternalExecuteAsync(DriverCommand.GoBack, null).ConfigureAwait(false);
69+
}
5670
}
5771

5872
/// <summary>
@@ -69,7 +83,17 @@ public void Forward()
6983
/// <returns>A task object representing the asynchronous operation.</returns>
7084
public async Task ForwardAsync()
7185
{
72-
await this.driver.InternalExecuteAsync(DriverCommand.GoForward, null).ConfigureAwait(false);
86+
if (this.driver.BiDiDriver != null)
87+
{
88+
var traverseHistoryCommandParameters =
89+
new TraverseHistoryCommandParameters(this.browsingContextId, 1);
90+
await this.driver.BiDiDriver.BrowsingContext.TraverseHistoryAsync(traverseHistoryCommandParameters)
91+
.ConfigureAwait(false);
92+
}
93+
else
94+
{
95+
await this.driver.InternalExecuteAsync(DriverCommand.GoForward, null).ConfigureAwait(false);
96+
}
7397
}
7498

7599
/// <summary>
@@ -93,11 +117,18 @@ public async Task GoToUrlAsync(string url)
93117
throw new ArgumentNullException(nameof(url), "URL cannot be null.");
94118
}
95119

96-
Dictionary<string, object> parameters = new Dictionary<string, object>
120+
if (this.driver.BiDiDriver != null)
97121
{
98-
{ "url", url }
99-
};
100-
await this.driver.InternalExecuteAsync(DriverCommand.Get, parameters).ConfigureAwait(false);
122+
await driver.BiDiDriver.BrowsingContext.NavigateAsync(new NavigateCommandParameters(this.browsingContextId, url)).ConfigureAwait(false);
123+
}
124+
else
125+
{
126+
Dictionary<string, object> parameters = new Dictionary<string, object>
127+
{
128+
{ "url", url }
129+
};
130+
await this.driver.InternalExecuteAsync(DriverCommand.Get, parameters).ConfigureAwait(false);
131+
}
101132
}
102133

103134
/// <summary>
@@ -138,8 +169,16 @@ public void Refresh()
138169
/// <returns>A task object representing the asynchronous operation.</returns>
139170
public async Task RefreshAsync()
140171
{
141-
// driver.SwitchTo().DefaultContent();
142-
await this.driver.InternalExecuteAsync(DriverCommand.Refresh, null).ConfigureAwait(false);
172+
if (this.driver.BiDiDriver != null)
173+
{
174+
var reloadCommandParameters =
175+
new ReloadCommandParameters(this.browsingContextId);
176+
await this.driver.BiDiDriver.BrowsingContext.ReloadAsync(reloadCommandParameters).ConfigureAwait(false);
177+
}
178+
else
179+
{
180+
await this.driver.InternalExecuteAsync(DriverCommand.Refresh, null).ConfigureAwait(false);
181+
}
143182
}
144183
}
145184
}

Diff for: dotnet/test/common/NavigationTest.cs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using NUnit.Framework;
22
using System;
3+
using WebDriverBiDi;
34

45
namespace OpenQA.Selenium
56
{
@@ -12,10 +13,20 @@ public class NavigationTest : DriverTestFixture
1213
[NeedsFreshDriver(IsCreatedBeforeTest = true)]
1314
public void ShouldNotHaveProblemNavigatingWithNoPagesBrowsed()
1415
{
15-
INavigation navigation;
16-
navigation = driver.Navigate();
17-
navigation.Back();
18-
navigation.Forward();
16+
INavigation navigation = driver.Navigate();
17+
18+
if (((WebDriver)driver).Capabilities.HasCapability("webSocketUrl"))
19+
{
20+
var ex1 = Assert.Throws<WebDriverBiDiException>(() => navigation.Back());
21+
Assert.True(ex1!.Message.Contains("no such history entry"));
22+
var ex2 = Assert.Throws<WebDriverBiDiException>(() => navigation.Forward());
23+
Assert.True(ex2!.Message.Contains("no such history entry"));
24+
}
25+
else
26+
{
27+
navigation.Back();
28+
navigation.Forward();
29+
}
1930
}
2031

2132
[Test]
@@ -40,7 +51,7 @@ public void ShouldAcceptInvalidUrlsUsingUris()
4051
INavigation navigation;
4152
navigation = driver.Navigate();
4253
Assert.That(() => navigation.GoToUrl((Uri)null), Throws.InstanceOf<ArgumentNullException>());
43-
// new Uri("") and new Uri("isidsji30342??éåµñ©æ")
54+
// new Uri("") and new Uri("isidsji30342??éåµñ©æ")
4455
// throw an exception, so we needn't worry about them.
4556
}
4657

@@ -89,6 +100,5 @@ public void ShouldRefreshPage()
89100
changedDiv = driver.FindElement(By.Id("dynamo"));
90101
Assert.AreEqual("What's for dinner?", changedDiv.Text);
91102
}
92-
93103
}
94104
}

0 commit comments

Comments
 (0)