Skip to content

Commit aff8e90

Browse files
Merge pull request #127 from aquality-automation/enhancement/use-native-open-new-tab-function
[Enhancement] Use native function to open new tab
2 parents 558e3a4 + 71b5013 commit aff8e90

File tree

5 files changed

+125
-40
lines changed

5 files changed

+125
-40
lines changed

src/main/java/aquality/selenium/browser/Browser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void refresh() {
122122
}
123123

124124
/**
125-
* Refreshes the page and process alert that apears after refreshing
125+
* Refreshes the page and process alert that appears after refreshing
126126
*
127127
* @param alertAction accept or decline alert
128128
*/
@@ -132,15 +132,15 @@ public void refreshPageWithAlert(AlertActions alertAction) {
132132
}
133133

134134
private Navigation navigate() {
135-
return new BrowserNavigation(getDriver());
135+
return new BrowserNavigation(getDriver(), localizedLogger);
136136
}
137137

138138
/**
139139
* Provides interface to manage of browser tabs.
140140
* @return Instance of IBrowserTabNavigation.
141141
*/
142142
public IBrowserTabNavigation tabs() {
143-
return new BrowserTabNavigation(getDriver());
143+
return new BrowserTabNavigation(getDriver(), localizedLogger);
144144
}
145145

146146
/**
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
11
package aquality.selenium.browser;
22

3+
import aquality.selenium.core.localization.ILocalizedLogger;
34
import org.openqa.selenium.WebDriver.Navigation;
45
import org.openqa.selenium.remote.RemoteWebDriver;
56

67
import java.net.URL;
78

8-
class BrowserNavigation implements Navigation {
9+
/**
10+
* Browser navigation wrapper with localized logging.
11+
*/
12+
public class BrowserNavigation implements Navigation {
913

1014
private final RemoteWebDriver driver;
15+
private final ILocalizedLogger logger;
1116

12-
BrowserNavigation(RemoteWebDriver driver){
17+
protected BrowserNavigation(RemoteWebDriver driver, ILocalizedLogger logger){
1318
this.driver = driver;
19+
this.logger = logger;
1420
}
1521

1622
@Override
1723
public void back() {
1824
infoLoc("loc.browser.back");
19-
getDriver().navigate().back();
25+
driver.navigate().back();
2026
}
2127

2228
@Override
2329
public void forward() {
2430
infoLoc("loc.browser.forward");
25-
getDriver().navigate().forward();
31+
driver.navigate().forward();
2632
}
2733

2834
@Override
2935
public void to(String s) {
3036
infoLoc("loc.browser.navigate", s);
31-
getDriver().navigate().to(s);
37+
driver.navigate().to(s);
3238
}
3339

3440
@Override
3541
public void to(URL url) {
3642
infoLoc("loc.browser.navigate", url);
37-
getDriver().navigate().to(url);
43+
driver.navigate().to(url);
3844
}
3945

4046
@Override
4147
public void refresh() {
4248
infoLoc("loc.browser.refresh");
43-
getDriver().navigate().refresh();
44-
}
45-
46-
private RemoteWebDriver getDriver() {
47-
return driver;
49+
driver.navigate().refresh();
4850
}
4951

5052
private void infoLoc(String key, Object... args) {
51-
AqualityServices.getLocalizedLogger().info(key, args);
53+
logger.info(key, args);
5254
}
5355
}
Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
11
package aquality.selenium.browser;
22

3+
import aquality.selenium.core.localization.ILocalizedLogger;
4+
import org.openqa.selenium.WindowType;
35
import org.openqa.selenium.remote.RemoteWebDriver;
46

7+
import java.net.URL;
58
import java.util.ArrayList;
69
import java.util.List;
710
import java.util.Set;
811

912
import static java.lang.String.format;
1013

11-
class BrowserTabNavigation implements IBrowserTabNavigation {
14+
/**
15+
* Wrapper to work with browser tab navigation with localized logging.
16+
*/
17+
public class BrowserTabNavigation implements IBrowserTabNavigation {
1218

1319
private final RemoteWebDriver driver;
20+
private final ILocalizedLogger logger;
1421

15-
BrowserTabNavigation(RemoteWebDriver driver) {
22+
protected BrowserTabNavigation(RemoteWebDriver driver, ILocalizedLogger logger) {
1623
this.driver = driver;
24+
this.logger = logger;
1725
}
1826

1927
@Override
2028
public String getCurrentTabHandle() {
21-
infoLoc("loc.browser.get.tab.handle");
22-
return getDriver().getWindowHandle();
29+
logger.info("loc.browser.get.tab.handle");
30+
return driver.getWindowHandle();
2331
}
2432

2533
@Override
2634
public Set<String> getTabHandles() {
27-
infoLoc("loc.browser.get.tab.handles");
28-
return getDriver().getWindowHandles();
35+
logger.info("loc.browser.get.tab.handles");
36+
return driver.getWindowHandles();
2937
}
3038

3139
@Override
3240
public void switchToTab(final String tabHandle, boolean closeCurrent) {
33-
infoLoc("loc.browser.switch.to.tab.handle", tabHandle);
41+
logger.info("loc.browser.switch.to.tab.handle", tabHandle);
3442
closeAndSwitch(tabHandle, closeCurrent);
3543
}
3644

3745
@Override
3846
public void switchToTab(int index, boolean closeCurrent) {
39-
infoLoc("loc.browser.switch.to.tab.index", index);
47+
logger.info("loc.browser.switch.to.tab.index", index);
4048
List<String> handles = new ArrayList<>(getTabHandles());
4149
if (index < 0 || handles.size() <= index) {
4250
throw new IndexOutOfBoundsException(format("Index of browser tab '%1$s' you provided is out of range 0..%2$s", index, handles.size()));
@@ -48,44 +56,58 @@ public void switchToTab(int index, boolean closeCurrent) {
4856

4957
@Override
5058
public void switchToLastTab(boolean closeCurrent) {
51-
infoLoc("loc.browser.switch.to.new.tab");
59+
logger.info("loc.browser.switch.to.new.tab");
5260
List<String> handles = new ArrayList<>(getTabHandles());
5361
closeAndSwitch(handles.get(handles.size() - 1), closeCurrent);
5462
}
5563

5664
@Override
5765
public void closeTab() {
58-
infoLoc("loc.browser.tab.close");
59-
getDriver().close();
66+
logger.info("loc.browser.tab.close");
67+
driver.close();
6068
}
6169

6270
@Override
6371
public void openNewTab(boolean switchToNew) {
64-
infoLoc("loc.browser.tab.open.new");
65-
AqualityServices.getBrowser().executeScript(JavaScript.OPEN_NEW_TAB);
72+
logger.info("loc.browser.tab.open.new");
73+
String currentHandle = switchToNew ? null : getCurrentTabHandle();
74+
driver.switchTo().newWindow(WindowType.TAB);
75+
if (!switchToNew) {
76+
closeAndSwitch(currentHandle, false);
77+
}
78+
}
79+
80+
@Override
81+
public void openNewTabViaJs(boolean switchToNew) {
82+
logger.info("loc.browser.tab.open.new");
83+
driver.executeScript(JavaScript.OPEN_NEW_TAB.getScript());
6684
if (switchToNew) {
6785
switchToLastTab();
6886
}
6987
}
7088

7189
@Override
7290
public void openInNewTab(final String url) {
73-
AqualityServices.getBrowser().executeScript(JavaScript.OPEN_IN_NEW_TAB, url);
91+
openNewTab();
92+
driver.navigate().to(url);
93+
}
94+
95+
@Override
96+
public void openInNewTab(final URL url) {
97+
driver.switchTo().newWindow(WindowType.TAB);
98+
driver.navigate().to(url);
7499
}
75100

76-
private RemoteWebDriver getDriver() {
77-
return driver;
101+
@Override
102+
public void openInNewTabViaJs(final String url) {
103+
driver.executeScript(JavaScript.OPEN_IN_NEW_TAB.getScript(), url);
78104
}
79105

80106
private void closeAndSwitch(final String name, boolean closeCurrent) {
81107
if (closeCurrent) {
82108
closeTab();
83109
}
84110

85-
getDriver().switchTo().window(name);
86-
}
87-
88-
private void infoLoc(String key, Object... args) {
89-
AqualityServices.getLocalizedLogger().info(key, args);
111+
driver.switchTo().window(name);
90112
}
91113
}

src/main/java/aquality/selenium/browser/IBrowserTabNavigation.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package aquality.selenium.browser;
22

3+
import java.net.URL;
34
import java.util.Set;
45

56
/**
@@ -87,10 +88,38 @@ default void openNewTab() {
8788
*/
8889
void openNewTab(boolean switchToNew);
8990

91+
/**
92+
* Opens and switches to new tab using JS function.
93+
*/
94+
default void openNewTabViaJs() {
95+
openNewTabViaJs(true);
96+
}
97+
98+
/**
99+
* Opens new tab using JS function.
100+
*
101+
* @param switchToNew Switches to new tab if true and stays at current otherwise.
102+
*/
103+
void openNewTabViaJs(boolean switchToNew);
104+
90105
/**
91106
* Navigates to desired url in new tab.
92107
*
93108
* @param url String representation of URL.
94109
*/
95110
void openInNewTab(String url);
111+
112+
/**
113+
* Navigates to desired url in new tab.
114+
*
115+
* @param url target URL.
116+
*/
117+
void openInNewTab(URL url);
118+
119+
/**
120+
* Navigates to desired url in new tab.
121+
*
122+
* @param url String representation of URL.
123+
*/
124+
void openInNewTabViaJs(String url);
96125
}

src/test/java/tests/integration/BrowserTabsTests.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import tests.BaseTest;
99
import theinternet.forms.WelcomeForm;
1010

11+
import java.net.MalformedURLException;
12+
import java.net.URL;
1113
import java.util.ArrayList;
1214
import java.util.List;
1315
import java.util.Set;
1416

15-
import static org.testng.Assert.assertEquals;
16-
import static org.testng.Assert.assertNotEquals;
17-
import static org.testng.Assert.assertFalse;
17+
import static org.testng.Assert.*;
1818

1919
public class BrowserTabsTests extends BaseTest {
2020

@@ -31,7 +31,24 @@ public void testShouldBePossibleToOpenUrlInNewTab() {
3131
String url = welcomeForm.getUrl();
3232
Browser browser = AqualityServices.getBrowser();
3333
browser.tabs().openInNewTab(url);
34-
browser.tabs().switchToLastTab();
34+
assertEquals(2, browser.tabs().getTabHandles().size());
35+
assertEquals(browser.getCurrentUrl(), url + "/", "Url should be opened in new tab");
36+
}
37+
38+
@Test
39+
public void testShouldBePossibleToOpenUriInNewTab() throws MalformedURLException {
40+
URL url = new URL(welcomeForm.getUrl());
41+
Browser browser = AqualityServices.getBrowser();
42+
browser.tabs().openInNewTab(url);
43+
assertEquals(2, browser.tabs().getTabHandles().size());
44+
assertEquals(browser.getCurrentUrl(), url + "/", "Url should be opened in new tab");
45+
}
46+
47+
@Test
48+
public void testShouldBePossibleToOpenUrlInNewTabViaJs() {
49+
String url = welcomeForm.getUrl();
50+
Browser browser = AqualityServices.getBrowser();
51+
browser.tabs().openInNewTabViaJs(url);
3552
assertEquals(2, browser.tabs().getTabHandles().size());
3653
assertEquals(browser.getCurrentUrl(), url + "/", "Url should be opened in new tab");
3754
}
@@ -66,6 +83,21 @@ public void testShouldBePossibleToOpenNewTab() {
6683
assertEquals(newTabHandle, browser.tabs().getCurrentTabHandle(), "Browser should not be switched to new tab");
6784
}
6885

86+
@Test
87+
public void testShouldBePossibleToOpenNewTabViaJs() {
88+
Browser browser = AqualityServices.getBrowser();
89+
String tabHandle = browser.tabs().getCurrentTabHandle();
90+
91+
browser.tabs().openNewTabViaJs();
92+
String newTabHandle = browser.tabs().getCurrentTabHandle();
93+
assertEquals(2, browser.tabs().getTabHandles().size(), "New tab should be opened");
94+
assertNotEquals(tabHandle, newTabHandle, "Browser should be switched to new tab");
95+
96+
browser.tabs().openNewTabViaJs(false);
97+
assertEquals(3, browser.tabs().getTabHandles().size(), "New tab should be opened");
98+
assertEquals(newTabHandle, browser.tabs().getCurrentTabHandle(), "Browser should not be switched to new tab");
99+
}
100+
69101
@Test
70102
public void testShouldBePossibleToCloseTab() {
71103
Browser browser = AqualityServices.getBrowser();

0 commit comments

Comments
 (0)