diff --git a/pom.xml b/pom.xml index 2581504..011b18d 100644 --- a/pom.xml +++ b/pom.xml @@ -82,7 +82,7 @@ com.github.aquality-automation aquality-selenium-core - 4.0.1 + 4.0.2 org.apache.commons @@ -92,24 +92,24 @@ com.fasterxml.jackson.core jackson-databind - 2.16.1 + 2.17.0 org.slf4j slf4j-simple - 2.0.12 + 2.0.13 test org.openpnp opencv - [4.7.0,) + [4.9.0,) org.testng testng - 7.9.0 + 7.10.2 test diff --git a/src/main/java/aquality/selenium/browser/Browser.java b/src/main/java/aquality/selenium/browser/Browser.java index 082216b..4a70cd5 100644 --- a/src/main/java/aquality/selenium/browser/Browser.java +++ b/src/main/java/aquality/selenium/browser/Browser.java @@ -136,11 +136,19 @@ private Navigation navigate() { } /** - * Provides interface to manage of browser tabs. + * Provides interface to manage browser tabs. * @return Instance of IBrowserTabNavigation. */ public IBrowserTabNavigation tabs() { - return new BrowserTabNavigation(getDriver(), localizedLogger); + return new BrowserWindowNavigation(getDriver(), localizedLogger, WindowType.TAB); + } + + /** + * Provides interface to manage browser windows. + * @return Instance of IBrowserWindowNavigation. + */ + public IBrowserWindowNavigation windows() { + return new BrowserWindowNavigation(getDriver(), localizedLogger, WindowType.WINDOW); } /** diff --git a/src/main/java/aquality/selenium/browser/BrowserTabNavigation.java b/src/main/java/aquality/selenium/browser/BrowserTabNavigation.java deleted file mode 100644 index e94eade..0000000 --- a/src/main/java/aquality/selenium/browser/BrowserTabNavigation.java +++ /dev/null @@ -1,113 +0,0 @@ -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; - -/** - * Wrapper to work with browser tab navigation with localized logging. - */ -public class BrowserTabNavigation implements IBrowserTabNavigation { - - private final RemoteWebDriver driver; - private final ILocalizedLogger logger; - - protected BrowserTabNavigation(RemoteWebDriver driver, ILocalizedLogger logger) { - this.driver = driver; - this.logger = logger; - } - - @Override - public String getCurrentTabHandle() { - logger.info("loc.browser.get.tab.handle"); - return driver.getWindowHandle(); - } - - @Override - public Set getTabHandles() { - logger.info("loc.browser.get.tab.handles"); - return driver.getWindowHandles(); - } - - @Override - public void switchToTab(final String tabHandle, boolean closeCurrent) { - logger.info("loc.browser.switch.to.tab.handle", tabHandle); - closeAndSwitch(tabHandle, closeCurrent); - } - - @Override - public void switchToTab(int index, boolean closeCurrent) { - 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())); - } - - String newTab = handles.get(index); - closeAndSwitch(newTab, closeCurrent); - } - - @Override - public void switchToLastTab(boolean closeCurrent) { - logger.info("loc.browser.switch.to.new.tab"); - List handles = new ArrayList<>(getTabHandles()); - closeAndSwitch(handles.get(handles.size() - 1), closeCurrent); - } - - @Override - public void closeTab() { - logger.info("loc.browser.tab.close"); - driver.close(); - } - - @Override - public void openNewTab(boolean switchToNew) { - 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) { - openNewTab(); - driver.navigate().to(url); - } - - @Override - public void openInNewTab(final URL url) { - driver.switchTo().newWindow(WindowType.TAB); - driver.navigate().to(url); - } - - @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(); - } - - driver.switchTo().window(name); - } -} diff --git a/src/main/java/aquality/selenium/browser/BrowserWindowNavigation.java b/src/main/java/aquality/selenium/browser/BrowserWindowNavigation.java new file mode 100644 index 0000000..852282e --- /dev/null +++ b/src/main/java/aquality/selenium/browser/BrowserWindowNavigation.java @@ -0,0 +1,130 @@ +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; + +/** + * Wrapper to work with browser tab/window navigation with localized logging. + */ +public class BrowserWindowNavigation implements IBrowserTabNavigation { + + private final RemoteWebDriver driver; + private final ILocalizedLogger logger; + private final WindowType windowType; + private final String type; + + protected BrowserWindowNavigation(RemoteWebDriver driver, ILocalizedLogger logger, WindowType windowType) { + this.driver = driver; + this.logger = logger; + this.windowType = windowType; + type = windowType.name().toLowerCase(); + } + + private void logInfo(String key, Object... params) { + logger.info(String.format(key, type), params); + } + + @Override + public String getCurrentHandle() { + logInfo("loc.browser.get.%s.handle"); + return driver.getWindowHandle(); + } + + @Override + public Set getHandles() { + logInfo("loc.browser.get.%s.handles"); + return driver.getWindowHandles(); + } + + @Override + public void switchTo(final String handle, boolean closeCurrent) { + logInfo("loc.browser.switch.to.%s.handle", handle); + closeAndSwitch(handle, closeCurrent); + } + + @Override + public void switchTo(int index, boolean closeCurrent) { + logInfo("loc.browser.switch.to.%s.index", index); + List handles = new ArrayList<>(getHandles()); + if (index < 0 || handles.size() <= index) { + throw new IndexOutOfBoundsException(format("Index of browser %1$s '%2$s' you provided is out of range 0..%3$s", type, index, handles.size())); + } + + String newTab = handles.get(index); + closeAndSwitch(newTab, closeCurrent); + } + + @Override + public void switchToLast(boolean closeCurrent) { + logInfo("loc.browser.switch.to.new.%s"); + List handles = new ArrayList<>(getHandles()); + closeAndSwitch(handles.get(handles.size() - 1), closeCurrent); + } + + @Override + public void close() { + logInfo("loc.browser.%s.close"); + driver.close(); + } + + @Override + public void openNew(boolean switchToNew) { + openNew(switchToNew, true); + } + private void openNew(boolean switchToNew, boolean log) { + if (log) { + logInfo("loc.browser.%s.open.new"); + } + String currentHandle = switchToNew ? null : getCurrentHandle(); + driver.switchTo().newWindow(windowType); + if (!switchToNew) { + closeAndSwitch(currentHandle, false); + } + } + + @Override + public void openNewViaJs(boolean switchToNew) { + logInfo("loc.browser.%s.open.new"); + JavaScript script = WindowType.TAB == windowType ? JavaScript.OPEN_NEW_TAB : JavaScript.OPEN_NEW_WINDOW; + driver.executeScript(script.getScript()); + if (switchToNew) { + switchToLast(); + } + } + + @Override + public void openInNew(final String url) { + logInfo("loc.browser.navigate.in.new.%s", url); + openNew(true, false); + driver.navigate().to(url); + } + + @Override + public void openInNew(final URL url) { + logInfo("loc.browser.navigate.in.new.%s", url); + driver.switchTo().newWindow(windowType); + driver.navigate().to(url); + } + + @Override + public void openInNewViaJs(final String url) { + JavaScript script = WindowType.TAB == windowType ? JavaScript.OPEN_IN_NEW_TAB : JavaScript.OPEN_IN_NEW_WINDOW; + driver.executeScript(script.getScript(), url); + } + + private void closeAndSwitch(final String name, boolean closeCurrent) { + if (closeCurrent) { + close(); + } + + 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 ea49fbd..9324edc 100644 --- a/src/main/java/aquality/selenium/browser/IBrowserTabNavigation.java +++ b/src/main/java/aquality/selenium/browser/IBrowserTabNavigation.java @@ -6,120 +6,174 @@ /** * Provides functionality to work with browser tab navigation. */ -public interface IBrowserTabNavigation { +public interface IBrowserTabNavigation extends IBrowserWindowNavigation { /** * Gets current tab handle. + * @deprecated Use {@link #getCurrentHandle()} instead. * * @return Current tab handle. */ - String getCurrentTabHandle(); + @Deprecated(since = "4.5.0", forRemoval = true) + default String getCurrentTabHandle() { + return getCurrentHandle(); + } /** * Gets opened tab handles. + * @deprecated Use {@link #getHandles()} instead. * * @return Set of tab handles. */ - Set getTabHandles(); + @Deprecated(since = "4.5.0", forRemoval = true) + default Set getTabHandles() { + return getHandles(); + } /** * Switches to tab and doesn't close current tab. + * @deprecated Use {@link #switchTo(String)} instead. * * @param tabHandle Tab handle. */ + @Deprecated(since = "4.5.0", forRemoval = true) default void switchToTab(String tabHandle) { switchToTab(tabHandle, false); } /** * Switches to tab. + * @deprecated Use {@link #switchTo(String, boolean)} instead. * * @param tabHandle Tab handle. * @param closeCurrent Close current tab if true and leave it otherwise. */ - void switchToTab(String tabHandle, boolean closeCurrent); + @Deprecated(since = "4.5.0", forRemoval = true) + default void switchToTab(String tabHandle, boolean closeCurrent) { + switchTo(tabHandle, closeCurrent); + } /** * Switches to tab and doesn't close current tab. + * @deprecated Use {@link #switchTo(int)} instead. * * @param index Tab index. */ + @Deprecated(since = "4.5.0", forRemoval = true) default void switchToTab(int index) { switchToTab(index, false); } /** * Switches to tab. + * @deprecated Use {@link #switchTo(int, boolean)} instead. * * @param index Tab index. * @param closeCurrent Close current tab if true and leave it otherwise. */ - void switchToTab(int index, boolean closeCurrent); + @Deprecated(since = "4.5.0", forRemoval = true) + default void switchToTab(int index, boolean closeCurrent) { + switchTo(index, closeCurrent); + } /** * Switches to the last tab and doesn't close current tab. + * @deprecated Use {@link #switchToLast()} instead. */ + @Deprecated(since = "4.5.0", forRemoval = true) default void switchToLastTab() { switchToLastTab(false); } /** * Switches to the last tab. + * @deprecated Use {@link #switchToLast(boolean)} instead. * * @param closeCurrent Close current tab if true and leave it otherwise. */ - void switchToLastTab(boolean closeCurrent); + @Deprecated(since = "4.5.0", forRemoval = true) + default void switchToLastTab(boolean closeCurrent) { + switchToLast(closeCurrent); + } /** * Closes current tab. + * @deprecated Use {@link #close()} instead. */ - void closeTab(); + @Deprecated(since = "4.5.0", forRemoval = true) + default void closeTab() { + close(); + } /** * Opens and switches to new tab. + * @deprecated Use {@link #openNew()} instead. */ + @Deprecated(since = "4.5.0", forRemoval = true) default void openNewTab() { openNewTab(true); } /** * Opens new tab. + * @deprecated Use {@link #openNew(boolean)} instead. * * @param switchToNew Switches to new tab if true and stays at current otherwise. */ - void openNewTab(boolean switchToNew); + @Deprecated(since = "4.5.0", forRemoval = true) + default void openNewTab(boolean switchToNew) { + openNew(switchToNew); + } /** * Opens and switches to new tab using JS function. + * @deprecated Use {@link #openNewViaJs()} instead. */ + @Deprecated(since = "4.5.0", forRemoval = true) default void openNewTabViaJs() { openNewTabViaJs(true); } /** * Opens new tab using JS function. + * @deprecated Use {@link #openNewViaJs(boolean)} instead. * * @param switchToNew Switches to new tab if true and stays at current otherwise. */ - void openNewTabViaJs(boolean switchToNew); + @Deprecated(since = "4.5.0", forRemoval = true) + default void openNewTabViaJs(boolean switchToNew) { + openNewViaJs(switchToNew); + } /** * Navigates to desired url in new tab. + * @deprecated Use {@link #openInNew(String)} instead. * * @param url String representation of URL. */ - void openInNewTab(String url); + @Deprecated(since = "4.5.0", forRemoval = true) + default void openInNewTab(String url) { + openInNew(url); + } /** * Navigates to desired url in new tab. + * @deprecated Use {@link #openInNew(URL)} instead. * * @param url target URL. */ - void openInNewTab(URL url); + @Deprecated(since = "4.5.0", forRemoval = true) + default void openInNewTab(URL url) { + openInNew(url); + } /** * Navigates to desired url in new tab. + * @deprecated Use {@link #openInNewViaJs(String)} instead. * * @param url String representation of URL. */ - void openInNewTabViaJs(String url); + @Deprecated(since = "4.5.0", forRemoval = true) + default void openInNewTabViaJs(String url) { + openInNewViaJs(url); + } } diff --git a/src/main/java/aquality/selenium/browser/IBrowserWindowNavigation.java b/src/main/java/aquality/selenium/browser/IBrowserWindowNavigation.java new file mode 100644 index 0000000..fb0c6a5 --- /dev/null +++ b/src/main/java/aquality/selenium/browser/IBrowserWindowNavigation.java @@ -0,0 +1,125 @@ +package aquality.selenium.browser; + +import java.net.URL; +import java.util.Set; + +/** + * Provides functionality to work with browser tab/window navigation. + */ +public interface IBrowserWindowNavigation { + /** + * Gets current tab/window handle. + * + * @return Current tab/window handle. + */ + String getCurrentHandle(); + + /** + * Gets opened tab/window handles. + * + * @return Set of tab/window handles. + */ + Set getHandles(); + + /** + * Switches to tab/window and doesn't close current tab/window. + * + * @param handle tab/window handle. + */ + default void switchTo(String handle) { + switchTo(handle, false); + } + + /** + * Switches to tab/window. + * + * @param handle tab/window handle. + * @param closeCurrent Close current tab/window if true and leave it otherwise. + */ + void switchTo(String handle, boolean closeCurrent); + + /** + * Switches to tab/window and doesn't close current tab/window. + * + * @param index tab/window index. + */ + default void switchTo(int index) { + switchTo(index, false); + } + + /** + * Switches to tab/window. + * + * @param index tab/window index. + * @param closeCurrent Close current tab/window if true and leave it otherwise. + */ + void switchTo(int index, boolean closeCurrent); + + /** + * Switches to the last tab/window and doesn't close current tab/window. + */ + default void switchToLast() { + switchToLast(false); + } + + /** + * Switches to the last tab/window. + * + * @param closeCurrent Close current tab/window if true and leave it otherwise. + */ + void switchToLast(boolean closeCurrent); + + /** + * Closes current tab/window. + */ + void close(); + + /** + * Opens and switches to new tab/window. + */ + default void openNew() { + openNew(true); + } + + /** + * Opens new tab/window. + * + * @param switchToNew Switches to new tab/window if true and stays at current otherwise. + */ + void openNew(boolean switchToNew); + + /** + * Opens and switches to new tab/window using JS function. + */ + default void openNewViaJs() { + openNewViaJs(true); + } + + /** + * Opens new tab/window using JS function. + * + * @param switchToNew Switches to new tab/window if true and stays at current otherwise. + */ + void openNewViaJs(boolean switchToNew); + + /** + * Navigates to desired url in new tab/window. + * + * @param url String representation of URL. + */ + void openInNew(String url); + + /** + * Navigates to desired url in new tab/window. + * + * @param url target URL. + */ + void openInNew(URL url); + + /** + * Navigates to desired url in new tab/window. + * + * @param url String representation of URL. + */ + void openInNewViaJs(String url); +} diff --git a/src/main/java/aquality/selenium/browser/JavaScript.java b/src/main/java/aquality/selenium/browser/JavaScript.java index f3825c3..2e33b93 100644 --- a/src/main/java/aquality/selenium/browser/JavaScript.java +++ b/src/main/java/aquality/selenium/browser/JavaScript.java @@ -45,6 +45,8 @@ public enum JavaScript { GET_SCREEN_OFFSET("getScreenOffset.js"), OPEN_IN_NEW_TAB("openInNewTab.js"), OPEN_NEW_TAB("openNewTab.js"), + OPEN_IN_NEW_WINDOW("openInNewWindow.js"), + OPEN_NEW_WINDOW("openNewWindow.js"), EXPAND_SHADOW_ROOT("expandShadowRoot.js"); private final String filename; diff --git a/src/main/resources/js/openInNewWindow.js b/src/main/resources/js/openInNewWindow.js new file mode 100644 index 0000000..10d1b1a --- /dev/null +++ b/src/main/resources/js/openInNewWindow.js @@ -0,0 +1 @@ +window.open(arguments[0], '', 'resizable'); diff --git a/src/main/resources/js/openNewWindow.js b/src/main/resources/js/openNewWindow.js new file mode 100644 index 0000000..526c01c --- /dev/null +++ b/src/main/resources/js/openNewWindow.js @@ -0,0 +1 @@ +window.open('', '', 'resizable'); diff --git a/src/main/resources/localization/be.json b/src/main/resources/localization/be.json index 2840c4e..9d7a554 100644 --- a/src/main/resources/localization/be.json +++ b/src/main/resources/localization/be.json @@ -88,6 +88,15 @@ "loc.browser.get.tab.handle": "Атрыманне дэскрыптара бягучай укладкі", "loc.browser.tab.open.new": "Адкрыццё новай укладкі", "loc.browser.tab.close": "Закрыццё ўкладкі", + "loc.browser.navigate.in.new.window": "Пераходзім па адрасе ў новай укладцы - '%s'", + "loc.browser.switch.to.window.handle": "Пераключэнне на акно па дэскрыптару '%1$s'", + "loc.browser.switch.to.window.index": "Пераключэнне на акно па індэксе '%1$s'", + "loc.browser.switch.to.new.window": "Пераключэнне на новае акно", + "loc.browser.get.window.handles": "Атрыманне спісу дэскрыптараў адкрытых вокнаў", + "loc.browser.get.window.handle": "Атрыманне дэскрыптара бягучага акна", + "loc.browser.window.open.new": "Адкрыццё новага акна", + "loc.browser.window.close": "Закрыццё акна", + "loc.browser.navigate.in.new.tab": "Пераходзім па адрасе ў новым акне - '%s'", "loc.browser.devtools.session.isactive": "Правяраем, ці актыўная DevTools сэсія", "loc.browser.devtools.session.isactive.result": "Ці актыўная DevTools сэсія: [%1$s]", "loc.browser.devtools.session.close": "Завяршаем сэсію DevTools", diff --git a/src/main/resources/localization/en.json b/src/main/resources/localization/en.json index fde73ae..1bb0bc4 100644 --- a/src/main/resources/localization/en.json +++ b/src/main/resources/localization/en.json @@ -88,6 +88,15 @@ "loc.browser.get.tab.handle": "Getting current tab handle", "loc.browser.tab.open.new": "Opening new tab", "loc.browser.tab.close": "Closing tab", + "loc.browser.navigate.in.new.tab": "Navigate to URL in new tab - '%s'", + "loc.browser.switch.to.window.handle": "Switching to window by handle '%1$s'", + "loc.browser.switch.to.window.index": "Switching to window by index '%1$s'", + "loc.browser.switch.to.new.window": "Switching to new window", + "loc.browser.get.window.handles": "Getting window handles", + "loc.browser.get.window.handle": "Getting current window handle", + "loc.browser.window.open.new": "Opening new window", + "loc.browser.window.close": "Closing window", + "loc.browser.navigate.in.new.window": "Navigate to URL in new window - '%s'", "loc.browser.devtools.session.isactive": "Checking if DevTools session is active", "loc.browser.devtools.session.isactive.result": "Is DevTools session active: [%1$s]", "loc.browser.devtools.session.close": "Closing DevTools session", diff --git a/src/main/resources/localization/pl.json b/src/main/resources/localization/pl.json index 20df9ba..ab3ba14 100644 --- a/src/main/resources/localization/pl.json +++ b/src/main/resources/localization/pl.json @@ -88,6 +88,15 @@ "loc.browser.get.tab.handle": "Pobieranie deskryptora bieżącej karty", "loc.browser.tab.open.new": "Otwieranie nowej karty", "loc.browser.tab.close": "Zamknięcie karty", + "loc.browser.navigate.in.new.tab": "Przejście do adresu URL w nowej karcie - '%s'", + "loc.browser.switch.to.window.handle": "Przełączanie na okno według deskryptora '%1$s'", + "loc.browser.switch.to.window.index": "Przełączanie na okno według indeksu '%1$s'", + "loc.browser.switch.to.new.window": "Przełączanie na nowe okno", + "loc.browser.get.window.handles": "Pobieranie listy deskryptorów otwartych okien", + "loc.browser.get.window.handle": "Pobieranie deskryptora bieżącego okna", + "loc.browser.window.open.new": "Otwieranie nowego okna", + "loc.browser.window.close": "Zamknięcie okna", + "loc.browser.navigate.in.new.window": "Przejście do adresu URL w nowym oknie - '%s'", "loc.browser.devtools.session.isactive": "Sprawdzanie, czy sesja DevTools jest aktywna", "loc.browser.devtools.session.isactive.result": "Czy sesja DevTools jest aktywna: [%1$s]", "loc.browser.devtools.session.close": "Zamykanie sesji DevTools", diff --git a/src/main/resources/localization/ru.json b/src/main/resources/localization/ru.json index 647ecff..a1f40d3 100644 --- a/src/main/resources/localization/ru.json +++ b/src/main/resources/localization/ru.json @@ -88,6 +88,15 @@ "loc.browser.get.tab.handle": "Получение дескриптора текущей вкладки", "loc.browser.tab.open.new": "Открытие новой вкладки", "loc.browser.tab.close": "Закрытие вкладки", + "loc.browser.navigate.in.new.tab": "Переход на страницу в новой вкладке - '%s'", + "loc.browser.switch.to.window.handle": "Переключение на окно по дескриптору '%1$s'", + "loc.browser.switch.to.window.index": "Переключение на окно по индексу '%1$s'", + "loc.browser.switch.to.new.window": "Переключение на новое окно", + "loc.browser.get.window.handles": "Получение списка дескрипторов открытых окон", + "loc.browser.get.window.handle": "Получение дескриптора текущего окна", + "loc.browser.window.open.new": "Открытие нового окна", + "loc.browser.window.close": "Закрытие окна", + "loc.browser.navigate.in.new.window": "Переход на страницу в новом окне - '%s'", "loc.browser.devtools.session.isactive": "Проверка, активна ли сессия DevTools", "loc.browser.devtools.session.isactive.result": "Активна ли DevTools сессия: [%1$s]", "loc.browser.devtools.session.close": "Завершение DevTools сессии", diff --git a/src/main/resources/localization/uk.json b/src/main/resources/localization/uk.json index 436d08b..f4914ff 100644 --- a/src/main/resources/localization/uk.json +++ b/src/main/resources/localization/uk.json @@ -88,6 +88,15 @@ "loc.browser.get.tab.handle": "Отримання дескриптора поточної вкладки", "loc.browser.tab.open.new": "Відкриття нової вкладки", "loc.browser.tab.close": "Закриття вкладки", + "loc.browser.navigate.in.new.tab": "Перехід до '%s' в новій вкладці", + "loc.browser.switch.to.window.handle": "Перехід на вікно за дескриптором '%1$s'", + "loc.browser.switch.to.window.index": "Перехід на вікно за індексом '%1$s'", + "loc.browser.switch.to.new.window": "Перехід на нове вікно", + "loc.browser.get.window.handles": "Отримання дескрипторів вікон", + "loc.browser.get.window.handle": "Отримання дескриптора поточного вікна", + "loc.browser.window.open.new": "Відкриття нового вікна", + "loc.browser.window.close": "Закриття вікна", + "loc.browser.navigate.in.new.window": "Перехід до '%s' в новому вікні", "loc.browser.devtools.session.isactive": "Перевірка, чи активний сеанс DevTools", "loc.browser.devtools.session.isactive.result": "Чи активний сеанс DevTools: [%1$s]", "loc.browser.devtools.session.close": "Закриття сеансу DevTools", diff --git a/src/test/java/tests/integration/BrowserTabsTests.java b/src/test/java/tests/integration/BrowserTabsTests.java index f39df22..3115ecd 100644 --- a/src/test/java/tests/integration/BrowserTabsTests.java +++ b/src/test/java/tests/integration/BrowserTabsTests.java @@ -1,7 +1,7 @@ package tests.integration; import aquality.selenium.browser.AqualityServices; -import aquality.selenium.browser.Browser; +import aquality.selenium.browser.IBrowserWindowNavigation; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -20,6 +20,10 @@ public class BrowserTabsTests extends BaseTest { private final WelcomeForm welcomeForm = new WelcomeForm(); + protected IBrowserWindowNavigation tabs() { + return AqualityServices.getBrowser().tabs(); + } + @Override @BeforeMethod public void beforeMethod() { @@ -29,146 +33,135 @@ public void beforeMethod() { @Test public void testShouldBePossibleToOpenUrlInNewTab() { String 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"); + tabs().openInNew(url); + assertEquals(2, tabs().getHandles().size()); + assertEquals(AqualityServices.getBrowser().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"); + tabs().openInNew(url); + assertEquals(2, tabs().getHandles().size()); + assertEquals(AqualityServices.getBrowser().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"); + tabs().openInNewViaJs(url); + assertEquals(2, tabs().getHandles().size()); + assertEquals(AqualityServices.getBrowser().getCurrentUrl(), url + "/", "Url should be opened in new tab"); } @Test public void testShouldBePossibleToHandleTab() { - Browser browser = AqualityServices.getBrowser(); - String tabHandle = browser.tabs().getCurrentTabHandle(); + String tabHandle = tabs().getCurrentHandle(); assertFalse(tabHandle.isEmpty(), "Tab handle should not be empty"); } @Test public void testShouldBePossibleToGetTabHandles() { - Browser browser = AqualityServices.getBrowser(); - Set tabHandles = browser.tabs().getTabHandles(); + Set tabHandles = tabs().getHandles(); assertEquals(1, tabHandles.size(), "Tab number should be correct"); assertFalse(tabHandles.stream().findFirst().toString().isEmpty(), "Tab handle should not be empty"); } @Test public void testShouldBePossibleToOpenNewTab() { - Browser browser = AqualityServices.getBrowser(); - String tabHandle = browser.tabs().getCurrentTabHandle(); + String tabHandle = tabs().getCurrentHandle(); - browser.tabs().openNewTab(); - String newTabHandle = browser.tabs().getCurrentTabHandle(); - assertEquals(2, browser.tabs().getTabHandles().size(), "New tab should be opened"); + tabs().openNew(); + String newTabHandle = tabs().getCurrentHandle(); + assertEquals(2, tabs().getHandles().size(), "New tab should be opened"); assertNotEquals(tabHandle, newTabHandle, "Browser should be switched to new tab"); - browser.tabs().openNewTab(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"); + tabs().openNew(false); + assertEquals(3, tabs().getHandles().size(), "New tab should be opened"); + assertEquals(newTabHandle, tabs().getCurrentHandle(), "Browser should not be switched to new tab"); } @Test public void testShouldBePossibleToOpenNewTabViaJs() { - Browser browser = AqualityServices.getBrowser(); - String tabHandle = browser.tabs().getCurrentTabHandle(); + String tabHandle = tabs().getCurrentHandle(); - browser.tabs().openNewTabViaJs(); - String newTabHandle = browser.tabs().getCurrentTabHandle(); - assertEquals(2, browser.tabs().getTabHandles().size(), "New tab should be opened"); + tabs().openNewViaJs(); + String newTabHandle = tabs().getCurrentHandle(); + assertEquals(2, tabs().getHandles().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"); + tabs().openNewViaJs(false); + assertEquals(3, tabs().getHandles().size(), "New tab should be opened"); + assertEquals(newTabHandle, tabs().getCurrentHandle(), "Browser should not be switched to new tab"); } @Test public void testShouldBePossibleToCloseTab() { - Browser browser = AqualityServices.getBrowser(); welcomeForm.clickElementalSelenium(); - assertEquals(2, browser.tabs().getTabHandles().size(), "New tab should be opened"); - browser.tabs().closeTab(); - assertEquals(1, browser.tabs().getTabHandles().size(), "New tab should be closed"); + assertEquals(2, tabs().getHandles().size(), "New tab should be opened"); + tabs().close(); + assertEquals(1, tabs().getHandles().size(), "New tab should be closed"); } @Test public void testShouldBePossibleToSwitchToNewTab() { - checkSwitching(2, () -> AqualityServices.getBrowser().tabs().switchToLastTab()); + checkSwitching(2, () -> tabs().switchToLast()); } @Test public void testShouldBePossibleToSwitchToNewTabAndClose() { - checkSwitching(1, () -> AqualityServices.getBrowser().tabs().switchToLastTab(true)); + checkSwitching(1, () -> tabs().switchToLast(true)); } @Test public void testShouldBePossibleToSwitchToTabByHandle() { checkSwitching(3, () -> { - Browser browser = AqualityServices.getBrowser(); String tabHandle = getLastTab(); - browser.tabs().openNewTab(false); - browser.tabs().switchToTab(tabHandle); + tabs().openNew(false); + tabs().switchTo(tabHandle); }); } @Test public void testShouldBePossibleToSwitchToTabByHandleAndClose() { checkSwitching(2, () -> { - Browser browser = AqualityServices.getBrowser(); String tabHandle = getLastTab(); - browser.tabs().openNewTab(false); - browser.tabs().switchToTab(tabHandle, true); + tabs().openNew(false); + tabs().switchTo(tabHandle, true); }); } @Test public void testShouldBePossibleToSwitchToTabByIndex() { checkSwitching(3, () -> { - AqualityServices.getBrowser().tabs().openNewTab(false); - AqualityServices.getBrowser().tabs().switchToTab(1); + tabs().openNew(false); + tabs().switchTo(1); }); } @Test public void testShouldBePossibleToSwitchToTabByIndexAndClose() { checkSwitching(2, () -> { - AqualityServices.getBrowser().tabs().openNewTab(false); - AqualityServices.getBrowser().tabs().switchToTab(1, true); + tabs().openNew(false); + tabs().switchTo(1, true); }); } @Test public void testShouldThrowIfSwitchToNewTabByIncorrectIndex() { - Assert.expectThrows(IndexOutOfBoundsException.class, () -> AqualityServices.getBrowser().tabs().switchToTab(10, true)); + Assert.expectThrows(IndexOutOfBoundsException.class, () -> tabs().switchTo(10, true)); } private void checkSwitching(int expectedTabCount, Runnable switchMethod) { - Browser browser = AqualityServices.getBrowser(); welcomeForm.clickElementalSelenium(); String newTabHandle = getLastTab(); switchMethod.run(); - assertEquals(newTabHandle, browser.tabs().getCurrentTabHandle(), "Browser should be switched to correct tab"); - assertEquals(expectedTabCount, browser.tabs().getTabHandles().size(), "Number of tabs should be correct"); + assertEquals(newTabHandle, tabs().getCurrentHandle(), "Browser should be switched to correct tab"); + assertEquals(expectedTabCount, tabs().getHandles().size(), "Number of tabs should be correct"); } private String getLastTab() { - List tabs = new ArrayList<>(AqualityServices.getBrowser().tabs().getTabHandles()); + List tabs = new ArrayList<>(tabs().getHandles()); return tabs.get(tabs.size() - 1); } -} \ No newline at end of file +} diff --git a/src/test/java/tests/integration/BrowserTests.java b/src/test/java/tests/integration/BrowserTests.java index b216839..71f13a9 100644 --- a/src/test/java/tests/integration/BrowserTests.java +++ b/src/test/java/tests/integration/BrowserTests.java @@ -130,7 +130,7 @@ public void testShouldBePossibleToExecuteJavaScriptAsync(){ } @Test (expectedExceptions = ScriptTimeoutException.class) - public void testScriptTimeoutExeceptionShouldBeThrownIfScriptTimeoutIsOver(){ + public void testScriptTimeoutExceptionShouldBeThrownIfScriptTimeoutIsOver(){ String url = new DynamicContentForm().getUrl(); getBrowser().goTo(url); getBrowser().waitForPageToLoad(); diff --git a/src/test/java/tests/integration/BrowserWindowsTests.java b/src/test/java/tests/integration/BrowserWindowsTests.java new file mode 100644 index 0000000..e191c0f --- /dev/null +++ b/src/test/java/tests/integration/BrowserWindowsTests.java @@ -0,0 +1,11 @@ +package tests.integration; + +import aquality.selenium.browser.AqualityServices; +import aquality.selenium.browser.IBrowserWindowNavigation; + +public class BrowserWindowsTests extends BrowserTabsTests { + @Override + protected IBrowserWindowNavigation tabs() { + return AqualityServices.getBrowser().windows(); + } +} diff --git a/src/test/java/tests/usecases/devtools/DevToolsSessionTest.java b/src/test/java/tests/usecases/devtools/DevToolsSessionTest.java index dc686f5..aea9d32 100644 --- a/src/test/java/tests/usecases/devtools/DevToolsSessionTest.java +++ b/src/test/java/tests/usecases/devtools/DevToolsSessionTest.java @@ -13,7 +13,7 @@ private static DevToolsHandling devTools() { @Test public void getAndCloseDevToolsSessionTest() { - String windowHandle = getBrowser().tabs().getCurrentTabHandle(); + String windowHandle = getBrowser().tabs().getCurrentHandle(); Assert.assertFalse(devTools().hasActiveDevToolsSession(), "No DevTools session should be running initially"); Assert.assertNotNull(devTools().getDevToolsSession(windowHandle), "Should be possible to get DevTools session"); Assert.assertTrue(devTools().hasActiveDevToolsSession(), "DevTools session should be indicated as active after getting"); diff --git a/src/test/java/tests/usecases/devtools/DeviceEmulationTest.java b/src/test/java/tests/usecases/devtools/DeviceEmulationTest.java index ffab8fb..7d5ab68 100644 --- a/src/test/java/tests/usecases/devtools/DeviceEmulationTest.java +++ b/src/test/java/tests/usecases/devtools/DeviceEmulationTest.java @@ -3,8 +3,8 @@ import aquality.selenium.browser.AqualityServices; import aquality.selenium.browser.devtools.EmulationHandling; import com.google.common.collect.ImmutableMap; -import org.openqa.selenium.devtools.v123.emulation.Emulation; -import org.openqa.selenium.devtools.v123.emulation.model.DisplayFeature; +import org.openqa.selenium.devtools.v124.emulation.Emulation; +import org.openqa.selenium.devtools.v124.emulation.model.DisplayFeature; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; diff --git a/src/test/java/tests/usecases/devtools/NetworkSpeedEmulationTest.java b/src/test/java/tests/usecases/devtools/NetworkSpeedEmulationTest.java index f7ba6a6..b8e30a5 100644 --- a/src/test/java/tests/usecases/devtools/NetworkSpeedEmulationTest.java +++ b/src/test/java/tests/usecases/devtools/NetworkSpeedEmulationTest.java @@ -2,7 +2,7 @@ import aquality.selenium.browser.AqualityServices; import org.openqa.selenium.TimeoutException; -import org.openqa.selenium.devtools.v123.network.model.ConnectionType; +import org.openqa.selenium.devtools.v124.network.model.ConnectionType; import org.testng.Assert; import org.testng.annotations.Test; import tests.BaseTest; diff --git a/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java b/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java index 26ff6fd..d5b966c 100644 --- a/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java +++ b/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java @@ -8,7 +8,7 @@ import manytools.BrowserLanguageForm; import manytools.UserAgentForm; import org.openqa.selenium.devtools.idealized.Network; -import org.openqa.selenium.devtools.v123.emulation.Emulation; +import org.openqa.selenium.devtools.v124.emulation.Emulation; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; diff --git a/src/test/java/w3schools/forms/SelectMultipleForm.java b/src/test/java/w3schools/forms/SelectMultipleForm.java index 7277e83..bf401d3 100644 --- a/src/test/java/w3schools/forms/SelectMultipleForm.java +++ b/src/test/java/w3schools/forms/SelectMultipleForm.java @@ -1,11 +1,13 @@ package w3schools.forms; import aquality.selenium.browser.AqualityServices; +import aquality.selenium.core.logging.Logger; import aquality.selenium.elements.interfaces.IButton; import aquality.selenium.elements.interfaces.IMultiChoiceBox; import aquality.selenium.elements.interfaces.ITextBox; import aquality.selenium.forms.Form; import org.openqa.selenium.By; +import org.openqa.selenium.ElementClickInterceptedException; import java.util.ArrayList; import java.util.List; @@ -15,6 +17,8 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import static aquality.selenium.browser.AqualityServices.getConfiguration; + public class SelectMultipleForm extends Form { private final IMultiChoiceBox cbxCars = getElementFactory().getMultiChoiceBox(By.id("cars"), "Cars"); @@ -27,7 +31,7 @@ public SelectMultipleForm() { } public void acceptCookies() { - if (btnAcceptCookies.state().isDisplayed()) { + if (btnAcceptCookies.state().waitForDisplayed(getConfiguration().getTimeoutConfiguration().getScript())) { btnAcceptCookies.click(); btnAcceptCookies.state().waitForNotDisplayed(); } @@ -38,7 +42,16 @@ public void switchToResultFrame() { } public void submit() { - btnSubmit.click(); + try { + btnSubmit.click(); + } + catch (ElementClickInterceptedException e) { + Logger.getInstance().debug(e.getMessage(), e); + AqualityServices.getBrowser().getDriver().switchTo().defaultContent(); + acceptCookies(); + switchToResultFrame(); + btnSubmit.click(); + } } public List getValuesFromResult() {