Skip to content

Commit

Permalink
[Enhancement] Use native function to open new tab (#237)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
mialeska and Aliaksej Mialeshka authored Sep 6, 2023
1 parent 7d24120 commit 007b0e8
Show file tree
Hide file tree
Showing 17 changed files with 196 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aquality.Selenium.Core" Version="3.0.1" />
<PackageReference Include="Aquality.Selenium.Core" Version="3.0.3" />
<PackageReference Include="WebDriverManager" Version="2.17.1" />
</ItemGroup>

Expand Down
20 changes: 19 additions & 1 deletion Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Aquality.Selenium/src/Aquality.Selenium/Browsers/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void Refresh()

private INavigation Navigate()
{
return new BrowserNavigation(Driver);
return new BrowserNavigation(Driver, Logger);
}

/// <summary>
Expand All @@ -225,7 +225,7 @@ private INavigation Navigate()
/// <returns>Instance of IBrowserTabNavigation.</returns>
public IBrowserTabNavigation Tabs()
{
return new BrowserTabNavigation(Driver);
return new BrowserTabNavigation(Driver, Logger);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Aquality.Selenium.Core.Localization;
using Aquality.Selenium.Core.Utilities;
using OpenQA.Selenium;
using System;

namespace Aquality.Selenium.Browsers
{
Expand Down Expand Up @@ -29,7 +30,7 @@ public virtual Browser Browser
{
get
{
var browser = new Browser(ActionRetrier.DoWithRetry(() => Driver, new[] { typeof(WebDriverException) }));
var browser = new Browser(ActionRetrier.DoWithRetry(() => Driver, new[] { typeof(WebDriverException), typeof(InvalidOperationException) }));
LocalizedLogger.Info("loc.browser.ready", BrowserProfile.BrowserName);
return browser;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public class BrowserNavigation : INavigation
{
private readonly WebDriver driver;

internal BrowserNavigation(WebDriver driver)
protected internal BrowserNavigation(WebDriver driver, ILocalizedLogger logger)
{
this.driver = driver;
Logger = logger;
}

private ILocalizedLogger Logger => AqualityServices.LocalizedLogger;
private ILocalizedLogger Logger { get; }

/// <summary>
/// Navigates back.
Expand All @@ -43,7 +44,15 @@ public void Forward()
public void GoToUrl(string url)
{
InfoLocNavigate(url);
driver.Navigate().GoToUrl(url);
// temporary workaround to avoid issue described at https://github.com/SeleniumHQ/selenium/issues/12277
try
{
driver.Navigate().GoToUrl(url);
}
catch (WebDriverException e) when (driver.Url == url)
{
Logger.Fatal($"Navigation error occurred: [{e.Message}], but successfully navigated to URL [{url}]", e);
}
}

/// <summary>
Expand All @@ -53,7 +62,14 @@ public void GoToUrl(string url)
public void GoToUrl(Uri url)
{
InfoLocNavigate(url.ToString());
driver.Navigate().GoToUrl(url);
try
{
driver.Navigate().GoToUrl(url);
}
catch (WebDriverException e) when (driver.Url == url.ToString())
{
Logger.Fatal($"Navigation error occurred: [{e.Message}], but successfully navigated to URL [{url}]", e);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ public class BrowserTabNavigation : IBrowserTabNavigation
{
private readonly WebDriver driver;

internal BrowserTabNavigation(WebDriver driver)
protected internal BrowserTabNavigation(WebDriver driver, ILocalizedLogger logger)
{
this.driver = driver;
Logger = logger;
}

private ILocalizedLogger Logger => AqualityServices.LocalizedLogger;
private ILocalizedLogger Logger { get; }

public string CurrentTabHandle
{
Expand Down Expand Up @@ -44,7 +45,18 @@ public void CloseTab()
public void OpenNewTab(bool switchToNew = true)
{
Logger.Info("loc.browser.tab.open.new");
AqualityServices.Browser.ExecuteScript(JavaScript.OpenNewTab);
var currentHandle = switchToNew ? null : CurrentTabHandle;
driver.SwitchTo().NewWindow(WindowType.Tab);
if (!switchToNew)
{
CloseAndSwitch(currentHandle, closeCurrent: false);
}
}

public void OpenNewTabViaJs(bool switchToNew = true)
{
Logger.Info("loc.browser.tab.open.new");
driver.ExecuteScript(JavaScript.OpenNewTab.GetScript());
if (switchToNew)
{
SwitchToLastTab();
Expand All @@ -53,7 +65,19 @@ public void OpenNewTab(bool switchToNew = true)

public void OpenInNewTab(string url)
{
AqualityServices.Browser.ExecuteScript(JavaScript.OpenInNewTab, url);
OpenNewTab(switchToNew: true);
driver.Navigate().GoToUrl(url);
}

public void OpenInNewTab(Uri url)
{
OpenNewTab(switchToNew: true);
driver.Navigate().GoToUrl(url);
}

public void OpenInNewTabViaJs(string url)
{
driver.ExecuteScript(JavaScript.OpenInNewTab.GetScript(), url);
}

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

var newTab = names.ElementAt(index);
var newTab = names[index];
CloseAndSwitch(newTab, closeCurrent);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Aquality.Selenium.Browsers
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public interface IBrowserTabNavigation
void SwitchToLastTab(bool closeCurrent = false);

/// <summary>
/// Closes curent tab.
/// Closes current tab.
/// </summary>
void CloseTab();

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

/// <summary>
/// Opens new tab using JS function.
/// </summary>
/// <param name="switchToNew">Switches to new tab if true and stays at current otherwise.</param>
void OpenNewTabViaJs(bool switchToNew = true);

/// <summary>
/// Navigates to desired url in new tab.
/// </summary>
/// <param name="url">String representation of URL.</param>
void OpenInNewTab(string url);

/// <summary>
/// Navigates to desired url in new tab.
/// </summary>
/// <param name="url">target URL.</param>
void OpenInNewTab(Uri url);

/// <summary>
/// Navigates to desired url in new tab using JS function.
/// </summary>
/// <param name="url">String representation of URL.</param>
void OpenInNewTabViaJs(string url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,29 @@ public void Should_BePossibleTo_OpenUrlInNewTab()
var url = new WelcomeForm().Url;
var browser = AqualityServices.Browser;
browser.Tabs().OpenInNewTab(url);
browser.Tabs().SwitchToLastTab();
Assert.AreEqual(2, browser.Tabs().TabHandles.Count);
Assert.AreEqual(browser.Driver.Url, url);
}

[Test]
public void Should_BePossibleTo_OpenUrlInNewTab_ViaJs()
{
var url = new WelcomeForm().Url;
var browser = AqualityServices.Browser;
browser.Tabs().OpenInNewTabViaJs(url);
Assert.AreEqual(2, browser.Tabs().TabHandles.Count);
Assert.AreEqual(browser.Driver.Url, url);
}

[Test]
public void Should_BePossibleTo_OpenUriInNewTab()
{
var url = new Uri(new WelcomeForm().Url);
var browser = AqualityServices.Browser;
browser.Tabs().OpenInNewTab(url);
Assert.AreEqual(2, browser.Tabs().TabHandles.Count);
Assert.AreEqual(new Uri(browser.Driver.Url), url);
}

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

[Test]
public void Should_BePossibleTo_OpenNewTab_ViaJs()
{
var browser = AqualityServices.Browser;
var tabHandle = browser.Tabs().CurrentTabHandle;

browser.Tabs().OpenNewTabViaJs();
var newTabHandle = browser.Tabs().CurrentTabHandle;
Assert.AreEqual(2, browser.Tabs().TabHandles.Count, "New tab should be opened");
Assert.AreNotEqual(tabHandle, newTabHandle, "Browser should be switched to new tab");

browser.Tabs().OpenNewTabViaJs(false);
Assert.AreEqual(3, browser.Tabs().TabHandles.Count, "New tab should be opened");
Assert.AreEqual(newTabHandle, browser.Tabs().CurrentTabHandle, "Browser should not be switched to new tab");
}

[Test]
public void Should_BePossibleTo_CloseTab()
{
Expand Down
Loading

0 comments on commit 007b0e8

Please sign in to comment.