|
| 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 | +} |
0 commit comments