-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browser tab management +semver: feature (#172)
* add BrowserTabNavigation with unuit tests * move js scripts to files add possibility to open url in new tab with test * #168 fix review issues * #168 fix review comments * #168 SwitchToTab -> SwitchToTab * #168 SwitchToTab -> SwitchToLastTab
- Loading branch information
Showing
12 changed files
with
357 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
Aquality.Selenium/src/Aquality.Selenium/Browsers/BrowserTabNavigation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using Aquality.Selenium.Core.Localization; | ||
using OpenQA.Selenium.Remote; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Aquality.Selenium.Browsers | ||
{ | ||
public class BrowserTabNavigation : IBrowserTabNavigation | ||
{ | ||
private readonly RemoteWebDriver driver; | ||
|
||
internal BrowserTabNavigation(RemoteWebDriver driver) | ||
{ | ||
this.driver = driver; | ||
} | ||
|
||
private ILocalizedLogger Logger => AqualityServices.LocalizedLogger; | ||
|
||
public string CurrentTabHandle | ||
{ | ||
get | ||
{ | ||
Logger.Info("loc.browser.get.tab.handle"); | ||
return driver.CurrentWindowHandle; | ||
} | ||
} | ||
|
||
public IList<string> TabHandles | ||
{ | ||
get | ||
{ | ||
Logger.Info("loc.browser.get.tab.handles"); | ||
return driver.WindowHandles; | ||
} | ||
} | ||
|
||
public void CloseTab() | ||
{ | ||
Logger.Info("loc.browser.tab.close"); | ||
driver.Close(); | ||
} | ||
|
||
public void OpenNewTab(bool switchToNew = true) | ||
{ | ||
Logger.Info("loc.browser.tab.open.new"); | ||
AqualityServices.Browser.ExecuteScript(JavaScript.OpenNewTab); | ||
if (switchToNew) | ||
{ | ||
SwitchToLastTab(); | ||
} | ||
} | ||
|
||
public void OpenInNewTab(string url) | ||
{ | ||
AqualityServices.Browser.ExecuteScript(JavaScript.OpenInNewTab, url); | ||
} | ||
|
||
public void SwitchToLastTab(bool closeCurrent = false) | ||
{ | ||
Logger.Info("loc.browser.switch.to.new.tab"); | ||
CloseAndSwitch(TabHandles.Last(), closeCurrent); | ||
} | ||
|
||
public void SwitchToTab(string handle, bool closeCurrent = false) | ||
{ | ||
Logger.Info("loc.browser.switch.to.tab.handle", handle); | ||
CloseAndSwitch(handle, closeCurrent); | ||
} | ||
|
||
public void SwitchToTab(int index, bool closeCurrent = false) | ||
{ | ||
Logger.Info("loc.browser.switch.to.tab.index", index); | ||
var names = TabHandles; | ||
if (index < 0 || names.Count <= index) | ||
{ | ||
throw new IndexOutOfRangeException( | ||
$"Index of browser tab '{index}' you provided is out of range {0}..{names.Count}"); | ||
} | ||
|
||
var newTab = names.ElementAt(index); | ||
CloseAndSwitch(newTab, closeCurrent); | ||
} | ||
|
||
private void CloseAndSwitch(string name, bool closeCurrent) | ||
{ | ||
if (closeCurrent) | ||
{ | ||
CloseTab(); | ||
} | ||
|
||
driver.SwitchTo().Window(name); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
Aquality.Selenium/src/Aquality.Selenium/Browsers/IBrowserTabNavigation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Aquality.Selenium.Browsers | ||
{ | ||
/// <summary> | ||
/// Provides functionality to work with browser tab navigation. | ||
/// </summary> | ||
public interface IBrowserTabNavigation | ||
{ | ||
/// <summary> | ||
/// Gets current tab handle. | ||
/// </summary> | ||
/// <returns>Current tab handle.</returns> | ||
string CurrentTabHandle { get; } | ||
|
||
/// <summary> | ||
/// Gets opened tab handles. | ||
/// </summary> | ||
/// <returns>List of tab handles.</returns> | ||
IList<string> TabHandles { get; } | ||
|
||
/// <summary> | ||
/// Switches to tab. | ||
/// </summary> | ||
/// <param name="name">Tab handle.</param> | ||
/// <param name="closeCurrent">Close current tab if true and leave it otherwise.</param> | ||
void SwitchToTab(string tabHandle, bool closeCurrent = false); | ||
|
||
/// <summary> | ||
/// Switches to tab. | ||
/// </summary> | ||
/// <param name="index">Tab index.</param> | ||
/// <param name="closeCurrent">Close current tab if true and leave it otherwise.</param> | ||
void SwitchToTab(int index, bool closeCurrent = false); | ||
|
||
/// <summary> | ||
/// Switches to the last tab. | ||
/// </summary> | ||
/// <param name="closeCurrent">Close current tab if true and leave it otherwise.</param> | ||
void SwitchToLastTab(bool closeCurrent = false); | ||
|
||
/// <summary> | ||
/// Closes curent tab. | ||
/// </summary> | ||
void CloseTab(); | ||
|
||
/// <summary> | ||
/// Opens new tab. | ||
/// </summary> | ||
/// <param name="switchToNew">Switches to new tab if true and stays at current otherwise.</param> | ||
void OpenNewTab(bool switchToNew = true); | ||
|
||
/// <summary> | ||
/// Navigates to desired url in new tab. | ||
/// </summary> | ||
/// <param name="url">String representation of URL.</param> | ||
void OpenInNewTab(string url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Aquality.Selenium/src/Aquality.Selenium/Resources/JavaScripts/OpenInNewTab.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
window.open(arguments[0]); |
1 change: 1 addition & 0 deletions
1
Aquality.Selenium/src/Aquality.Selenium/Resources/JavaScripts/OpenNewTab.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
window.open(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/BrowserTabsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
using System; | ||
using Aquality.Selenium.Browsers; | ||
using Aquality.Selenium.Tests.Integration.TestApp.TheInternet.Forms; | ||
using NUnit.Framework; | ||
using System.Linq; | ||
|
||
namespace Aquality.Selenium.Tests.Integration | ||
{ | ||
internal class BrowserTabsTests : UITest | ||
{ | ||
private readonly WelcomeForm WelcomeForm = new WelcomeForm(); | ||
|
||
[SetUp] | ||
public void Before() | ||
{ | ||
AqualityServices.Browser.GoTo(WelcomeForm.Url); | ||
} | ||
|
||
[Test] | ||
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_HandleTab() | ||
{ | ||
var browser = AqualityServices.Browser; | ||
var tabHandle = browser.Tabs().CurrentTabHandle; | ||
Assert.IsNotEmpty(tabHandle, "Tab name should not be empty"); | ||
} | ||
|
||
[Test] | ||
public void Should_BePossibleTo_GetTabHandles() | ||
{ | ||
var browser = AqualityServices.Browser; | ||
var tabHandles = browser.Tabs().TabHandles; | ||
Assert.AreEqual(1, tabHandles.Count, "Tab number should be correct"); | ||
Assert.IsNotEmpty(tabHandles.First(), "Tab handle should not be empty"); | ||
} | ||
|
||
[Test] | ||
public void Should_BePossibleTo_OpenNewTab() | ||
{ | ||
var browser = AqualityServices.Browser; | ||
var tabHandle = browser.Tabs().CurrentTabHandle; | ||
|
||
browser.Tabs().OpenNewTab(); | ||
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().OpenNewTab(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() | ||
{ | ||
var browser = AqualityServices.Browser; | ||
WelcomeForm.ClickElementalSelenium(); | ||
Assert.AreEqual(2, browser.Tabs().TabHandles.Count, "New tab should be opened"); | ||
browser.Tabs().CloseTab(); | ||
Assert.AreEqual(1, browser.Tabs().TabHandles.Count, "New tab should be closed"); | ||
} | ||
|
||
[Test] | ||
public void Should_BePossibleTo_SwitchToNewTab() | ||
{ | ||
CheckSwitchingBy(2, () => | ||
{ | ||
AqualityServices.Browser.Tabs().SwitchToLastTab(); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Should_BePossibleTo_SwitchToNewTab_AndClose() | ||
{ | ||
CheckSwitchingBy(1, () => | ||
{ | ||
AqualityServices.Browser.Tabs().SwitchToLastTab(true); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Should_BePossibleTo_SwitchToNewTabByHandle() | ||
{ | ||
CheckSwitchingBy(3, () => | ||
{ | ||
var browser = AqualityServices.Browser; | ||
var tabHandle = browser.Tabs().TabHandles.Last(); | ||
browser.Tabs().OpenNewTab(false); | ||
browser.Tabs().SwitchToTab(tabHandle); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Should_BePossibleTo_SwitchToNewTabByHandle_AndClose() | ||
{ | ||
CheckSwitchingBy(2, () => | ||
{ | ||
var browser = AqualityServices.Browser; | ||
var tabHandle = browser.Tabs().TabHandles.Last(); | ||
browser.Tabs().OpenNewTab(false); | ||
browser.Tabs().SwitchToTab(tabHandle, true); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Should_BePossibleTo_SwitchToNewTabByIndex() | ||
{ | ||
CheckSwitchingBy(3, () => | ||
{ | ||
AqualityServices.Browser.Tabs().OpenNewTab(false); | ||
AqualityServices.Browser.Tabs().SwitchToTab(1); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Should_BePossibleTo_SwitchToNewTabByIndex_AndClose() | ||
{ | ||
CheckSwitchingBy(2, () => | ||
{ | ||
AqualityServices.Browser.Tabs().OpenNewTab(false); | ||
AqualityServices.Browser.Tabs().SwitchToTab(1, true); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void Should_BeThrow_IfSwitchToNewTab_ByIncorrectIndex() | ||
{ | ||
Assert.Throws(typeof(IndexOutOfRangeException), () => { AqualityServices.Browser.Tabs().SwitchToTab(10, true); }); | ||
} | ||
|
||
private void CheckSwitchingBy(int expectedTabCount, Action switchMethod) | ||
{ | ||
var browser = AqualityServices.Browser; | ||
var tabHandle = browser.Tabs().CurrentTabHandle; | ||
WelcomeForm.ClickElementalSelenium(); | ||
var newTabHandle = browser.Tabs().TabHandles.Last(); | ||
switchMethod.Invoke(); | ||
Assert.AreEqual(newTabHandle, browser.Tabs().CurrentTabHandle, "Browser should be switched to correct tab"); | ||
Assert.AreEqual(expectedTabCount, browser.Tabs().TabHandles.Count, "Number of tabs should be correct"); | ||
} | ||
|
||
private void CheckSwitching(int expectedTabCount, Action switchMethod) | ||
{ | ||
var browser = AqualityServices.Browser; | ||
var tabHandle = browser.Tabs().CurrentTabHandle; | ||
WelcomeForm.ClickElementalSelenium(); | ||
switchMethod.Invoke(); | ||
var newTabHandle = browser.Tabs().CurrentTabHandle; | ||
Assert.AreNotEqual(tabHandle, newTabHandle, "Browser should be switched to new tab"); | ||
Assert.AreEqual(expectedTabCount, browser.Tabs().TabHandles.Count, "Number of tabs should be correct"); | ||
} | ||
} | ||
} |
Oops, something went wrong.