Skip to content

Commit 007b0e8

Browse files
mialeskaAliaksej Mialeshka
andauthored
[Enhancement] Use native function to open new tab (#237)
* Use native function to open new tab - add functions ..ViaJs to access previous behavior - add overload accepting Uri parameter - fix typo in the documentation - refactor BrowserNavigation and BrowserTabNavigation initialization to allow inheritance - use native ExecuteScript functions instead of AqualityServices wrappers for correct constructor parameter usage - add tests for new functions closes #231 * Update Selenium package versions * Add retry on test task * Update TheInternetForm.cs to use https address * Restore theIntenetForm URL and rework the check in BrowserConcurrencyTests * Fix locator and change theInternet url once again * Change TheInternetForm URL to be HTTPS once again * Stabilize async actions in DevToolsEmulationTests * Update selenium version * Handle ChromeDriver session not created issue: https://groups.google.com/g/chromedriver-users/c/Dgv9xRHZf58 * Add temporary workaround to avoid navigation issue described at SeleniumHQ/selenium#12277 * Try to stabilize Should_BePossibleTo_CheckThatHiddenElementsNotDisplayed * Use different vmImage: 'windows-2019' --------- Co-authored-by: Aliaksej Mialeshka <[email protected]>
1 parent 7d24120 commit 007b0e8

17 files changed

+196
-71
lines changed

Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
</ItemGroup>
7777

7878
<ItemGroup>
79-
<PackageReference Include="Aquality.Selenium.Core" Version="3.0.1" />
79+
<PackageReference Include="Aquality.Selenium.Core" Version="3.0.3" />
8080
<PackageReference Include="WebDriverManager" Version="2.17.1" />
8181
</ItemGroup>
8282

Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void Refresh()
216216

217217
private INavigation Navigate()
218218
{
219-
return new BrowserNavigation(Driver);
219+
return new BrowserNavigation(Driver, Logger);
220220
}
221221

222222
/// <summary>
@@ -225,7 +225,7 @@ private INavigation Navigate()
225225
/// <returns>Instance of IBrowserTabNavigation.</returns>
226226
public IBrowserTabNavigation Tabs()
227227
{
228-
return new BrowserTabNavigation(Driver);
228+
return new BrowserTabNavigation(Driver, Logger);
229229
}
230230

231231
/// <summary>

Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Aquality.Selenium.Core.Localization;
33
using Aquality.Selenium.Core.Utilities;
44
using OpenQA.Selenium;
5+
using System;
56

67
namespace Aquality.Selenium.Browsers
78
{
@@ -29,7 +30,7 @@ public virtual Browser Browser
2930
{
3031
get
3132
{
32-
var browser = new Browser(ActionRetrier.DoWithRetry(() => Driver, new[] { typeof(WebDriverException) }));
33+
var browser = new Browser(ActionRetrier.DoWithRetry(() => Driver, new[] { typeof(WebDriverException), typeof(InvalidOperationException) }));
3334
LocalizedLogger.Info("loc.browser.ready", BrowserProfile.BrowserName);
3435
return browser;
3536
}

Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserNavigation.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ public class BrowserNavigation : INavigation
1111
{
1212
private readonly WebDriver driver;
1313

14-
internal BrowserNavigation(WebDriver driver)
14+
protected internal BrowserNavigation(WebDriver driver, ILocalizedLogger logger)
1515
{
1616
this.driver = driver;
17+
Logger = logger;
1718
}
1819

19-
private ILocalizedLogger Logger => AqualityServices.LocalizedLogger;
20+
private ILocalizedLogger Logger { get; }
2021

2122
/// <summary>
2223
/// Navigates back.
@@ -43,7 +44,15 @@ public void Forward()
4344
public void GoToUrl(string url)
4445
{
4546
InfoLocNavigate(url);
46-
driver.Navigate().GoToUrl(url);
47+
// temporary workaround to avoid issue described at https://github.com/SeleniumHQ/selenium/issues/12277
48+
try
49+
{
50+
driver.Navigate().GoToUrl(url);
51+
}
52+
catch (WebDriverException e) when (driver.Url == url)
53+
{
54+
Logger.Fatal($"Navigation error occurred: [{e.Message}], but successfully navigated to URL [{url}]", e);
55+
}
4756
}
4857

4958
/// <summary>
@@ -53,7 +62,14 @@ public void GoToUrl(string url)
5362
public void GoToUrl(Uri url)
5463
{
5564
InfoLocNavigate(url.ToString());
56-
driver.Navigate().GoToUrl(url);
65+
try
66+
{
67+
driver.Navigate().GoToUrl(url);
68+
}
69+
catch (WebDriverException e) when (driver.Url == url.ToString())
70+
{
71+
Logger.Fatal($"Navigation error occurred: [{e.Message}], but successfully navigated to URL [{url}]", e);
72+
}
5773
}
5874

5975
/// <summary>

Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserTabNavigation.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ public class BrowserTabNavigation : IBrowserTabNavigation
1010
{
1111
private readonly WebDriver driver;
1212

13-
internal BrowserTabNavigation(WebDriver driver)
13+
protected internal BrowserTabNavigation(WebDriver driver, ILocalizedLogger logger)
1414
{
1515
this.driver = driver;
16+
Logger = logger;
1617
}
1718

18-
private ILocalizedLogger Logger => AqualityServices.LocalizedLogger;
19+
private ILocalizedLogger Logger { get; }
1920

2021
public string CurrentTabHandle
2122
{
@@ -44,7 +45,18 @@ public void CloseTab()
4445
public void OpenNewTab(bool switchToNew = true)
4546
{
4647
Logger.Info("loc.browser.tab.open.new");
47-
AqualityServices.Browser.ExecuteScript(JavaScript.OpenNewTab);
48+
var currentHandle = switchToNew ? null : CurrentTabHandle;
49+
driver.SwitchTo().NewWindow(WindowType.Tab);
50+
if (!switchToNew)
51+
{
52+
CloseAndSwitch(currentHandle, closeCurrent: false);
53+
}
54+
}
55+
56+
public void OpenNewTabViaJs(bool switchToNew = true)
57+
{
58+
Logger.Info("loc.browser.tab.open.new");
59+
driver.ExecuteScript(JavaScript.OpenNewTab.GetScript());
4860
if (switchToNew)
4961
{
5062
SwitchToLastTab();
@@ -53,7 +65,19 @@ public void OpenNewTab(bool switchToNew = true)
5365

5466
public void OpenInNewTab(string url)
5567
{
56-
AqualityServices.Browser.ExecuteScript(JavaScript.OpenInNewTab, url);
68+
OpenNewTab(switchToNew: true);
69+
driver.Navigate().GoToUrl(url);
70+
}
71+
72+
public void OpenInNewTab(Uri url)
73+
{
74+
OpenNewTab(switchToNew: true);
75+
driver.Navigate().GoToUrl(url);
76+
}
77+
78+
public void OpenInNewTabViaJs(string url)
79+
{
80+
driver.ExecuteScript(JavaScript.OpenInNewTab.GetScript(), url);
5781
}
5882

5983
public void SwitchToLastTab(bool closeCurrent = false)
@@ -78,7 +102,7 @@ public void SwitchToTab(int index, bool closeCurrent = false)
78102
$"Index of browser tab '{index}' you provided is out of range {0}..{names.Count}");
79103
}
80104

81-
var newTab = names.ElementAt(index);
105+
var newTab = names[index];
82106
CloseAndSwitch(newTab, closeCurrent);
83107
}
84108

Aquality.Selenium/src/Aquality.Selenium/Browsers/IBrowserTabNavigation.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace Aquality.Selenium.Browsers
45
{
@@ -40,7 +41,7 @@ public interface IBrowserTabNavigation
4041
void SwitchToLastTab(bool closeCurrent = false);
4142

4243
/// <summary>
43-
/// Closes curent tab.
44+
/// Closes current tab.
4445
/// </summary>
4546
void CloseTab();
4647

@@ -50,10 +51,28 @@ public interface IBrowserTabNavigation
5051
/// <param name="switchToNew">Switches to new tab if true and stays at current otherwise.</param>
5152
void OpenNewTab(bool switchToNew = true);
5253

54+
/// <summary>
55+
/// Opens new tab using JS function.
56+
/// </summary>
57+
/// <param name="switchToNew">Switches to new tab if true and stays at current otherwise.</param>
58+
void OpenNewTabViaJs(bool switchToNew = true);
59+
5360
/// <summary>
5461
/// Navigates to desired url in new tab.
5562
/// </summary>
5663
/// <param name="url">String representation of URL.</param>
5764
void OpenInNewTab(string url);
65+
66+
/// <summary>
67+
/// Navigates to desired url in new tab.
68+
/// </summary>
69+
/// <param name="url">target URL.</param>
70+
void OpenInNewTab(Uri url);
71+
72+
/// <summary>
73+
/// Navigates to desired url in new tab using JS function.
74+
/// </summary>
75+
/// <param name="url">String representation of URL.</param>
76+
void OpenInNewTabViaJs(string url);
5877
}
5978
}

Aquality.Selenium/tests/Aquality.Selenium.Tests/Aquality.Selenium.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<PrivateAssets>all</PrivateAssets>
3535
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3636
</PackageReference>
37-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
37+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
3838
</ItemGroup>
3939

4040
<ItemGroup>

Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/BrowserTabsTests.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,29 @@ public void Should_BePossibleTo_OpenUrlInNewTab()
2222
var url = new WelcomeForm().Url;
2323
var browser = AqualityServices.Browser;
2424
browser.Tabs().OpenInNewTab(url);
25-
browser.Tabs().SwitchToLastTab();
2625
Assert.AreEqual(2, browser.Tabs().TabHandles.Count);
2726
Assert.AreEqual(browser.Driver.Url, url);
2827
}
28+
29+
[Test]
30+
public void Should_BePossibleTo_OpenUrlInNewTab_ViaJs()
31+
{
32+
var url = new WelcomeForm().Url;
33+
var browser = AqualityServices.Browser;
34+
browser.Tabs().OpenInNewTabViaJs(url);
35+
Assert.AreEqual(2, browser.Tabs().TabHandles.Count);
36+
Assert.AreEqual(browser.Driver.Url, url);
37+
}
38+
39+
[Test]
40+
public void Should_BePossibleTo_OpenUriInNewTab()
41+
{
42+
var url = new Uri(new WelcomeForm().Url);
43+
var browser = AqualityServices.Browser;
44+
browser.Tabs().OpenInNewTab(url);
45+
Assert.AreEqual(2, browser.Tabs().TabHandles.Count);
46+
Assert.AreEqual(new Uri(browser.Driver.Url), url);
47+
}
2948

3049
[Test]
3150
public void Should_BePossibleTo_HandleTab()
@@ -60,6 +79,22 @@ public void Should_BePossibleTo_OpenNewTab()
6079
Assert.AreEqual(newTabHandle, browser.Tabs().CurrentTabHandle, "Browser should not be switched to new tab");
6180
}
6281

82+
[Test]
83+
public void Should_BePossibleTo_OpenNewTab_ViaJs()
84+
{
85+
var browser = AqualityServices.Browser;
86+
var tabHandle = browser.Tabs().CurrentTabHandle;
87+
88+
browser.Tabs().OpenNewTabViaJs();
89+
var newTabHandle = browser.Tabs().CurrentTabHandle;
90+
Assert.AreEqual(2, browser.Tabs().TabHandles.Count, "New tab should be opened");
91+
Assert.AreNotEqual(tabHandle, newTabHandle, "Browser should be switched to new tab");
92+
93+
browser.Tabs().OpenNewTabViaJs(false);
94+
Assert.AreEqual(3, browser.Tabs().TabHandles.Count, "New tab should be opened");
95+
Assert.AreEqual(newTabHandle, browser.Tabs().CurrentTabHandle, "Browser should not be switched to new tab");
96+
}
97+
6398
[Test]
6499
public void Should_BePossibleTo_CloseTab()
65100
{

0 commit comments

Comments
 (0)