io.magentys.cinnamon.webdriver.Browser Maven / Gradle / Ivy
package io.magentys.cinnamon.webdriver;
import io.magentys.cinnamon.webdriver.actions.synthetic.DomEvent;
import io.magentys.cinnamon.webdriver.actions.synthetic.SyntheticEvent;
import io.magentys.cinnamon.webdriver.conditions.Condition;
import io.magentys.cinnamon.webdriver.support.ui.CinnamonExpectedConditions;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import static io.magentys.cinnamon.webdriver.Timeouts.defaultTimeout;
import static io.magentys.cinnamon.webdriver.WebDriverContainer.getWebDriverContainer;
public final class Browser {
/**
* Opens a new web page in the current browser window.
*
* @param url The URL to load
*/
public static void open(String url) {
getWebDriver().get(url);
}
public static String currentUrl() {
return getWebDriver().getCurrentUrl();
}
/**
* Gets the title of the current page.
*
* @return The title of the current page
*/
public static String title() {
return getWebDriver().getTitle();
}
public static void back() {
getWebDriver().navigate().back();
}
public static void forward() {
getWebDriver().navigate().forward();
}
public static void refresh() {
getWebDriver().navigate().refresh();
}
/**
* Closes the browser window that has focus.
*/
public static void close() {
getWebDriver().close();
}
public static CinnamonTargetLocator switchTo() {
return new CinnamonTargetLocator(getWebDriver(), getWindowTracker());
}
public static Alert alert() {
return alert(defaultTimeout());
}
public static Alert alert(final Timeout timeout) {
return switchTo().alert(timeout);
}
public static void fireEvent(final WebElement element, final DomEvent event) {
syntheticEvent().fireEvent(element, event);
}
/**
* Gets the Options interface
*
* Binds to an instance of CinnamonWebDriverOptions. Uses Javascript to manage cookies if the browser
* is either InternetExplorer or Safari.
*
* @return WebDriver.Options
* @see org.openqa.selenium.WebDriver.Options
*/
public static WebDriver.Options manage() {
return new CinnamonWebDriverOptions(getWebDriver());
}
public static void waitUntil(final Condition condition) {
waitUntil(condition, defaultTimeout());
}
public static void waitUntil(final Condition condition, final Timeout timeout) {
wait(timeout).until(CinnamonExpectedConditions.conditionToBe(condition));
}
private static FluentWait wait(final Timeout timeout) {
return new WebDriverWait(getWebDriver(), timeout.getSeconds());
}
/**
* Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of
* milliseconds.
*
* This method is not recommended. Instead, you should identify a more robust synchronisation solution.
*
* @param millis The time to sleep in milliseconds
* @see Thread#sleep(long)
*/
public static void sleep(long millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private static WebDriver getWebDriver() {
return getWebDriverContainer().getWebDriver();
}
private static WindowTracker getWindowTracker() {
return getWebDriverContainer().getWindowTracker();
}
private static SyntheticEvent syntheticEvent() {
return new SyntheticEvent(getWebDriver());
}
}