com.testvagrant.ekam.atoms.web.BrowserDriver Maven / Gradle / Ivy
package com.testvagrant.ekam.atoms.web;
import com.google.inject.Inject;
import org.awaitility.Awaitility;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.Optional;
import java.util.function.Supplier;
public class BrowserDriver {
private final WebDriver driver;
@Inject
public BrowserDriver(WebDriver driver) {
this.driver = driver;
}
public void get(String url) {
this.driver.get(url);
}
public String currentUrl() {
return this.driver.getCurrentUrl();
}
public void deleteCookies() {
this.driver.manage().deleteAllCookies();
}
public void switchTab() {
String currentWindowHandle = this.driver.getWindowHandle();
Optional tab =
this.driver.getWindowHandles().stream()
.filter(handle -> !handle.equals(currentWindowHandle))
.findFirst();
tab.ifPresent(s -> this.driver.switchTo().window(s));
}
public String title() {
return this.driver.getTitle();
}
private JavascriptExecutor jsDriver() {
return ((JavascriptExecutor) this.driver);
}
public void switchToDefaultContent() {
this.driver.switchTo().defaultContent();
}
public void waitUntil(Supplier condition) {
waitUntil(condition, 30);
}
public void waitUntil(Supplier condition, long timeOutInSeconds) {
try {
Awaitility.await()
.ignoreExceptions()
.timeout(Duration.ofSeconds(timeOutInSeconds))
.until(condition::get);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public void waitForFrameToLoad(String frameId) {
WebDriverWait webDriverWait = new WebDriverWait(this.driver, Duration.ofSeconds(30));
webDriverWait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameId));
}
public void waitForFrameToLoad(Integer index) {
WebDriverWait webDriverWait = new WebDriverWait(this.driver, Duration.ofSeconds(30));
webDriverWait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(index));
}
public void waitForPageToLoad() {
waitUntil(() -> this.jsDriver().executeScript("return document.readyState").equals("complete"));
}
public void navigateBack() {
this.driver.navigate().back();
}
public void scrollToBottom() {
jsDriver().executeScript("window.scrollTo(0, document.body.scrollHeight)");
}
public LogEntries getBrowserLogs() {
return driver.manage().logs().get("browser");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy