From 26b504ae9b8a2058dc8f164179c5efd4438e4bc6 Mon Sep 17 00:00:00 2001 From: Aleksey2 Meleshko Date: Fri, 12 May 2023 18:15:38 +0200 Subject: [PATCH] [Enhancement] 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 --- .../aquality/selenium/browser/Browser.java | 6 +- .../selenium/browser/BrowserNavigation.java | 26 ++++---- .../browser/BrowserTabNavigation.java | 64 +++++++++++++------ .../browser/IBrowserTabNavigation.java | 29 +++++++++ .../tests/integration/BrowserTabsTests.java | 40 ++++++++++-- 5 files changed, 125 insertions(+), 40 deletions(-) diff --git a/src/main/java/aquality/selenium/browser/Browser.java b/src/main/java/aquality/selenium/browser/Browser.java index 8b6a998..a297cae 100644 --- a/src/main/java/aquality/selenium/browser/Browser.java +++ b/src/main/java/aquality/selenium/browser/Browser.java @@ -125,7 +125,7 @@ public void refresh() { } /** - * Refreshes the page and process alert that apears after refreshing + * Refreshes the page and process alert that appears after refreshing * * @param alertAction accept or decline alert */ @@ -135,7 +135,7 @@ public void refreshPageWithAlert(AlertActions alertAction) { } private Navigation navigate() { - return new BrowserNavigation(getDriver()); + return new BrowserNavigation(getDriver(), localizedLogger); } /** @@ -143,7 +143,7 @@ private Navigation navigate() { * @return Instance of IBrowserTabNavigation. */ public IBrowserTabNavigation tabs() { - return new BrowserTabNavigation(getDriver()); + return new BrowserTabNavigation(getDriver(), localizedLogger); } /** diff --git a/src/main/java/aquality/selenium/browser/BrowserNavigation.java b/src/main/java/aquality/selenium/browser/BrowserNavigation.java index c81e50a..5cf031f 100644 --- a/src/main/java/aquality/selenium/browser/BrowserNavigation.java +++ b/src/main/java/aquality/selenium/browser/BrowserNavigation.java @@ -1,53 +1,55 @@ package aquality.selenium.browser; +import aquality.selenium.core.localization.ILocalizedLogger; import org.openqa.selenium.WebDriver.Navigation; import org.openqa.selenium.remote.RemoteWebDriver; import java.net.URL; -class BrowserNavigation implements Navigation { +/** + * Browser navigation wrapper with localized logging. + */ +public class BrowserNavigation implements Navigation { private final RemoteWebDriver driver; + private final ILocalizedLogger logger; - BrowserNavigation(RemoteWebDriver driver){ + protected BrowserNavigation(RemoteWebDriver driver, ILocalizedLogger logger){ this.driver = driver; + this.logger = logger; } @Override public void back() { infoLoc("loc.browser.back"); - getDriver().navigate().back(); + driver.navigate().back(); } @Override public void forward() { infoLoc("loc.browser.forward"); - getDriver().navigate().forward(); + driver.navigate().forward(); } @Override public void to(String s) { infoLoc("loc.browser.navigate", s); - getDriver().navigate().to(s); + driver.navigate().to(s); } @Override public void to(URL url) { infoLoc("loc.browser.navigate", url); - getDriver().navigate().to(url); + driver.navigate().to(url); } @Override public void refresh() { infoLoc("loc.browser.refresh"); - getDriver().navigate().refresh(); - } - - private RemoteWebDriver getDriver() { - return driver; + driver.navigate().refresh(); } private void infoLoc(String key, Object... args) { - AqualityServices.getLocalizedLogger().info(key, args); + logger.info(key, args); } } diff --git a/src/main/java/aquality/selenium/browser/BrowserTabNavigation.java b/src/main/java/aquality/selenium/browser/BrowserTabNavigation.java index 8dddbf5..e94eade 100644 --- a/src/main/java/aquality/selenium/browser/BrowserTabNavigation.java +++ b/src/main/java/aquality/selenium/browser/BrowserTabNavigation.java @@ -1,42 +1,50 @@ package aquality.selenium.browser; +import aquality.selenium.core.localization.ILocalizedLogger; +import org.openqa.selenium.WindowType; import org.openqa.selenium.remote.RemoteWebDriver; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Set; import static java.lang.String.format; -class BrowserTabNavigation implements IBrowserTabNavigation { +/** + * Wrapper to work with browser tab navigation with localized logging. + */ +public class BrowserTabNavigation implements IBrowserTabNavigation { private final RemoteWebDriver driver; + private final ILocalizedLogger logger; - BrowserTabNavigation(RemoteWebDriver driver) { + protected BrowserTabNavigation(RemoteWebDriver driver, ILocalizedLogger logger) { this.driver = driver; + this.logger = logger; } @Override public String getCurrentTabHandle() { - infoLoc("loc.browser.get.tab.handle"); - return getDriver().getWindowHandle(); + logger.info("loc.browser.get.tab.handle"); + return driver.getWindowHandle(); } @Override public Set getTabHandles() { - infoLoc("loc.browser.get.tab.handles"); - return getDriver().getWindowHandles(); + logger.info("loc.browser.get.tab.handles"); + return driver.getWindowHandles(); } @Override public void switchToTab(final String tabHandle, boolean closeCurrent) { - infoLoc("loc.browser.switch.to.tab.handle", tabHandle); + logger.info("loc.browser.switch.to.tab.handle", tabHandle); closeAndSwitch(tabHandle, closeCurrent); } @Override public void switchToTab(int index, boolean closeCurrent) { - infoLoc("loc.browser.switch.to.tab.index", index); + logger.info("loc.browser.switch.to.tab.index", index); List handles = new ArrayList<>(getTabHandles()); if (index < 0 || handles.size() <= index) { throw new IndexOutOfBoundsException(format("Index of browser tab '%1$s' you provided is out of range 0..%2$s", index, handles.size())); @@ -48,21 +56,31 @@ public void switchToTab(int index, boolean closeCurrent) { @Override public void switchToLastTab(boolean closeCurrent) { - infoLoc("loc.browser.switch.to.new.tab"); + logger.info("loc.browser.switch.to.new.tab"); List handles = new ArrayList<>(getTabHandles()); closeAndSwitch(handles.get(handles.size() - 1), closeCurrent); } @Override public void closeTab() { - infoLoc("loc.browser.tab.close"); - getDriver().close(); + logger.info("loc.browser.tab.close"); + driver.close(); } @Override public void openNewTab(boolean switchToNew) { - infoLoc("loc.browser.tab.open.new"); - AqualityServices.getBrowser().executeScript(JavaScript.OPEN_NEW_TAB); + logger.info("loc.browser.tab.open.new"); + String currentHandle = switchToNew ? null : getCurrentTabHandle(); + driver.switchTo().newWindow(WindowType.TAB); + if (!switchToNew) { + closeAndSwitch(currentHandle, false); + } + } + + @Override + public void openNewTabViaJs(boolean switchToNew) { + logger.info("loc.browser.tab.open.new"); + driver.executeScript(JavaScript.OPEN_NEW_TAB.getScript()); if (switchToNew) { switchToLastTab(); } @@ -70,11 +88,19 @@ public void openNewTab(boolean switchToNew) { @Override public void openInNewTab(final String url) { - AqualityServices.getBrowser().executeScript(JavaScript.OPEN_IN_NEW_TAB, url); + openNewTab(); + driver.navigate().to(url); + } + + @Override + public void openInNewTab(final URL url) { + driver.switchTo().newWindow(WindowType.TAB); + driver.navigate().to(url); } - private RemoteWebDriver getDriver() { - return driver; + @Override + public void openInNewTabViaJs(final String url) { + driver.executeScript(JavaScript.OPEN_IN_NEW_TAB.getScript(), url); } private void closeAndSwitch(final String name, boolean closeCurrent) { @@ -82,10 +108,6 @@ private void closeAndSwitch(final String name, boolean closeCurrent) { closeTab(); } - getDriver().switchTo().window(name); - } - - private void infoLoc(String key, Object... args) { - AqualityServices.getLocalizedLogger().info(key, args); + driver.switchTo().window(name); } } diff --git a/src/main/java/aquality/selenium/browser/IBrowserTabNavigation.java b/src/main/java/aquality/selenium/browser/IBrowserTabNavigation.java index 01d245a..ea49fbd 100644 --- a/src/main/java/aquality/selenium/browser/IBrowserTabNavigation.java +++ b/src/main/java/aquality/selenium/browser/IBrowserTabNavigation.java @@ -1,5 +1,6 @@ package aquality.selenium.browser; +import java.net.URL; import java.util.Set; /** @@ -87,10 +88,38 @@ default void openNewTab() { */ void openNewTab(boolean switchToNew); + /** + * Opens and switches to new tab using JS function. + */ + default void openNewTabViaJs() { + openNewTabViaJs(true); + } + + /** + * Opens new tab using JS function. + * + * @param switchToNew Switches to new tab if true and stays at current otherwise. + */ + void openNewTabViaJs(boolean switchToNew); + /** * Navigates to desired url in new tab. * * @param url String representation of URL. */ void openInNewTab(String url); + + /** + * Navigates to desired url in new tab. + * + * @param url target URL. + */ + void openInNewTab(URL url); + + /** + * Navigates to desired url in new tab. + * + * @param url String representation of URL. + */ + void openInNewTabViaJs(String url); } diff --git a/src/test/java/tests/integration/BrowserTabsTests.java b/src/test/java/tests/integration/BrowserTabsTests.java index ee9f996..f39df22 100644 --- a/src/test/java/tests/integration/BrowserTabsTests.java +++ b/src/test/java/tests/integration/BrowserTabsTests.java @@ -8,13 +8,13 @@ import tests.BaseTest; import theinternet.forms.WelcomeForm; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Set; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotEquals; -import static org.testng.Assert.assertFalse; +import static org.testng.Assert.*; public class BrowserTabsTests extends BaseTest { @@ -31,7 +31,24 @@ public void testShouldBePossibleToOpenUrlInNewTab() { String url = welcomeForm.getUrl(); Browser browser = AqualityServices.getBrowser(); browser.tabs().openInNewTab(url); - browser.tabs().switchToLastTab(); + assertEquals(2, browser.tabs().getTabHandles().size()); + assertEquals(browser.getCurrentUrl(), url + "/", "Url should be opened in new tab"); + } + + @Test + public void testShouldBePossibleToOpenUriInNewTab() throws MalformedURLException { + URL url = new URL(welcomeForm.getUrl()); + Browser browser = AqualityServices.getBrowser(); + browser.tabs().openInNewTab(url); + assertEquals(2, browser.tabs().getTabHandles().size()); + assertEquals(browser.getCurrentUrl(), url + "/", "Url should be opened in new tab"); + } + + @Test + public void testShouldBePossibleToOpenUrlInNewTabViaJs() { + String url = welcomeForm.getUrl(); + Browser browser = AqualityServices.getBrowser(); + browser.tabs().openInNewTabViaJs(url); assertEquals(2, browser.tabs().getTabHandles().size()); assertEquals(browser.getCurrentUrl(), url + "/", "Url should be opened in new tab"); } @@ -66,6 +83,21 @@ public void testShouldBePossibleToOpenNewTab() { assertEquals(newTabHandle, browser.tabs().getCurrentTabHandle(), "Browser should not be switched to new tab"); } + @Test + public void testShouldBePossibleToOpenNewTabViaJs() { + Browser browser = AqualityServices.getBrowser(); + String tabHandle = browser.tabs().getCurrentTabHandle(); + + browser.tabs().openNewTabViaJs(); + String newTabHandle = browser.tabs().getCurrentTabHandle(); + assertEquals(2, browser.tabs().getTabHandles().size(), "New tab should be opened"); + assertNotEquals(tabHandle, newTabHandle, "Browser should be switched to new tab"); + + browser.tabs().openNewTabViaJs(false); + assertEquals(3, browser.tabs().getTabHandles().size(), "New tab should be opened"); + assertEquals(newTabHandle, browser.tabs().getCurrentTabHandle(), "Browser should not be switched to new tab"); + } + @Test public void testShouldBePossibleToCloseTab() { Browser browser = AqualityServices.getBrowser();