Skip to content

Commit

Permalink
Add browser window navigation (#140) +semver:feature
Browse files Browse the repository at this point in the history
Add browser window navigation +semver:feature
- Update to Selenium 4.20.0
  • Loading branch information
mialeska authored May 6, 2024
1 parent 8107606 commit de3307a
Show file tree
Hide file tree
Showing 22 changed files with 465 additions and 195 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-selenium-core</artifactId>
<version>4.0.1</version>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand All @@ -92,24 +92,24 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.1</version>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.12</version>
<version>2.0.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>[4.7.0,)</version>
<version>[4.9.0,)</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/aquality/selenium/browser/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
113 changes: 0 additions & 113 deletions src/main/java/aquality/selenium/browser/BrowserTabNavigation.java

This file was deleted.

130 changes: 130 additions & 0 deletions src/main/java/aquality/selenium/browser/BrowserWindowNavigation.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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);
}
}
Loading

0 comments on commit de3307a

Please sign in to comment.