Class Selenide

java.lang.Object
com.codeborne.selenide.Selenide

public class Selenide extends Object
The main starting point of Selenide.

You start with methods open(String) for opening the tested application page and $(String) for searching web elements.

  • Constructor Details

    • Selenide

      public Selenide()
  • Method Details

    • open

      public static void open(String relativeOrAbsoluteUrl)
      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

      public static void open(String relativeOrAbsoluteUrl, Config config)
      Opens browser with given config. If there was already an opened browser with different config, it will be closed.
    • open

      public static void open(URL absoluteUrl)
      See Also:
    • open

      public static void open(String relativeOrAbsoluteUrl, String domain, String login, String password)
      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

      public static void open(URL absoluteUrl, String domain, String login, String password)
      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

      public static void open(Config config)
      Open an empty browser with given config. If there was already an opened browser with different config, it will be closed.
    • webdriver

      public static Conditional<org.openqa.selenium.WebDriver> webdriver()
    • using

      public static void using(org.openqa.selenium.WebDriver webDriver, Runnable lambda)
    • using

      public static void using(org.openqa.selenium.WebDriver webDriver, SelenideProxyServer proxy, Runnable lambda)
    • inNewBrowser

      public 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.

      In the end, the browser will be closed.

    • inNewBrowser

      public static void inNewBrowser(Config config, Runnable lambda)
      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

      public static void updateHash(String hash)
      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

      public static @Nullable String 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

      @CanIgnoreReturnValue public static @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 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
    • 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.
    • $

      public static SelenideElement $(org.openqa.selenium.WebElement webElement)
      Wrap standard Selenium WebElement into SelenideElement to use additional methods like SelenideElement.should(WebElementCondition...), SelenideElement.selectOption(String, String...) etc.
      Parameters:
      webElement - standard Selenium WebElement
      Returns:
      given WebElement wrapped into SelenideElement
    • $

      public static SelenideElement $(String cssSelector)
      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

      public static SelenideElement $x(String xpathExpression)
      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:
    • $

      public static SelenideElement $(org.openqa.selenium.By seleniumSelector)
      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:
    • $

      public static SelenideElement $(org.openqa.selenium.By seleniumSelector, int index)
      See Also:
    • $

      public static SelenideElement $(String cssSelector, int index)
      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:
    • $$

      public static ElementsCollection $$(Collection<? extends org.openqa.selenium.WebElement> elements)
      Initialize collection with Elements
    • $$

      public static ElementsCollection $$(String cssSelector)
      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

      public static ElementsCollection $$x(String xpathExpression)
      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:
    • $$

      public static ElementsCollection $$(org.openqa.selenium.By seleniumSelector)
      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

      public static SelenideElement element(org.openqa.selenium.WebElement webElement)
      Wrap standard Selenium WebElement into SelenideElement to use additional methods like SelenideElement.shouldHave(WebElementCondition...), SelenideElement.selectOption(String, String...) etc.
      Parameters:
      webElement - standard Selenium WebElement
      Returns:
      given WebElement wrapped into SelenideElement
    • element

      public static SelenideElement element(String cssSelector)
      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

      public static SelenideElement element(org.openqa.selenium.By seleniumSelector)
      Locates the first element matching given CSS selector
      Parameters:
      seleniumSelector - any Selenium selector like By.id(String), By.name(String) etc.
      Returns:
      SelenideElement
      See Also:
    • element

      public static SelenideElement element(org.openqa.selenium.By seleniumSelector, int index)
      Locates the Nth element matching given criteria
      Parameters:
      seleniumSelector - any Selenium selector like By.id(String), By.name(String) etc.
      index - 0..N
      Returns:
      SelenideElement
      See Also:
    • element

      public static SelenideElement element(String cssSelector, int index)
      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 like SelenideElement.shouldHave(WebElementCondition...) etc.
      Parameters:
      elements - standard Selenium WebElement collection
      Returns:
      given WebElement collection wrapped into SelenideElement collection
    • elements

      public static ElementsCollection elements(String cssSelector)
      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

      public static ElementsCollection elements(org.openqa.selenium.By seleniumSelector)
      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

      public static @Nullable SelenideElement getSelectedRadio(org.openqa.selenium.By radioField)
      Returns selected element in radio group
      Returns:
      null if nothing selected
    • confirm

      @CanIgnoreReturnValue public static String confirm()
      Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'alert' or 'confirm').
      Returns:
      actual dialog text
    • confirm

      @CanIgnoreReturnValue public static String confirm(@Nullable String expectedDialogText)
      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

      @CanIgnoreReturnValue public static String confirm(ModalOptions options)
      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

      @CanIgnoreReturnValue public static String prompt()
      Accept (Click "Yes" or "Ok") in the confirmation dialog (javascript 'prompt').
      Returns:
      actual dialog text
    • prompt

      @CanIgnoreReturnValue public static String prompt(@Nullable String inputText)
      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

      @CanIgnoreReturnValue public static String prompt(ModalOptions options, @Nullable String inputText)
      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

      @CanIgnoreReturnValue public static String dismiss()
      Dismiss (click "No" or "Cancel") in the confirmation dialog (javascript 'alert' or 'confirm').
      Returns:
      actual dialog text
    • dismiss

      @CanIgnoreReturnValue public static String dismiss(@Nullable String expectedDialogText)
      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

      @CanIgnoreReturnValue public static String dismiss(ModalOptions options)
      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

      public static SelenideTargetLocator 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

      public static SelenideElement getFocusedElement()
      Returns:
      the element that currently is focused, or null if none of elements if focused
    • getSelectedText

      public static String getSelectedText()
      Returns selected text or empty string if no text is selected.
      Returns:
      selected text
    • copy

      @CanIgnoreReturnValue public static String copy()
      Copy selected text or empty string if no text is selected to clipboard.
      Returns:
      the copied text
      See Also:
    • page

      public static <PageObjectClass> PageObjectClass page(Class<PageObjectClass> pageObjectClass)
      Create a Page Object instance
    • page

      @SafeVarargs public static <PageObjectClass> PageObjectClass page(PageObjectClass... reified)
      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

      public static SelenideWait Wait()
      Create an instance of FluentWait 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

      public static List<String> getWebDriverLogs(String logType)
    • getWebDriverLogs

      public static List<String> 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. 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 types
      logLevel - logging level that will be used to control logging output
      Returns:
      list of log entries
      See Also:
    • 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

      public static String 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 reached

      Useful if you need to scroll down by x pixels unknown number of times.

    • download

      public static File download(String url) throws URISyntaxException
      NB! URL must be properly encoded. E.g. instead of "/files/ж.txt", it should be "/files/%D0%B6.txt"
      Throws:
      URISyntaxException
      See Also:
    • download

      public static File download(URI url)
      See Also:
    • download

      public static File download(URI url, long timeoutMs)
      See Also:
    • download

      public static File download(String url, long timeoutMs) throws URISyntaxException
      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 file
      URISyntaxException - if given url has invalid syntax
    • localStorage

      public static LocalStorage localStorage()
      Access browser's local storage. Allows setting, getting, removing items as well as getting the size and clear the storage.
      Returns:
      LocalStorage
    • sessionStorage

      public static SessionStorage 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

      public static Clipboard clipboard()
      Provide access to system clipboard, allows get and set String content. Default implementation acts via Toolkit 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.

    • sessionId

      public static org.openqa.selenium.remote.SessionId sessionId()
      Get current browser session id
      Returns:
      SessionId