Skip to content

Commit

Permalink
Merge pull request #127 from aquality-automation/enhancement/use-nati…
Browse files Browse the repository at this point in the history
…ve-open-new-tab-function

[Enhancement] Use native function to open new tab
  • Loading branch information
aqualityAutomation authored Feb 15, 2024
2 parents 558e3a4 + 71b5013 commit aff8e90
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/main/java/aquality/selenium/browser/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,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
*/
Expand All @@ -132,15 +132,15 @@ public void refreshPageWithAlert(AlertActions alertAction) {
}

private Navigation navigate() {
return new BrowserNavigation(getDriver());
return new BrowserNavigation(getDriver(), localizedLogger);
}

/**
* Provides interface to manage of browser tabs.
* @return Instance of IBrowserTabNavigation.
*/
public IBrowserTabNavigation tabs() {
return new BrowserTabNavigation(getDriver());
return new BrowserTabNavigation(getDriver(), localizedLogger);
}

/**
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/aquality/selenium/browser/BrowserNavigation.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
64 changes: 43 additions & 21 deletions src/main/java/aquality/selenium/browser/BrowserTabNavigation.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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()));
Expand All @@ -48,44 +56,58 @@ 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<String> 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();
}
}

@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) {
if (closeCurrent) {
closeTab();
}

getDriver().switchTo().window(name);
}

private void infoLoc(String key, Object... args) {
AqualityServices.getLocalizedLogger().info(key, args);
driver.switchTo().window(name);
}
}
29 changes: 29 additions & 0 deletions src/main/java/aquality/selenium/browser/IBrowserTabNavigation.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package aquality.selenium.browser;

import java.net.URL;
import java.util.Set;

/**
Expand Down Expand Up @@ -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);
}
40 changes: 36 additions & 4 deletions src/test/java/tests/integration/BrowserTabsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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");
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit aff8e90

Please sign in to comment.