Skip to content

Commit caf08db

Browse files
authored
#82 added tab management to browser (#85)
* #82 added tab management to browser * #82 updated pom version * #82 review comments fix
1 parent ba3c8a0 commit caf08db

File tree

15 files changed

+393
-8
lines changed

15 files changed

+393
-8
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.aquality-automation</groupId>
88
<artifactId>aquality-selenium</artifactId>
9-
<version>2.0.2</version>
9+
<version>2.1.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Aquality Selenium</name>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ private Navigation navigate() {
131131
return new BrowserNavigation(getDriver());
132132
}
133133

134+
/**
135+
* Provides interface to manage of browser tabs.
136+
* @return Instance of IBrowserTabNavigation.
137+
*/
138+
public IBrowserTabNavigation tabs() {
139+
return new BrowserTabNavigation(getDriver());
140+
}
141+
134142
/**
135143
* Sets page load timeout (Will be ignored for Safari https://github.com/SeleniumHQ/selenium-google-code-issue-archive/issues/687)
136144
*
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package aquality.selenium.browser;
2+
3+
import org.openqa.selenium.remote.RemoteWebDriver;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Set;
8+
9+
import static java.lang.String.format;
10+
11+
class BrowserTabNavigation implements IBrowserTabNavigation {
12+
13+
private final RemoteWebDriver driver;
14+
15+
BrowserTabNavigation(RemoteWebDriver driver) {
16+
this.driver = driver;
17+
}
18+
19+
@Override
20+
public String getCurrentTabHandle() {
21+
infoLoc("loc.browser.get.tab.handle");
22+
return getDriver().getWindowHandle();
23+
}
24+
25+
@Override
26+
public Set<String> getTabHandles() {
27+
infoLoc("loc.browser.get.tab.handles");
28+
return getDriver().getWindowHandles();
29+
}
30+
31+
@Override
32+
public void switchToTab(final String tabHandle, boolean closeCurrent) {
33+
infoLoc("loc.browser.switch.to.tab.handle", tabHandle);
34+
closeAndSwitch(tabHandle, closeCurrent);
35+
}
36+
37+
@Override
38+
public void switchToTab(int index, boolean closeCurrent) {
39+
infoLoc("loc.browser.switch.to.tab.index", index);
40+
List<String> handles = new ArrayList<>(getTabHandles());
41+
if (index < 0 || handles.size() <= index) {
42+
throw new IndexOutOfBoundsException(format("Index of browser tab '%1$s' you provided is out of range 0..%2$s", index, handles.size()));
43+
}
44+
45+
String newTab = handles.get(index);
46+
closeAndSwitch(newTab, closeCurrent);
47+
}
48+
49+
@Override
50+
public void switchToLastTab(boolean closeCurrent) {
51+
infoLoc("loc.browser.switch.to.new.tab");
52+
List<String> handles = new ArrayList<>(getTabHandles());
53+
closeAndSwitch(handles.get(handles.size() - 1), closeCurrent);
54+
}
55+
56+
@Override
57+
public void closeTab() {
58+
infoLoc("loc.browser.tab.close");
59+
getDriver().close();
60+
}
61+
62+
@Override
63+
public void openNewTab(boolean switchToNew) {
64+
infoLoc("loc.browser.tab.open.new");
65+
AqualityServices.getBrowser().executeScript(JavaScript.OPEN_NEW_TAB);
66+
if (switchToNew) {
67+
switchToLastTab();
68+
}
69+
}
70+
71+
@Override
72+
public void openInNewTab(final String url) {
73+
AqualityServices.getBrowser().executeScript(JavaScript.OPEN_IN_NEW_TAB, url);
74+
}
75+
76+
private RemoteWebDriver getDriver() {
77+
return driver;
78+
}
79+
80+
private void closeAndSwitch(final String name, boolean closeCurrent) {
81+
if (closeCurrent) {
82+
closeTab();
83+
}
84+
85+
getDriver().switchTo().window(name);
86+
}
87+
88+
private void infoLoc(String key, Object... args) {
89+
AqualityServices.getLocalizedLogger().info(key, args);
90+
}
91+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package aquality.selenium.browser;
2+
3+
import java.util.Set;
4+
5+
/**
6+
* Provides functionality to work with browser tab navigation.
7+
*/
8+
public interface IBrowserTabNavigation {
9+
/**
10+
* Gets current tab handle.
11+
*
12+
* @return Current tab handle.
13+
*/
14+
String getCurrentTabHandle();
15+
16+
/**
17+
* Gets opened tab handles.
18+
*
19+
* @return Set of tab handles.
20+
*/
21+
Set<String> getTabHandles();
22+
23+
/**
24+
* Switches to tab and doesn't close current tab.
25+
*
26+
* @param tabHandle Tab handle.
27+
*/
28+
default void switchToTab(String tabHandle) {
29+
switchToTab(tabHandle, false);
30+
}
31+
32+
/**
33+
* Switches to tab.
34+
*
35+
* @param tabHandle Tab handle.
36+
* @param closeCurrent Close current tab if true and leave it otherwise.
37+
*/
38+
void switchToTab(String tabHandle, boolean closeCurrent);
39+
40+
/**
41+
* Switches to tab and doesn't close current tab.
42+
*
43+
* @param index Tab index.
44+
*/
45+
default void switchToTab(int index) {
46+
switchToTab(index, false);
47+
}
48+
49+
/**
50+
* Switches to tab.
51+
*
52+
* @param index Tab index.
53+
* @param closeCurrent Close current tab if true and leave it otherwise.
54+
*/
55+
void switchToTab(int index, boolean closeCurrent);
56+
57+
/**
58+
* Switches to the last tab and doesn't close current tab.
59+
*/
60+
default void switchToLastTab() {
61+
switchToLastTab(false);
62+
}
63+
64+
/**
65+
* Switches to the last tab.
66+
*
67+
* @param closeCurrent Close current tab if true and leave it otherwise.
68+
*/
69+
void switchToLastTab(boolean closeCurrent);
70+
71+
/**
72+
* Closes current tab.
73+
*/
74+
void closeTab();
75+
76+
/**
77+
* Opens and switches to new tab.
78+
*/
79+
default void openNewTab() {
80+
openNewTab(true);
81+
}
82+
83+
/**
84+
* Opens new tab.
85+
*
86+
* @param switchToNew Switches to new tab if true and stays at current otherwise.
87+
*/
88+
void openNewTab(boolean switchToNew);
89+
90+
/**
91+
* Navigates to desired url in new tab.
92+
*
93+
* @param url String representation of URL.
94+
*/
95+
void openInNewTab(String url);
96+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ public enum JavaScript {
3838
SCROLL_WINDOW_BY("scrollWindowBy.js"),
3939
SET_INNER_HTML("setInnerHTML.js"),
4040
GET_VIEWPORT_COORDINATES("getViewPortCoordinates.js"),
41-
GET_SCREEN_OFFSET("getScreenOffset.js");
41+
GET_SCREEN_OFFSET("getScreenOffset.js"),
42+
OPEN_IN_NEW_TAB("openInNewTab.js"),
43+
OPEN_NEW_TAB("openNewTab.js");
4244

4345
private final String filename;
4446

src/main/resources/js/openInNewTab.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
window.open(arguments[0]);

src/main/resources/js/openNewTab.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
window.open();

src/main/resources/localization/be.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,12 @@
6868
"loc.elements.found.but.should.not": "Не павінна быць знойдзена элементаў па лакатару '%1$s' у %2$s стане",
6969
"loc.search.of.elements.failed": "Пошук элемента па лакатару '%1$s' прайшоў няўдала",
7070
"loc.element.not.in.state": "Элемент %1$s не стаў %2$s пасля таймаўта %3$s",
71-
"loc.get.page.source.failed": "Адбылася памылка ў час атрымання разметкі старонкі"
71+
"loc.get.page.source.failed": "Адбылася памылка ў час атрымання разметкі старонкі",
72+
"loc.browser.switch.to.tab.handle": "Пераключэнне на новую ўкладку па дэскрыптару '%1$s'",
73+
"loc.browser.switch.to.tab.index": "Пераключэнне на новую ўкладку па індэксе '%1$s'",
74+
"loc.browser.switch.to.new.tab": "Пераключэнне на новую ўкладку",
75+
"loc.browser.get.tab.handles": "Атрыманне спісу дэскрыптараў адкрытых укладак",
76+
"loc.browser.get.tab.handle": "Атрыманне дэскрыптара бягучай укладкі",
77+
"loc.browser.tab.open.new": "Адкрыццё новай укладкі",
78+
"loc.browser.tab.close": "Закрыццё ўкладкі"
7279
}

src/main/resources/localization/en.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,12 @@
6868
"loc.elements.found.but.should.not": "No elements should be found by locator '%1$s' in %2$s state",
6969
"loc.search.of.elements.failed": "Search of element by locator '%1$s' failed",
7070
"loc.element.not.in.state": "Element %1$s has not become %2$s after timeout %3$s",
71-
"loc.get.page.source.failed": "An exception occurred while tried to save the page source"
71+
"loc.get.page.source.failed": "An exception occurred while tried to save the page source",
72+
"loc.browser.switch.to.tab.handle": "Switching to tab by handle '%1$s'",
73+
"loc.browser.switch.to.tab.index": "Switching to tab by index '%1$s'",
74+
"loc.browser.switch.to.new.tab": "Switching to new tab",
75+
"loc.browser.get.tab.handles": "Getting tab handles",
76+
"loc.browser.get.tab.handle": "Getting current tab handle",
77+
"loc.browser.tab.open.new": "Opening new tab",
78+
"loc.browser.tab.close": "Closing tab"
7279
}

src/main/resources/localization/ru.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,12 @@
6868
"loc.elements.found.but.should.not": "Не должно быть найдено элементов по локатору '%1$s' в %2$s состоянии",
6969
"loc.search.of.elements.failed": "Поиск элемента по локатору '%1$s' прошел неудачно",
7070
"loc.element.not.in.state": "Элемент %1$s не стал %2$s после таймаута %3$s",
71-
"loc.get.page.source.failed": "Произошла ошибка во время получения разметки страницы"
71+
"loc.get.page.source.failed": "Произошла ошибка во время получения разметки страницы",
72+
"loc.browser.switch.to.tab.handle": "Переключение на новую вкладку по дескриптору '%1$s'",
73+
"loc.browser.switch.to.tab.index": "Переключение на новую вкладку по индексу '%1$s'",
74+
"loc.browser.switch.to.new.tab": "Переключение на новую вкладку",
75+
"loc.browser.get.tab.handles": "Получение списка дескрипторов открытых вкладок",
76+
"loc.browser.get.tab.handle": "Получение дескриптора текущей вкладки",
77+
"loc.browser.tab.open.new": "Открытие новой вкладки",
78+
"loc.browser.tab.close": "Закрытие вкладки"
7279
}

0 commit comments

Comments
 (0)