Class SeleniumUtils

java.lang.Object
io.github.the_sdet.web.Utils
io.github.the_sdet.web.SeleniumUtils
Direct Known Subclasses:
AppiumUtils

public class SeleniumUtils extends Utils
Utility class for Selenium-based web automation tasks. This class provides various methods to interact with web elements, take screenshots, handle timeouts, and perform other common actions using the Selenium WebDriver API.
Author:
Pabitra Swain ([email protected])
  • Constructor Details

    • SeleniumUtils

      public SeleniumUtils(org.openqa.selenium.WebDriver driver)
      Constructor to initialize SeleniumUtils.
      Parameters:
      driver - The WebDriver instance to use.
  • Method Details

    • openPage

      public void openPage(String url)
      Opens the specified URL in the browser.
      Parameters:
      url - The URL to open.
    • maximizeScreen

      public void maximizeScreen()
      Maximizes the browser window.
    • setScreenSize

      public void setScreenSize(int width, int height)
      Sets the screen size to the specified width and height.
      Parameters:
      width - The width of the screen.
      height - The height of the screen.
    • click

      public void click(String xpath)
      Clicks on the element identified by the given XPath.
      Parameters:
      xpath - The XPath of the element to click.
    • fillText

      public void fillText(String xpath, String value)
      Enters value on to element identified by the specified XPath
      Parameters:
      xpath - The XPath of the element
      value - Value to send
    • getElement

      public org.openqa.selenium.WebElement getElement(String xpath)
      Retrieves the WebElement by Xpath.
      Parameters:
      xpath - The XPath of the element to retrieve.
      Returns:
      The WebElement found.
    • getElement

      public org.openqa.selenium.WebElement getElement(org.openqa.selenium.By element)
      Retrieves the WebElement
      Parameters:
      element - By object of element
      Returns:
      The WebElement found.
    • getElementByText

      public org.openqa.selenium.WebElement getElementByText(String xpath, String textContent)
      Retrieves the WebElement by text content from a list of matching WebElements
      Parameters:
      xpath - Xpath of element
      textContent - text value to look for
      Returns:
      The WebElement found.
    • getElementByText

      public org.openqa.selenium.WebElement getElementByText(org.openqa.selenium.By element, String textContent)
      Retrieves the WebElement by text content from a list of matching WebElements
      Parameters:
      element - The locator of the element
      textContent - text value to look for
      Returns:
      The WebElement found.
    • getElements

      public List<org.openqa.selenium.WebElement> getElements(String xpath)
      Retrieves the WebElements by Xpath.
      Parameters:
      xpath - The XPath of the element
      Returns:
      The WebElements found as a list
    • getElements

      public List<org.openqa.selenium.WebElement> getElements(org.openqa.selenium.By element)
      Retrieves the WebElements by Xpath.
      Parameters:
      element - The locator of the element
      Returns:
      The WebElements found as a list
    • getElements

      public List<org.openqa.selenium.WebElement> getElements(String xpath, boolean waitForFirstElement)
      Retrieves the WebElements by Xpath.
      Parameters:
      xpath - The XPath of the element
      waitForFirstElement - true if wait for first element
      Returns:
      The WebElements found as a list
    • getElements

      public List<org.openqa.selenium.WebElement> getElements(org.openqa.selenium.By element, boolean waitForFirstElement)
      Retrieves the WebElements by Xpath.
      Parameters:
      element - The locator of the element
      waitForFirstElement - true if wait for first element
      Returns:
      The WebElements found as a list
    • getElements

      public List<org.openqa.selenium.WebElement> getElements(String xpath, boolean waitForFirstElement, Duration duration)
      Retrieves the WebElements by Xpath.
      Parameters:
      xpath - The XPath of the element
      waitForFirstElement - true if wait for first element
      duration - maximum wait
      Returns:
      The WebElements found as a list
    • getElements

      public List<org.openqa.selenium.WebElement> getElements(org.openqa.selenium.By element, boolean waitForFirstElement, Duration duration)
      Retrieves the WebElements by Xpath.
      Parameters:
      element - The locator of the element
      waitForFirstElement - true if wait for first element
      duration - maximum wait
      Returns:
      The WebElements found as a list
    • getElementTextContent

      public String getElementTextContent(String xpath)
      Retrieves the text content of WebElement by Xpath.
      Parameters:
      xpath - The XPath of the element
      Returns:
      The text content of WebElement
    • getElementTextContent

      public String getElementTextContent(org.openqa.selenium.By element)
      Retrieves the text content of WebElement by Xpath.
      Parameters:
      element - The locator of the element
      Returns:
      The text content of WebElement
    • getElementsTextContent

      public List<String> getElementsTextContent(String xpath)
      Retrieves the text content of WebElements by Xpath.
      Parameters:
      xpath - The XPath of the element
      Returns:
      The text content of WebElements found as a list
    • getElementsTextContent

      public List<String> getElementsTextContent(org.openqa.selenium.By element)
      Retrieves the text content of WebElements by Xpath.
      Parameters:
      element - The locator of the element
      Returns:
      The text content of WebElements found as a list
    • getElementsTextContent

      public List<String> getElementsTextContent(String xpath, boolean waitForFirstElement)
      Retrieves the text content of WebElements by Xpath.
      Parameters:
      xpath - The XPath of the element
      waitForFirstElement - true if wait for first element
      Returns:
      The text content of WebElements found as a list
    • getElementsTextContent

      public List<String> getElementsTextContent(org.openqa.selenium.By element, boolean waitForFirstElement)
      Retrieves the text content of WebElements by Xpath.
      Parameters:
      element - The locator of the element
      waitForFirstElement - true if wait for first element
      Returns:
      The text content of WebElements found as a list
    • getElementsTextContent

      public List<String> getElementsTextContent(String xpath, boolean waitForFirstElement, Duration duration)
      Retrieves the text content of WebElements by Xpath.
      Parameters:
      xpath - The XPath of the element
      waitForFirstElement - true if wait for first element
      duration - maximum wait
      Returns:
      The text content of WebElements found as a list
    • getElementsTextContent

      public List<String> getElementsTextContent(org.openqa.selenium.By element, boolean waitForFirstElement, Duration duration)
      Retrieves the text content of WebElements by Xpath.
      Parameters:
      element - The locator of the element
      waitForFirstElement - true if wait for first element
      duration - maximum wait
      Returns:
      The text content of WebElements found as a list
    • click

      public void click(org.openqa.selenium.WebElement element)
      Clicks on the specified WebElement.
      Parameters:
      element - The WebElement to click.
    • click

      public void click(org.openqa.selenium.By element)
      Clicks on the element identified by the given By selector.
      Parameters:
      element - The By selector of the element to click.
    • javaScriptClick

      public void javaScriptClick(String xpath)
      Clicks on the element identified by the given XPath using JavaScript.
      Parameters:
      xpath - The XPath of the element to click.
    • javaScriptClick

      public void javaScriptClick(org.openqa.selenium.By element)
      Clicks on the element identified by the given XPath using JavaScript.
      Parameters:
      element - The locator of the element to click.
    • javaScriptFillText

      public void javaScriptFillText(String xpath, String value)
      Enters value on to element identified by the specified XPath using JavaScript.
      Parameters:
      xpath - The XPath of the element
      value - Value to send
    • javaScriptFillText

      public void javaScriptFillText(org.openqa.selenium.By element, String value)
      Enters value on to element identified by the specified XPath using JavaScript.
      Parameters:
      element - The locator of the element
      value - Value to send
    • enterText

      public void enterText(String xpath, String text)
      Enters the specified text into the element identified by the given XPath.
      Parameters:
      xpath - The XPath of the element.
      text - The text to enter.
    • enterText

      public void enterText(org.openqa.selenium.By element, String text)
      Enters the specified text into the element identified by the given XPath.
      Parameters:
      element - The locator of the element.
      text - The text to enter.
    • getScreenshot

      public File getScreenshot()
      Retrieves a screenshot of the current page.
      Returns:
      A File object representing the screenshot.
    • takeScreenshot

      public void takeScreenshot(String filepath)
      Takes a screenshot of the current page and saves it to the specified filepath.
      Parameters:
      filepath - The filepath where the screenshot should be saved.
    • getElementScreenshot

      public File getElementScreenshot(String xpath)
      Retrieves a screenshot of the specified element.
      Parameters:
      xpath - The XPath of the element.
      Returns:
      A File object representing the screenshot.
    • takeElementScreenshot

      public void takeElementScreenshot(String xpath, String filepath)
      Takes a screenshot of the specified element and saves it to the specified filepath.
      Parameters:
      xpath - The XPath of the element.
      filepath - The filepath where the screenshot should be saved.
    • getElementScreenshot

      public File getElementScreenshot(org.openqa.selenium.By element)
      Retrieves a screenshot of the specified element.
      Parameters:
      element - The locator of the element.
      Returns:
      A File object representing the screenshot.
    • takeElementScreenshot

      public void takeElementScreenshot(org.openqa.selenium.By element, String filepath)
      Takes a screenshot of the specified element and saves it to the specified filepath.
      Parameters:
      element - The locator of the element.
      filepath - The filepath where the screenshot should be saved.
    • takeFullPageScreenshot

      public File takeFullPageScreenshot()
      Takes a full-page screenshot of the current page.
      Returns:
      A File object representing the full-page screenshot.
    • takeFullPageScreenshot

      public void takeFullPageScreenshot(String filepath)
      Takes a full-page screenshot of the current page and saves it to the specified filepath.
      Parameters:
      filepath - The filepath where the screenshot should be saved.
    • getScreenshotAsBase64

      public String getScreenshotAsBase64()
      Retrieves the screenshot of the current page as a Base64 encoded string.
      Returns:
      The Base64 encoded string representation of the screenshot.
    • getElementScreenshotAsBase64

      public String getElementScreenshotAsBase64(String xpath)
      Retrieves the screenshot of the element identified by the given XPath as a Base64 encoded string.
      Parameters:
      xpath - The XPath of the element.
      Returns:
      The Base64 encoded string representation of the screenshot.
    • getElementScreenshotAsBase64

      public String getElementScreenshotAsBase64(org.openqa.selenium.By element)
      Retrieves the screenshot of the element identified by the given XPath as a Base64 encoded string.
      Parameters:
      element - The locator of the element.
      Returns:
      The Base64 encoded string representation of the screenshot.
    • getFullPageScreenshotAsBase64

      public String getFullPageScreenshotAsBase64()
      Retrieves the full-page screenshot as a Base64 encoded string.
      Returns:
      The Base64 encoded string representation of the full-page screenshot.
    • getScreenshotAsByte

      public byte[] getScreenshotAsByte()
      Retrieves the screenshot of the current page as a byte array.
      Returns:
      The byte array representing the screenshot.
    • getElementScreenshotAsByte

      public byte[] getElementScreenshotAsByte(String xpath)
      Retrieves the screenshot of the element identified by the given XPath as a byte array.
      Parameters:
      xpath - The XPath of the element.
      Returns:
      The byte array representing the screenshot.
    • getElementScreenshotAsByte

      public byte[] getElementScreenshotAsByte(org.openqa.selenium.By element)
      Retrieves the screenshot of the element identified by the given XPath as a byte array.
      Parameters:
      element - The locator of the element.
      Returns:
      The byte array representing the screenshot.
    • getFullPageScreenshotAsByte

      public byte[] getFullPageScreenshotAsByte()
      Retrieves the full-page screenshot as a byte array.
      Returns:
      The byte array representing the full-page screenshot.
    • clearAndEnterText

      public void clearAndEnterText(String xpath, String text)
      Clears the existing text and enters the specified text into the element identified by the given XPath.
      Parameters:
      xpath - The XPath of the element.
      text - The text to enter.
    • clearAndEnterText

      public void clearAndEnterText(org.openqa.selenium.By element, String text)
      Clears the existing text and enters the specified text into the element identified by the given XPath.
      Parameters:
      element - The locator of the element.
      text - The text to enter.
    • waitAndClick

      public void waitAndClick(String xpath, int seconds)
      Waits for the specified element to be clickable and then clicks on it.
      Parameters:
      xpath - The XPath of the element to click.
      seconds - The maximum time to wait for the element to be clickable, in seconds.
    • waitAndClick

      public void waitAndClick(org.openqa.selenium.By element, int seconds)
      Waits for the specified element to be clickable and then clicks on it.
      Parameters:
      element - The locator of the element to click.
      seconds - The maximum time to wait for the element to be clickable, in seconds.
    • waitFor

      public void waitFor(Duration duration)
      Pauses the execution for the specified duration.
      Parameters:
      duration - wait duration.
    • setDefaultTimeOut

      public void setDefaultTimeOut(int seconds)
      Sets the default timeout for implicit waits.
      Parameters:
      seconds - The default timeout value, in seconds.
    • focusOnElement

      public void focusOnElement(String xpath)
      Focuses on the element identified by the given XPath.
      Parameters:
      xpath - The XPath of the element to focus on.
    • focusOnElement

      public void focusOnElement(org.openqa.selenium.By element)
      Focuses on the element identified by the given XPath.
      Parameters:
      element - The locator of the element to focus on.
    • hoverOverElement

      public void hoverOverElement(org.openqa.selenium.By element)
      Hovers over the element identified by the given XPath.
      Parameters:
      element - The locator of the element to hover over.
    • hoverOverElement

      public void hoverOverElement(String xpath)
      Hovers over the element identified by the given XPath.
      Parameters:
      xpath - The XPath of the element to hover over.
    • pressTab

      public void pressTab()
      Simulates pressing the Tab key.
    • pressEnter

      public void pressEnter()
      Simulates pressing the Enter key.
    • pressTabOnElement

      public void pressTabOnElement(String xpath)
      Simulates pressing the Tab key on the element identified by the given XPath.
      Parameters:
      xpath - The XPath of the element to press Tab on.
    • pressTabOnElement

      public void pressTabOnElement(org.openqa.selenium.By element)
      Simulates pressing the Tab key on the element identified by the given XPath.
      Parameters:
      element - The locator of the element to press Tab on.
    • pressEnterOnElement

      public void pressEnterOnElement(org.openqa.selenium.By element)
      Simulates pressing the Enter key on the element identified by the given XPath.
      Parameters:
      element - The locator of the element to press Enter on.
    • pressEnterOnElement

      public void pressEnterOnElement(String xpath)
      Simulates pressing the Enter key on the element identified by the given XPath.
      Parameters:
      xpath - The XPath of the element to press Enter on.
    • scrollElementIntoToView

      public void scrollElementIntoToView(String xpath)
      Scrolls the element identified by the given XPath into view.
      Parameters:
      xpath - The XPath of the element to scroll into view.
    • scrollElementIntoToView

      public void scrollElementIntoToView(org.openqa.selenium.By element)
      Scrolls the element identified by the given XPath into view.
      Parameters:
      element - The locator of the element to scroll into view.
    • scrollByPercent

      public void scrollByPercent(double percentage)
      Scrolls the page by the specified percentage.
      Parameters:
      percentage - The percentage by which to scroll the page.
    • getAttributeValue

      public String getAttributeValue(String xpath, String attributeName)
      Retrieves the value of the specified attribute of the element identified by the given XPath.
      Parameters:
      xpath - The XPath of the element.
      attributeName - The name of the attribute.
      Returns:
      The value of the attribute.
    • getAttributeValue

      public String getAttributeValue(org.openqa.selenium.By element, String attributeName)
      Retrieves the value of the specified attribute of the element identified by the given XPath.
      Parameters:
      element - The locator of the element.
      attributeName - The name of the attribute.
      Returns:
      The value of the attribute.
    • findElementByCustomizeXpath

      public org.openqa.selenium.WebElement findElementByCustomizeXpath(String rawXpath, String value)
      Finds the element using the customized XPath with one parameter.
      Parameters:
      rawXpath - The raw XPath pattern.
      value - The value to be replaced in the XPath.
      Returns:
      The WebElement found.
    • findElementByCustomizeXpath

      public org.openqa.selenium.WebElement findElementByCustomizeXpath(String rawXpath, String value1, String value2)
      Finds the element using the customized XPath with two parameters.
      Parameters:
      rawXpath - The raw XPath pattern.
      value1 - The first value to be replaced in the XPath.
      value2 - The second value to be replaced in the XPath.
      Returns:
      The WebElement found.
    • findElementByCustomizeXpath

      public org.openqa.selenium.WebElement findElementByCustomizeXpath(String rawXpath, String value1, String value2, String value3)
      Finds the element using the customized XPath with three parameters.
      Parameters:
      rawXpath - The raw XPath pattern.
      value1 - The first value to be replaced in the XPath.
      value2 - The second value to be replaced in the XPath.
      value3 - The third value to be replaced in the XPath.
      Returns:
      The WebElement found.
    • isVisible

      public boolean isVisible(String xpath)
      Checks if the element identified by XPath is visible.
      Parameters:
      xpath - XPath identifying the element
      Returns:
      true if the element is visible, false otherwise
    • isVisible

      public boolean isVisible(org.openqa.selenium.By locator)
      Checks if the element identified by the given locator is visible.
      Parameters:
      locator - locator for identifying the element
      Returns:
      true if the element is visible, false otherwise
    • waitAndCheckIsVisible

      public boolean waitAndCheckIsVisible(String xpath, Duration duration)
      Waits for the element identified by XPath to be visible.
      Parameters:
      xpath - XPath identifying the element
      duration - maximum duration to wait
      Returns:
      true if the element becomes visible within the duration, false otherwise
    • waitAndCheckIsVisible

      public boolean waitAndCheckIsVisible(org.openqa.selenium.By element, Duration duration)
      Waits for the element identified by XPath to be visible.
      Parameters:
      element - locator for identifying the element
      duration - maximum duration to wait
      Returns:
      true if the element becomes visible within the duration, false otherwise
    • waitAndCheckIsClickable

      public boolean waitAndCheckIsClickable(String xpath, Duration duration)
      Waits for the element identified by XPath to be clickable.
      Parameters:
      xpath - XPath identifying the element
      duration - maximum duration to wait
      Returns:
      true if the element becomes clickable within the duration, false otherwise
    • waitAndCheckIsClickable

      public boolean waitAndCheckIsClickable(org.openqa.selenium.By element, Duration duration)
      Waits for the element identified by XPath to be clickable.
      Parameters:
      element - locator for identifying the element
      duration - maximum duration to wait
      Returns:
      true if the element becomes clickable within the duration, false otherwise
    • waitAndCheckIsInVisible

      public boolean waitAndCheckIsInVisible(String xpath, Duration duration)
      Waits for the element identified by XPath to become invisible.
      Parameters:
      xpath - XPath identifying the element
      duration - maximum duration to wait
      Returns:
      true if the element becomes invisible within the duration, false otherwise
    • waitAndCheckIsInVisible

      public boolean waitAndCheckIsInVisible(org.openqa.selenium.By element, Duration duration)
      Waits for the element identified by the given locator to become invisible.
      Parameters:
      element - locator for identifying the element
      duration - maximum duration to wait
      Returns:
      true if the element becomes invisible within the duration, false otherwise
    • waitAndFindElement

      public org.openqa.selenium.WebElement waitAndFindElement(String xpath, Duration duration)
      Waits for the element identified by XPath to be present within a specified duration.
      Parameters:
      xpath - XPath identifying the element
      duration - maximum duration to wait
      Returns:
      WebElement representing the located element
    • waitAndFindElement

      public org.openqa.selenium.WebElement waitAndFindElement(org.openqa.selenium.By locator, Duration duration)
      Waits for the element identified by the given locator to be present within a specified duration.
      Parameters:
      locator - locator for identifying the element
      duration - maximum duration to wait
      Returns:
      WebElement representing the located element
    • waitAndFindElement

      public org.openqa.selenium.WebElement waitAndFindElement(String xpath)
      Waits for the element identified by XPath to be present within a default duration of 5 seconds.
      Parameters:
      xpath - XPath identifying the element
      Returns:
      WebElement representing the located element
    • waitAndFindElement

      public org.openqa.selenium.WebElement waitAndFindElement(org.openqa.selenium.By locator)
      Waits for the element identified by the given locator to be present within a default duration of 5 seconds.
      Parameters:
      locator - locator for identifying the element
      Returns:
      WebElement representing the located element
    • waitAndCheckElementHasText

      public boolean waitAndCheckElementHasText(org.openqa.selenium.By element, Duration timeout, String expectedText)
      Waits for a specific seconds for the element to have text.
      Parameters:
      element - - By element
      timeout - - Duration timeout
      expectedText - - String expected text
      Returns:
      boolean - true if element has expected text, false otherwise
    • waitAndCheckElementHasText

      public boolean waitAndCheckElementHasText(String xpath, Duration timeout, String expectedText)
      Waits for a specific seconds for the element to have text.
      Parameters:
      xpath - - Xpath of the Element
      timeout - - Duration timeout
      expectedText - - String expected text
      Returns:
      boolean - true if element has expected text, false otherwise
    • waitAndCheckUrlContains

      public boolean waitAndCheckUrlContains(String expectedValue, Duration timeout)
      Waits for a specific seconds for the URL to contain the expected value.
      Parameters:
      expectedValue - - String expected URL value
      timeout - - Duration timeout
      Returns:
      boolean - true if URL contains the expected value, false otherwise
    • waitAndCheckIsAlertPresent

      public boolean waitAndCheckIsAlertPresent(Duration timeout)
      Waits for a specific seconds for an alert to be present.
      Parameters:
      timeout - - Duration timeout
      Returns:
      boolean - true if alert is present, false otherwise
    • waitAndCheckIsElementSelected

      public boolean waitAndCheckIsElementSelected(org.openqa.selenium.By element, Duration timeout)
      Waits for a specific seconds for the element to be selected.
      Parameters:
      element - - By element
      timeout - - Duration timeout
      Returns:
      boolean - true if element is selected, false otherwise
    • waitAndCheckIsElementSelected

      public boolean waitAndCheckIsElementSelected(String xpath, Duration timeout)
      Waits for a specific seconds for the element to be selected.
      Parameters:
      xpath - - Xpath of the element
      timeout - - Duration timeout
      Returns:
      boolean - true if element is selected, false otherwise
    • waitAndCheckIsElementAttributeHasValue

      public boolean waitAndCheckIsElementAttributeHasValue(String xpath, Duration timeout, String attributeName, String expectedValue)
      Waits for a specific seconds for the element's attribute to have a certain value.
      Parameters:
      xpath - - Xpath of the element
      timeout - - duration
      attributeName - - String attribute name
      expectedValue - - String expected attribute value
      Returns:
      boolean - true if the attribute has the expected value, false otherwise
    • waitAndCheckIsElementAttributeHasValue

      public boolean waitAndCheckIsElementAttributeHasValue(org.openqa.selenium.By element, Duration timeout, String attributeName, String expectedValue)
      Waits for a specific seconds for the element's attribute to have a certain value.
      Parameters:
      element - - By element
      timeout - - duration
      attributeName - - String attribute name
      expectedValue - - String expected attribute value
      Returns:
      boolean - true if the attribute has the expected value, false otherwise
    • waitAndCheckIsPresent

      public boolean waitAndCheckIsPresent(String xpath, Duration timeout)
      Waits for a specific seconds for the element to be present in the DOM.
      Parameters:
      xpath - - Xpath of the element
      timeout - - Duration timeout
      Returns:
      boolean - true if element is present, false otherwise
    • waitAndCheckIsPresent

      public boolean waitAndCheckIsPresent(org.openqa.selenium.By element, Duration timeout)
      Waits for a specific seconds for the element to be present in the DOM.
      Parameters:
      element - - By element
      timeout - - Duration timeout
      Returns:
      boolean - true if element is present, false otherwise
    • waitAndCheckIsClickableEnhanced

      public boolean waitAndCheckIsClickableEnhanced(org.openqa.selenium.By element, Duration timeout)
      Waits for a specific seconds for the element to be clickable using a custom condition.
      Parameters:
      element - - By element
      timeout - - Duration timeout
      Returns:
      boolean - true if the element is clickable, false otherwise
    • waitAndCheckIsClickableEnhanced

      public boolean waitAndCheckIsClickableEnhanced(String xpath, Duration timeout)
      Waits for a specific seconds for the element to be clickable using a custom condition.
      Parameters:
      xpath - - Xpath of the element
      timeout - - Duration timeout
      Returns:
      boolean - true if the element is clickable, false otherwise
    • scrollAndClick

      public boolean scrollAndClick(org.openqa.selenium.By element)
      Scrolls to the element and performs a click.
      Parameters:
      element - - By element
      Returns:
      boolean - true if the scroll and click were successful, false otherwise
    • scrollAndClick

      public boolean scrollAndClick(String xpath)
      Scrolls to the element and performs a click.
      Parameters:
      xpath - - Xpath of the element
      Returns:
      boolean - true if the scroll and click were successful, false otherwise