com.codeborne.selenide.SelenideDriver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenide-core Show documentation
Show all versions of selenide-core Show documentation
Selenide = concise API for Selenium WebDriver
package com.codeborne.selenide;
import com.codeborne.selenide.drivercommands.LazyDriver;
import com.codeborne.selenide.drivercommands.Navigator;
import com.codeborne.selenide.drivercommands.WebDriverWrapper;
import com.codeborne.selenide.impl.DownloadFileWithHttpRequest;
import com.codeborne.selenide.impl.ElementFinder;
import com.codeborne.selenide.impl.JavaScript;
import com.codeborne.selenide.impl.PageObjectFactory;
import com.codeborne.selenide.impl.ScreenShotLaboratory;
import com.codeborne.selenide.logevents.SelenideLogger;
import com.codeborne.selenide.proxy.SelenideProxyServer;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.support.events.WebDriverEventListener;
import org.openqa.selenium.support.events.WebDriverListener;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import static com.codeborne.selenide.files.FileFilters.none;
import static com.codeborne.selenide.impl.Plugins.inject;
import static com.codeborne.selenide.impl.WebElementWrapper.wrap;
import static java.util.Collections.emptyList;
/**
* "Selenide driver" is a container for WebDriver + proxy server + settings
*/
@ParametersAreNonnullByDefault
public class SelenideDriver {
private final JavaScript zoomJs = new JavaScript("zoom.js");
private static final Navigator navigator = new Navigator();
private static final ScreenShotLaboratory screenshots = ScreenShotLaboratory.getInstance();
private final Config config;
private final Driver driver;
public SelenideDriver(Config config) {
this(config, emptyList(), emptyList());
}
/**
* @deprecated Class {@link WebDriverEventListener} is deprecated since Selenium 4.0.0
*/
@Deprecated
public SelenideDriver(Config config, List eventListeners) {
this(config, eventListeners, emptyList());
}
/**
* @since 6.0.0
*/
public SelenideDriver(Config config, List eventListeners, List listeners) {
this(config, new LazyDriver(config, null, eventListeners, listeners));
}
public SelenideDriver(Config config, Driver driver) {
this.config = config;
this.driver = driver;
}
public SelenideDriver(Config config, WebDriver webDriver, @Nullable SelenideProxyServer selenideProxy) {
this(config, webDriver, selenideProxy, new SharedDownloadsFolder(config.downloadsFolder()));
}
public SelenideDriver(Config config, WebDriver webDriver, @Nullable SelenideProxyServer selenideProxy,
DownloadsFolder browserDownloadsFolder) {
this.config = config;
this.driver = new WebDriverWrapper(config, webDriver, selenideProxy, browserDownloadsFolder);
}
@CheckReturnValue
@Nonnull
public Config config() {
return config;
}
@CheckReturnValue
@Nonnull
public Driver driver() {
return driver;
}
public void open() {
navigator.open(this);
}
public void open(String relativeOrAbsoluteUrl) {
navigator.open(this, relativeOrAbsoluteUrl);
}
public void open(URL absoluteUrl) {
navigator.open(this, absoluteUrl);
}
public void open(String relativeOrAbsoluteUrl, String domain, String login, String password) {
navigator.open(this, relativeOrAbsoluteUrl, domain, login, password);
}
public void open(String relativeOrAbsoluteUrl, AuthenticationType authenticationType, Credentials credentials) {
navigator.open(this, relativeOrAbsoluteUrl, authenticationType, credentials);
}
public void open(URL absoluteUrl, String domain, String login, String password) {
navigator.open(this, absoluteUrl, domain, login, password);
}
@CheckReturnValue
@Nonnull
public PageObjectClass open(String relativeOrAbsoluteUrl,
Class pageObjectClassClass) {
return open(relativeOrAbsoluteUrl, "", "", "", pageObjectClassClass);
}
@CheckReturnValue
@Nonnull
public PageObjectClass open(URL absoluteUrl,
Class pageObjectClassClass) {
return open(absoluteUrl, "", "", "", pageObjectClassClass);
}
@CheckReturnValue
@Nonnull
public PageObjectClass open(String relativeOrAbsoluteUrl,
String domain, String login, String password,
Class pageObjectClassClass) {
open(relativeOrAbsoluteUrl, domain, login, password);
return page(pageObjectClassClass);
}
@CheckReturnValue
@Nonnull
public PageObjectClass open(URL absoluteUrl, String domain, String login, String password,
Class pageObjectClassClass) {
open(absoluteUrl, domain, login, password);
return page(pageObjectClassClass);
}
@CheckReturnValue
@Nonnull
public PageObjectClass page(Class pageObjectClass) {
return pageFactory.page(driver(), pageObjectClass);
}
/**
* @since 6.8.0
* @param reified Don't pass any values here. It's Java Magic :)
*/
@CheckReturnValue
@Nonnull
@SuppressWarnings("unchecked")
public PageObjectClass page(PageObjectClass... reified) {
if (reified.length > 0) {
throw new IllegalArgumentException("Please don't pass any values here. Java will detect page object class automagically.");
}
return pageFactory.page(driver(), (Class) reified.getClass().getComponentType());
}
@CheckReturnValue
@Nonnull
public PageObjectClass page(T pageObject) {
return pageFactory.page(driver(), pageObject);
}
public void refresh() {
navigator.refresh(driver());
}
public void back() {
navigator.back(driver());
}
public void forward() {
navigator.forward(driver());
}
public void updateHash(String hash) {
SelenideLogger.run("updateHash", hash, () -> {
String localHash = (hash.charAt(0) == '#') ? hash.substring(1) : hash;
executeJavaScript("window.location.hash='" + localHash + "'");
});
}
@CheckReturnValue
@Nonnull
public Browser browser() {
return driver().browser();
}
@CheckReturnValue
@Nullable
public SelenideProxyServer getProxy() {
return driver().getProxy();
}
public boolean hasWebDriverStarted() {
return driver().hasWebDriverStarted();
}
@CheckReturnValue
@Nonnull
public WebDriver getWebDriver() {
return driver.getWebDriver();
}
@Nonnull
public WebDriver getAndCheckWebDriver() {
return driver.getAndCheckWebDriver();
}
public void clearCookies() {
SelenideLogger.run("clearCookies", "", () -> {
driver().clearCookies();
});
}
public void close() {
driver.close();
}
public T executeJavaScript(String jsCode, Object... arguments) {
return driver().executeJavaScript(jsCode, arguments);
}
public T executeAsyncJavaScript(String jsCode, Object... arguments) {
return driver().executeAsyncJavaScript(jsCode, arguments);
}
@CheckReturnValue
@Nullable
public WebElement getFocusedElement() {
return executeJavaScript("return document.activeElement");
}
/**
* Returns selected text or empty string if no text is selected.
*
* @return selected text
* @since 6.11.0
*/
@CheckReturnValue
@Nonnull
public String getSelectedText() {
return this.executeJavaScript("return window.getSelection().toString()");
}
/**
* Copy selected text or empty string if no text is selected to clipboard.
*
* @return the copied text
*
* @see #getClipboard()
* @see Clipboard
* @since 6.11.0
*/
public String copy() {
String selectedText = this.getSelectedText();
this.getClipboard().setText(selectedText);
return selectedText;
}
@CheckReturnValue
@Nonnull
public SelenideWait Wait() {
return new SelenideWait(getWebDriver(), config().timeout(), config().pollingInterval());
}
public void zoom(double factor) {
zoomJs.execute(driver(), factor);
}
@Nullable
public String title() {
return getWebDriver().getTitle();
}
@CheckReturnValue
@Nonnull
public SelenideElement $(WebElement webElement) {
return wrap(driver(), webElement);
}
@CheckReturnValue
@Nonnull
public SelenideElement $(String cssSelector) {
return find(cssSelector);
}
@CheckReturnValue
@Nonnull
public SelenideElement find(String cssSelector) {
return find(By.cssSelector(cssSelector));
}
@CheckReturnValue
@Nonnull
public SelenideElement $x(String xpathExpression) {
return find(By.xpath(xpathExpression));
}
@CheckReturnValue
@Nonnull
public SelenideElement $(By seleniumSelector) {
return find(seleniumSelector);
}
@CheckReturnValue
@Nonnull
public SelenideElement $(By seleniumSelector, int index) {
return find(seleniumSelector, index);
}
@CheckReturnValue
@Nonnull
public SelenideElement $(String cssSelector, int index) {
return ElementFinder.wrap(driver(), cssSelector, index);
}
@CheckReturnValue
@Nonnull
public SelenideElement find(By criteria) {
return ElementFinder.wrap(driver(), null, criteria, 0);
}
@CheckReturnValue
@Nonnull
public SelenideElement find(By criteria, int index) {
return ElementFinder.wrap(driver(), null, criteria, index);
}
@CheckReturnValue
@Nonnull
public ElementsCollection $$(Collection extends WebElement> elements) {
return new ElementsCollection(driver(), elements);
}
@CheckReturnValue
@Nonnull
public ElementsCollection $$(String cssSelector) {
return new ElementsCollection(driver(), cssSelector);
}
@CheckReturnValue
@Nonnull
public ElementsCollection $$x(String xpathExpression) {
return $$(By.xpath(xpathExpression));
}
@CheckReturnValue
@Nonnull
public ElementsCollection findAll(By seleniumSelector) {
return new ElementsCollection(driver(), seleniumSelector);
}
@CheckReturnValue
@Nonnull
public ElementsCollection findAll(String cssSelector) {
return new ElementsCollection(driver(), By.cssSelector(cssSelector));
}
@CheckReturnValue
@Nonnull
public ElementsCollection $$(By criteria) {
return findAll(criteria);
}
@CheckReturnValue
@Nullable
public SelenideElement getSelectedRadio(By radioField) {
for (WebElement radio : $$(radioField)) {
if (radio.getAttribute("checked") != null) {
return $(radio);
}
}
return null;
}
@CheckReturnValue
@Nonnull
public Modal modal() {
return new Modal(driver());
}
@CheckReturnValue
@Nonnull
public WebDriverLogs getWebDriverLogs() {
return new WebDriverLogs(driver());
}
public void clearBrowserLocalStorage() {
executeJavaScript("localStorage.clear();");
}
public boolean atBottom() {
return executeJavaScript("return window.pageYOffset + window.innerHeight >= document.body.scrollHeight");
}
@Nonnull
public SelenideTargetLocator switchTo() {
return driver().switchTo();
}
@CheckReturnValue
@Nonnull
public String url() {
return getWebDriver().getCurrentUrl();
}
@CheckReturnValue
@Nullable
public String source() {
return getWebDriver().getPageSource();
}
@CheckReturnValue
@Nonnull
public String getCurrentFrameUrl() {
return executeJavaScript("return window.location.href").toString();
}
@CheckReturnValue
@Nonnull
public String getUserAgent() {
return driver().getUserAgent();
}
@CheckReturnValue
@Nonnull
public SessionId getSessionId() {
return driver().getSessionId();
}
/**
* Take a screenshot of the current page
*
* @return absolute path of the screenshot taken or null if failed to create screenshot
* @since 5.14.0
*/
@CheckReturnValue
@Nullable
public String screenshot(String fileName) {
return screenshots.takeScreenshot(driver(), fileName, true, false).getImage();
}
/**
* Take a screenshot of the current page
*
* @return The screenshot (as bytes, base64 or temporary file)
* @since 5.14.0
*/
@CheckReturnValue
@Nullable
public T screenshot(OutputType outputType) {
return screenshots.takeScreenShot(driver(), outputType);
}
@Nonnull
public File download(String url) throws IOException, URISyntaxException {
return download(new URI(url), config.timeout());
}
@Nonnull
public File download(String url, long timeoutMs) throws IOException, URISyntaxException {
return download(new URI(url), timeoutMs);
}
@Nonnull
public File download(URI url) throws IOException {
return download(url, config.timeout());
}
@Nonnull
public File download(URI url, long timeoutMs) throws IOException {
return downloadFileWithHttpRequest().download(driver(), url, timeoutMs, none());
}
@CheckReturnValue
@Nonnull
public LocalStorage getLocalStorage() {
return new LocalStorage(driver());
}
@CheckReturnValue
@Nonnull
public SessionStorage getSessionStorage() {
return new SessionStorage(driver());
}
@CheckReturnValue
@Nonnull
public Clipboard getClipboard() {
return inject(ClipboardService.class).getClipboard(driver());
}
private static final PageObjectFactory pageFactory = inject(PageObjectFactory.class);
private static DownloadFileWithHttpRequest downloadFileWithHttpRequest;
private static synchronized DownloadFileWithHttpRequest downloadFileWithHttpRequest() {
if (downloadFileWithHttpRequest == null) downloadFileWithHttpRequest = new DownloadFileWithHttpRequest();
return downloadFileWithHttpRequest;
}
public Conditional webdriver() {
return new Conditional() {
@Nonnull
@Override
public Driver driver() {
return SelenideDriver.this.driver();
}
@Nonnull
@Override
public WebDriver object() {
return SelenideDriver.this.getWebDriver();
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy