Class Selenide
You start with methods open(String)
for opening the tested application page and
$(String)
for searching web elements.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic SelenideElement
Locates the first element matching given CSS selector (lazy evaluation)static SelenideElement
Locates the Nth element matching given criteria (lazy evaluation)static SelenideElement
$
(org.openqa.selenium.By seleniumSelector) Locates the first element matching given CSS selector (lazy evaluation)static SelenideElement
$
(org.openqa.selenium.By seleniumSelector, int index) static SelenideElement
$
(org.openqa.selenium.WebElement webElement) Wrap standard Selenium WebElement into SelenideElement to use additional methods likeSelenideElement.should(WebElementCondition...)
,SelenideElement.selectOption(String, String...)
etc.static ElementsCollection
Locates all elements matching given CSS selector (lazy evaluation).static ElementsCollection
$$
(Collection<? extends org.openqa.selenium.WebElement> elements) Initialize collection with Elementsstatic ElementsCollection
$$
(org.openqa.selenium.By seleniumSelector) Locates all elements matching given CSS selector.static ElementsCollection
Locates all elements matching given XPATH expression (lazy evaluation)static SelenideElement
Locates the first element matching given XPATH expression (lazy evaluation)static org.openqa.selenium.interactions.Actions
actions()
With this method you can use Selenium Actions like described in the AdvancedUserInteractions page.static boolean
atBottom()
Return true if bottom of the page is reachedstatic void
back()
Navigate browser back to previous pagestatic void
Clear browser cookies.static void
Clear browser local storage.static Clipboard
Provide access to system clipboard, allows get and set String content.static void
Close the browser if it's open.static void
Close the current window, quitting the browser if it's the last window currently open.static String
confirm()
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'alert' or 'confirm').static String
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'alert' or 'confirm').static String
confirm
(ModalOptions options) Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'alert' or 'confirm').static String
copy()
Copy selected text or empty string if no text is selected to clipboard.static String
dismiss()
Dismiss (click "No" or "Cancel") in the confirmation dialog (javascript 'alert' or 'confirm').static String
Dismiss (click "No" or "Cancel") in the confirmation dialog (javascript 'alert' or 'confirm').static String
dismiss
(ModalOptions options) Dismiss (click "No" or "Cancel") in the confirmation dialog (javascript 'alert' or 'confirm').static File
NB! URL must be properly encoded.static File
Download file using a direct link.static File
static File
static SelenideElement
Locates the first element matching given CSS selectorstatic SelenideElement
Locates the Nth element matching given criteriastatic SelenideElement
element
(org.openqa.selenium.By seleniumSelector) Locates the first element matching given CSS selectorstatic SelenideElement
element
(org.openqa.selenium.By seleniumSelector, int index) Locates the Nth element matching given criteriastatic SelenideElement
element
(org.openqa.selenium.WebElement webElement) Wrap standard Selenium WebElement into SelenideElement to use additional methods likeSelenideElement.shouldHave(WebElementCondition...)
,SelenideElement.selectOption(String, String...)
etc.static ElementsCollection
Locates all elements matching given CSS selector.static ElementsCollection
elements
(Collection<? extends org.openqa.selenium.WebElement> elements) Wrap standard Selenium WebElement collection into SelenideElement collection to use additional methods likeSelenideElement.shouldHave(WebElementCondition...)
etc.static ElementsCollection
elements
(org.openqa.selenium.By seleniumSelector) Locates all elements matching given CSS selector.static <T> @Nullable T
executeAsyncJavaScript
(String jsCode, Object... arguments) static <T> @Nullable T
executeJavaScript
(String jsCode, Object... arguments) static void
forward()
Navigate browser forward to next pagestatic SelenideElement
static @Nullable SelenideElement
getSelectedRadio
(org.openqa.selenium.By radioField) Returns selected element in radio groupstatic String
Returns selected text or empty string if no text is selected.static String
Get current user agent from browser sessiongetWebDriverLogs
(String logType) Same asgetWebDriverLogs(String, Level)
getWebDriverLogs
(String logType, Level logLevel) Getting and filtering of the WebDriver logs for specified LogType by specified logging level
For example to get WebDriver Browser's console output (including JS info, warnings, errors, etc.static void
inNewBrowser
(Config config, Runnable lambda) Open a new browser (with given settings), and run given code block in this browser.static void
inNewBrowser
(Runnable lambda) Open a new browser (with the same settings as the default browser), and run given code block in this browser.static LocalStorage
Access browser's local storage.static void
open()
Open an empty browser (without opening any pages).static void
Open an empty browser with given config.static void
The main starting point in your tests.static void
open
(String relativeOrAbsoluteUrl, AuthenticationType authenticationType, Credentials credentials) The main starting point in your tests.static void
Opens browser with given config.static <PageObjectClass>
PageObjectClassOpen a web page and create PageObject for it.static void
The main starting point in your tests.static <PageObjectClass>
PageObjectClassopen
(String relativeOrAbsoluteUrl, String domain, String login, String password, Class<PageObjectClass> pageObjectClassClass) Open a web page using Basic Auth credentials and create PageObject for it.static void
static <PageObjectClass>
PageObjectClassOpen a web page and create PageObject for it.static void
static <PageObjectClass>
PageObjectClassopen
(URL absoluteUrl, String domain, String login, String password, Class<PageObjectClass> pageObjectClassClass) Open a web page using Basic Auth credentials and create PageObject for it.static <PageObjectClass>
PageObjectClassCreate a Page Object instancestatic <PageObjectClass>
PageObjectClasspage
(PageObjectClass... reified) Create a Page Object instancestatic <PageObjectClass,
T extends PageObjectClass>
PageObjectClasspage
(T pageObject) Initialize a given Page Object instancestatic String
prompt()
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'prompt').static String
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'prompt').static String
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'prompt').static String
prompt
(ModalOptions options, @Nullable String inputText) Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'prompt').static void
refresh()
Reload current pagestatic @Nullable String
screenshot
(String fileName) Take the screenshot of current page and save to file "fileName.png" (and optionally, "fileName.html") File "fileName.png" is created always, even ifConfiguration.screenshots == false
File "fileName.html" is created only ifConfiguration.savePageSource == true
static <T> @Nullable T
screenshot
(org.openqa.selenium.OutputType<T> outputType) Take the screenshot of current page and return it.static org.openqa.selenium.remote.SessionId
Get current browser session idstatic SessionStorage
Access browser's session storage.static void
sleep
(long milliseconds) Not recommended.static SelenideTargetLocator
switchTo()
Switch to window/tab/frame/parentFrame/innerFrame/alert.static @Nullable String
title()
static void
updateHash
(String hash) Update the hash of the window location.static void
using
(org.openqa.selenium.WebDriver webDriver, SelenideProxyServer proxy, Runnable lambda) static void
static SelenideWait
Wait()
Create an instance ofFluentWait
with Selenide timeout/polling.static Conditional<org.openqa.selenium.WebDriver>
static void
zoom
(double factor) Zoom current page (in or out).
-
Constructor Details
-
Selenide
public Selenide()
-
-
Method Details
-
open
The main starting point in your tests. Open a browser window with given URL.If browser window was already opened before, it will be reused.
Don't bother about closing the browser - it will be closed automatically when all your tests are done.
- Parameters:
relativeOrAbsoluteUrl
- If not starting with "http://" or "https://" or "file://", it's considered to be relative URL. In this case, it's prepended by baseUrl
-
open
Opens browser with given config. If there was already an opened browser with different config, it will be closed. -
open
- See Also:
-
open
The main starting point in your tests.Open a browser window with given URL and credentials for basic authentication
If browser window was already opened before, it will be reused.
Don't bother about closing the browser - it will be closed automatically when all your tests are done.
If not starting with "http://" or "https://" or "file://", it's considered to be relative URL.
In this case, it's prepended by baseUrl
- Parameters:
domain
- Name of domain to apply Basic Auth. 1. If empty, Basic Auth will be applied to all domains. 2. If non-empty, Basic Auth will be applied only to URLs containing this domain. 2.1. May contain multiple domain names (delimited by "," or "|").
-
open
public static void open(String relativeOrAbsoluteUrl, AuthenticationType authenticationType, Credentials credentials) The main starting point in your tests.Open browser and pass authentication using build-in proxy.
A common authenticationType is "Basic". See Web HTTP reference for other types.
This method can only work if -
Configuration.proxyEnabled == true
- See Also:
-
open
- See Also:
-
open
public static void open()Open an empty browser (without opening any pages). E.g. useful for starting mobile applications in Appium. -
open
Open an empty browser with given config. If there was already an opened browser with different config, it will be closed. -
webdriver
-
using
-
using
public static void using(org.openqa.selenium.WebDriver webDriver, SelenideProxyServer proxy, Runnable lambda) -
inNewBrowser
Open a new browser (with the same settings as the default browser), and run given code block in this browser.In the end, the browser will be closed.
-
inNewBrowser
Open a new browser (with given settings), and run given code block in this browser.In the end, the browser will be closed.
- Since:
- 7.6.0
-
updateHash
Update the hash of the window location. Useful to navigate in ajax apps without reloading the page, since open(url) makes a full page reload.- Parameters:
hash
- value for window.location.hash - Accept either "#hash" or "hash".
-
open
public static <PageObjectClass> PageObjectClass open(String relativeOrAbsoluteUrl, Class<PageObjectClass> pageObjectClassClass) Open a web page and create PageObject for it.- Returns:
- PageObject of given class
-
open
public static <PageObjectClass> PageObjectClass open(URL absoluteUrl, Class<PageObjectClass> pageObjectClassClass) Open a web page and create PageObject for it.- Returns:
- PageObject of given class
-
open
public static <PageObjectClass> PageObjectClass open(String relativeOrAbsoluteUrl, String domain, String login, String password, Class<PageObjectClass> pageObjectClassClass) Open a web page using Basic Auth credentials and create PageObject for it.- Returns:
- PageObject of given class
-
open
public static <PageObjectClass> PageObjectClass open(URL absoluteUrl, String domain, String login, String password, Class<PageObjectClass> pageObjectClassClass) Open a web page using Basic Auth credentials and create PageObject for it.- Returns:
- PageObject of given class
-
closeWindow
public static void closeWindow()Close the current window, quitting the browser if it's the last window currently open.- See Also:
-
WebDriver.close()
-
closeWebDriver
public static void closeWebDriver()Close the browser if it's open.
NB! Method quits this driver, closing every associated window.
- See Also:
-
WebDriver.quit()
-
refresh
public static void refresh()Reload current page -
back
public static void back()Navigate browser back to previous page -
forward
public static void forward()Navigate browser forward to next page -
title
- Returns:
- title of the page
-
sleep
public static void sleep(long milliseconds) Not recommended. Test should not sleep, but should wait for some condition instead.
Implementation detail: method
Thread.sleep(long)
is not guaranteed to sleep exactly given number of milliseconds, it can awake earlier. That's why we need to use a loop to guarantee the sleep duration.- Parameters:
milliseconds
- Time to sleep in milliseconds
-
screenshot
Take the screenshot of current page and save to file "fileName.png" (and optionally, "fileName.html")- File "fileName.png" is created always, even if
Configuration.screenshots == false
- File "fileName.html" is created only if
Configuration.savePageSource == true
- Parameters:
fileName
- Name of file (without extension) to save PNG (and HTML) to- Returns:
- URL of screenshot file
- File "fileName.png" is created always, even if
-
screenshot
@CanIgnoreReturnValue public static <T> @Nullable T screenshot(org.openqa.selenium.OutputType<T> outputType) Take the screenshot of current page and return it.- Parameters:
outputType
- type of the returned screenshot- Returns:
- The screenshot (as bytes, base64 or temporary file) or null if webdriver does not support taking screenshots.
-
$
Wrap standard Selenium WebElement into SelenideElement to use additional methods likeSelenideElement.should(WebElementCondition...)
,SelenideElement.selectOption(String, String...)
etc.- Parameters:
webElement
- standard Selenium WebElement- Returns:
- given WebElement wrapped into SelenideElement
-
$
Locates the first element matching given CSS selector (lazy evaluation)- Parameters:
cssSelector
- any CSS selector like "input[name='first_name']" or "#messages .new_message"- Returns:
- SelenideElement
- See Also:
-
$x
Locates the first element matching given XPATH expression (lazy evaluation)- Parameters:
xpathExpression
- any XPATH expression //*[@id='value'] //E[contains(@A, 'value')]- Returns:
- SelenideElement which locates elements via XPath
- See Also:
-
$
Locates the first element matching given CSS selector (lazy evaluation)- Parameters:
seleniumSelector
- any Selenium selector like By.id(), By.name() etc.- Returns:
- SelenideElement
- See Also:
-
$
- See Also:
-
$
Locates the Nth element matching given criteria (lazy evaluation)- Parameters:
cssSelector
- any CSS selector like "input[name='first_name']" or "#messages .new_message"index
- 0..N- Returns:
- SelenideElement
- See Also:
-
$$
Initialize collection with Elements -
$$
Locates all elements matching given CSS selector (lazy evaluation).Methods returns an ElementsCollection which is a list of WebElement objects that can be iterated, and at the same time is implementation of WebElement interface, meaning that you can call methods .sendKeys(), click() etc. on it.
- Parameters:
cssSelector
- any CSS selector like "input[name='first_name']" or "#messages .new_message"- Returns:
- empty list if element was no found
- See Also:
-
$$x
Locates all elements matching given XPATH expression (lazy evaluation)Methods returns an ElementsCollection which is a list of WebElement objects that can be iterated, and at the same time is implementation of WebElement interface, meaning that you can call methods .sendKeys(), click() etc. on it.
- Parameters:
xpathExpression
- any XPATH expression //*[@id='value'] //E[contains(@A, 'value')]- Returns:
- ElementsCollection which locates elements via XPath
- See Also:
-
$$
Locates all elements matching given CSS selector.Method returns an ElementsCollection which is a list of WebElement objects that can be iterated, and at the same time is implementation of WebElement interface, meaning that you can call methods .sendKeys(), click() etc. on it.
- Parameters:
seleniumSelector
- any Selenium selector like By.id(), By.name() etc.- Returns:
- empty list if element was no found
- See Also:
-
element
Wrap standard Selenium WebElement into SelenideElement to use additional methods likeSelenideElement.shouldHave(WebElementCondition...)
,SelenideElement.selectOption(String, String...)
etc.- Parameters:
webElement
- standard Selenium WebElement- Returns:
- given WebElement wrapped into SelenideElement
-
element
Locates the first element matching given CSS selector- Parameters:
cssSelector
- any CSS selector like "input[name='first_name']" or "#messages .new_message"- Returns:
- SelenideElement
- See Also:
-
element
Locates the first element matching given CSS selector- Parameters:
seleniumSelector
- any Selenium selector likeBy.id(String)
,By.name(String)
etc.- Returns:
- SelenideElement
- See Also:
-
element
Locates the Nth element matching given criteria- Parameters:
seleniumSelector
- any Selenium selector likeBy.id(String)
,By.name(String)
etc.index
- 0..N- Returns:
- SelenideElement
- See Also:
-
element
Locates the Nth element matching given criteria- Parameters:
cssSelector
- any CSS selector like"input[name='first_name']"
or"#messages .new_message"
index
- 0..N- Returns:
- SelenideElement
- See Also:
-
elements
public static ElementsCollection elements(Collection<? extends org.openqa.selenium.WebElement> elements) Wrap standard Selenium WebElement collection into SelenideElement collection to use additional methods likeSelenideElement.shouldHave(WebElementCondition...)
etc.- Parameters:
elements
- standard Selenium WebElement collection- Returns:
- given WebElement collection wrapped into SelenideElement collection
-
elements
Locates all elements matching given CSS selector.Method returns an ElementsCollection which is a list of WebElement objects that can be iterated, and at the same time is implementation of WebElement interface, meaning that you can call methods
WebElement.sendKeys(CharSequence...)
,WebElement.click()
etc. on it.- Parameters:
cssSelector
- any CSS selector like "input[name='first_name']" or "#messages .new_message"- Returns:
- empty list if element was no found
- See Also:
-
elements
Locates all elements matching given CSS selector.Method returns an ElementsCollection which is a list of WebElement objects that can be iterated, and at the same time is implementation of WebElement interface, meaning that you can call methods
WebElement.sendKeys(CharSequence...)
,WebElement.click()
etc. on it.- Parameters:
seleniumSelector
- any Selenium selector like By.id(), By.name() etc.- Returns:
- empty list if element was no found
- See Also:
-
executeJavaScript
@CanIgnoreReturnValue public static <T> @Nullable T executeJavaScript(String jsCode, Object... arguments) - See Also:
-
JavascriptExecutor.executeScript(String, java.lang.Object...)
-
executeAsyncJavaScript
@CanIgnoreReturnValue public static <T> @Nullable T executeAsyncJavaScript(String jsCode, Object... arguments) - See Also:
-
JavascriptExecutor.executeAsyncScript(String, java.lang.Object...)
-
getSelectedRadio
Returns selected element in radio group- Returns:
- null if nothing selected
-
confirm
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'alert' or 'confirm').- Returns:
- actual dialog text
-
confirm
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'alert' or 'confirm').- Parameters:
expectedDialogText
- if not null, check that confirmation dialog displays this message (case-sensitive)- Returns:
- actual dialog text
- Throws:
DialogTextMismatch
- if confirmation message differs from expected message
-
confirm
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'alert' or 'confirm').- Parameters:
options
- parameters: timeout, expected texts etc.- Returns:
- actual dialog text
- Throws:
DialogTextMismatch
- if confirmation message differs from expected message
-
prompt
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'prompt').- Returns:
- actual dialog text
-
prompt
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'prompt').- Parameters:
inputText
- if not null, sets value in prompt dialog input- Returns:
- actual dialog text
-
prompt
@CanIgnoreReturnValue public static String prompt(@Nullable String expectedDialogText, @Nullable String inputText) Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'prompt').- Parameters:
expectedDialogText
- if not null, check that confirmation dialog displays this message (case-sensitive)inputText
- if not null, sets value in prompt dialog input- Returns:
- actual dialog text
- Throws:
DialogTextMismatch
- if confirmation message differs from expected message
-
prompt
Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'prompt').- Parameters:
options
- parameters: timeout, expected texts etc.inputText
- if not null, sets value in prompt dialog input- Returns:
- actual dialog text
- Throws:
DialogTextMismatch
- if confirmation message differs from expected message
-
dismiss
Dismiss (click "No" or "Cancel") in the confirmation dialog (javascript 'alert' or 'confirm').- Returns:
- actual dialog text
-
dismiss
Dismiss (click "No" or "Cancel") in the confirmation dialog (javascript 'alert' or 'confirm').- Parameters:
expectedDialogText
- if not null, check that confirmation dialog displays this message (case-sensitive)- Returns:
- actual dialog text
- Throws:
DialogTextMismatch
- if confirmation message differs from expected message
-
dismiss
Dismiss (click "No" or "Cancel") in the confirmation dialog (javascript 'alert' or 'confirm').- Parameters:
options
- if not null, check that confirmation dialog displays this message (case-sensitive)- Returns:
- actual dialog text
- Throws:
DialogTextMismatch
- if confirmation message differs from expected message
-
switchTo
Switch to window/tab/frame/parentFrame/innerFrame/alert. Allows switching to window by title, index, name etc.Similar to
WebDriver.switchTo()
, but all methods wait until frame/window/alert appears if it's not visible yet (like other Selenide methods).- Returns:
- SelenideTargetLocator
-
getFocusedElement
- Returns:
- the element that currently is focused, or null if none of elements if focused
-
getSelectedText
Returns selected text or empty string if no text is selected.- Returns:
- selected text
-
copy
Copy selected text or empty string if no text is selected to clipboard.- Returns:
- the copied text
- See Also:
-
page
Create a Page Object instance -
page
Create a Page Object instance- Parameters:
reified
- Don't pass any values here. It's Java Magic :)
-
page
public static <PageObjectClass,T extends PageObjectClass> PageObjectClass page(T pageObject) Initialize a given Page Object instance -
Wait
Create an instance ofFluentWait
with Selenide timeout/polling.Sample usage:
Wait().until(invisibilityOfElementLocated(By.id("magic-id")));
- Returns:
- instance of org.openqa.selenium.support.ui.FluentWait
-
actions
public static org.openqa.selenium.interactions.Actions actions()With this method you can use Selenium Actions like described in the AdvancedUserInteractions page.actions() .sendKeys($(By.name("rememberMe")), "John") .click($(#rememberMe")) .click($(byText("Login"))) .build() .perform();
-
zoom
public static void zoom(double factor) Zoom current page (in or out).- Parameters:
factor
- e.g. 1.1 or 2.0 or 0.5
-
getWebDriverLogs
Same asgetWebDriverLogs(String, Level)
-
getWebDriverLogs
Getting and filtering of the WebDriver logs for specified LogType by specified logging level
For example to get WebDriver Browser's console output (including JS info, warnings, errors, etc. messages) you can use:
for(String logEntry : getWebDriverLogs(LogType.BROWSER, Level.ALL)) { Reporter.log(logEntry + "<br>"); }
Be aware that currently "manage().logs()" is in the Beta stage, but it is beta-then-nothing :)- Parameters:
logType
- WebDriver supported log typeslogLevel
- logging level that will be used to control logging output- Returns:
- list of log entries
- See Also:
-
LogType
Level
-
clearBrowserCookies
public static void clearBrowserCookies()Clear browser cookies.It can be useful e.g. if you are trying to avoid restarting browser between tests
-
clearBrowserLocalStorage
public static void clearBrowserLocalStorage()Clear browser local storage.In case if you need to be sure that browser's localStorage is empty
-
getUserAgent
Get current user agent from browser session- Returns:
- browser user agent
-
atBottom
public static boolean atBottom()Return true if bottom of the page is reachedUseful if you need to scroll down by x pixels unknown number of times.
-
download
NB! URL must be properly encoded. E.g. instead of "/files/ж.txt", it should be "/files/%D0%B6.txt" -
download
- See Also:
-
download
- See Also:
-
download
Download file using a direct link. This method download file like it would be done in currently opened browser: it adds all cookies and "User-Agent" header to the downloading request.Download fails if specified timeout is exceeded
- Parameters:
url
- either relative or absolute url NB! URL must be properly encoded. E.g. instead of "/files/ж.txt", it should be "/files/%D0%B6.txt"timeoutMs
- specific timeout in ms- Returns:
- downloaded File in folder `Configuration.reportsFolder`
- Throws:
FileNotDownloadedError
- if failed to download fileURISyntaxException
- if given url has invalid syntax
-
localStorage
Access browser's local storage. Allows setting, getting, removing items as well as getting the size and clear the storage.- Returns:
- LocalStorage
-
sessionStorage
Access browser's session storage. Allows setting, getting, removing items as well as getting the size, check for contains item and clear the storage.- Returns:
- sessionStorage
-
clipboard
Provide access to system clipboard, allows get and set String content. Default implementation acts viaToolkit
and supports only local runs.Remote runs support can be implemented via plugins. Plugin for Selenoid supports clipboard since v1.1.0.
- Returns:
- Clipboard
- See Also:
-
- selenide-selenoid
Pay attention that Clipboard is shared resource for instance where tests runs and keep in mind while developing test suite with multiple tests for clipboard.
- selenide-selenoid
-
sessionId
public static org.openqa.selenium.remote.SessionId sessionId()Get current browser session id- Returns:
- SessionId
-