Class CapybaraPortingLayerImpl

java.lang.Object
org.jenkinsci.test.acceptance.po.CapybaraPortingLayerImpl
All Implemented Interfaces:
CapybaraPortingLayer
Direct Known Subclasses:
AbstractJUnitTest, Control, KeycloakUtils, PageAreaImpl, PageObject, PendingScript, PendingSignature, Script, SlaveController

public class CapybaraPortingLayerImpl extends Object implements CapybaraPortingLayer
For assisting porting from Capybara.
Author:
Kohsuke Kawaguchi
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected class 
     
    protected class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected org.openqa.selenium.WebDriver
    WebDriver that subtypes use to talk to the server.
    com.google.inject.Injector
    Access to the rest of the world.
    static final String
     
    protected ElasticTime
     

    Fields inherited from interface org.jenkinsci.test.acceptance.po.CapybaraPortingLayer

    by
  • Constructor Summary

    Constructors
    Constructor
    Description
    CapybaraPortingLayerImpl(com.google.inject.Injector injector)
    Some subtypes are constructed via Guice, in which case injection is done by outside this class.
  • Method Summary

    Modifier and Type
    Method
    Description
    List<org.openqa.selenium.WebElement>
    all(org.openqa.selenium.By selector)
    Finds all the elements that match the selector.
    void
    blur(org.openqa.selenium.WebElement e)
    Sends a blur event to the provided element
    void
    check(String locator)
    Checks the specified checkbox.
    void
    check(org.openqa.selenium.WebElement e)
    Checks the checkbox.
    void
    check(org.openqa.selenium.WebElement e, boolean state)
    Sets the state of the checkbox to the specified value.
    org.openqa.selenium.WebElement
    choose(String locator)
    Select radio button by its name, id, or label text.
    void
     
    void
    clickLink(String locator)
     
    void
    confirmAlert(int timeout)
    Confirms an alert giving it some time to appear
    void
    elasticSleep(long ms)
     
    executeScript(String javaScript, Object... args)
    Executes JavaScript.
    void
    fillIn(String formFieldName, Object value)
     
    org.openqa.selenium.WebElement
    find(org.openqa.selenium.By selector)
    Returns the first visible element that matches the selector.
    protected <T> T
    findCaption(Class<?> type, Function<String,T> finder)
     
    protected <T> T
     
    org.openqa.selenium.WebElement
    findIfNotVisible(org.openqa.selenium.By selector)
    Returns the first element that matches the selector even if not visible.
    Get a string representing the current URL that the browser is looking at.
    Get a string representing the current URL with fragment part that the browser is looking at.
    org.openqa.selenium.WebElement
    getElement(org.openqa.selenium.By selector)
    Works like find(org.openqa.selenium.By) but instead of throwing an exception, this method returns null.
    static String
    getPageContent(org.openqa.selenium.WebDriver driver)
    Get visible text on the page.
    Get all text of the page including markup.
    static String
    getPageSource(org.openqa.selenium.WebDriver driver)
     
    getPath(org.openqa.selenium.WebElement e)
    Resolves the path attribute from the element, handling radio buttons and checkboxes.
    void
    handleAlert(Consumer<org.openqa.selenium.Alert> action)
     
    protected static boolean
    isHiddenOrStale(org.openqa.selenium.WebElement we)
     
    protected static boolean
    isStale(org.openqa.selenium.WebElement element)
    Returns true if the webElement is stale (that is accessing it would cause a StaleElementReferenceException to be thrown
    org.openqa.selenium.WebElement
    last(org.openqa.selenium.By selector)
    Picks up the last visible element that matches given selector.
    org.openqa.selenium.WebElement
    lastIfNotVisible(org.openqa.selenium.By selector)
    Picks up the last visible element that matches given selector.
    protected <T> T
    newInstance(Class<T> type, Object... args)
    Finds matching constructor and invoke it.
    Obtains a resource in a wrapper.
    void
     
    void
    runThenConfirmAlert(Runnable runnable, int timeout)
    Do something that triggers an alert then giving it some time to appear
    void
    runThenHandleAlert(Runnable runnable, Consumer<org.openqa.selenium.Alert> action)
     
    void
    runThenHandleAlert(Runnable runnable, Consumer<org.openqa.selenium.Alert> action, int timeoutSeconds)
     
    void
    Helper to execute something that shows a confirmation dialog with a "Yes" button or a classical browser confirm Executes the runnable, wait for "Yes" button of the modal dialog and presses the "Yes" button" In case the runnable opens a classical browse dialog this is accepted.
    void
    runThenHandleInputDialog(Runnable runnable, String input, String buttonLabel)
    Executes the runnable, then attempts to write input in a dialog's input field and click the button with the specified label buttonLabel.
    void
    sleep(long ms)
    Thread.sleep that masks exception.
    protected final org.openqa.selenium.WebDriver
    visit(URL url)
    Navigates the browser to the page.
     
    org.openqa.selenium.WebElement
    waitFor(org.openqa.selenium.By selector)
    Wait until the element that matches the given selector appears.
    org.openqa.selenium.WebElement
    waitFor(org.openqa.selenium.By selector, int timeoutSec)
    Wait until the element that matches the given selector appears.
    <MatcherT, SubjectT extends MatcherT>
    void
    waitFor(SubjectT item, org.hamcrest.Matcher<MatcherT> matcher, int timeout)
    Wait until a matcher matches.
    <T> Wait<T>
    waitFor(T subject)
    Default waiting object configured with default timing.
    <T> T
    Deprecated.
    <T> T
    waitForCond(Callable<T> block, int timeoutSec)
    Deprecated.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LABEL_TO_INPUT_XPATH

      public static final String LABEL_TO_INPUT_XPATH
      See Also:
    • driver

      @Inject protected org.openqa.selenium.WebDriver driver
      WebDriver that subtypes use to talk to the server.
    • injector

      @Inject public com.google.inject.Injector injector
      Access to the rest of the world.
    • time

      @Inject protected ElasticTime time
  • Constructor Details

    • CapybaraPortingLayerImpl

      public CapybaraPortingLayerImpl(com.google.inject.Injector injector)
      Some subtypes are constructed via Guice, in which case injection is done by outside this class. The injector parameter should be null for that case.

      Some subtypes are constructed programmatically. In that case, non-null injector must be supplied.

  • Method Details

    • getCurrentUrl

      public String getCurrentUrl()
      Get a string representing the current URL that the browser is looking at.
      Returns:
      The URL of the page currently loaded in the browser
    • getCurrentUrlWithFragment

      public String getCurrentUrlWithFragment()
      Get a string representing the current URL with fragment part that the browser is looking at.
      Returns:
      The URL with fragment part of the page currently loaded in the browser.
    • visit

      protected final org.openqa.selenium.WebDriver visit(URL url)
      Navigates the browser to the page.
      Parameters:
      url - URL relative to the context path of Jenkins, such as "/about" or "/job/foo/configure".
    • clickButton

      public void clickButton(String text)
      Specified by:
      clickButton in interface CapybaraPortingLayer
    • choose

      public org.openqa.selenium.WebElement choose(String locator)
      Select radio button by its name, id, or label text.
      Specified by:
      choose in interface CapybaraPortingLayer
    • waitFor

      public <T> Wait<T> waitFor(T subject)
      Default waiting object configured with default timing.
      Specified by:
      waitFor in interface CapybaraPortingLayer
      See Also:
    • waitFor

      public Wait<CapybaraPortingLayer> waitFor()
      Specified by:
      waitFor in interface CapybaraPortingLayer
    • waitFor

      public org.openqa.selenium.WebElement waitFor(org.openqa.selenium.By selector, int timeoutSec)
      Wait until the element that matches the given selector appears.
      Specified by:
      waitFor in interface CapybaraPortingLayer
    • waitFor

      public org.openqa.selenium.WebElement waitFor(org.openqa.selenium.By selector)
      Description copied from interface: CapybaraPortingLayer
      Wait until the element that matches the given selector appears.
      Specified by:
      waitFor in interface CapybaraPortingLayer
    • waitForCond

      @Deprecated public <T> T waitForCond(Callable<T> block, int timeoutSec)
      Deprecated.
      Repeated evaluate the given predicate until it returns true.

      If it times out, an exception will be thrown.

      Specified by:
      waitForCond in interface CapybaraPortingLayer
      timeoutSec - 0 if left to the default value
    • waitForCond

      @Deprecated public <T> T waitForCond(Callable<T> block)
      Deprecated.
      Specified by:
      waitForCond in interface CapybaraPortingLayer
    • waitFor

      public <MatcherT, SubjectT extends MatcherT> void waitFor(SubjectT item, org.hamcrest.Matcher<MatcherT> matcher, int timeout)
      Description copied from interface: CapybaraPortingLayer
      Wait until a matcher matches.
      Specified by:
      waitFor in interface CapybaraPortingLayer
    • find

      public org.openqa.selenium.WebElement find(org.openqa.selenium.By selector)
      Returns the first visible element that matches the selector.
      Specified by:
      find in interface CapybaraPortingLayer
      Throws:
      org.openqa.selenium.NoSuchElementException - if the element is not found.
      See Also:
    • findIfNotVisible

      public org.openqa.selenium.WebElement findIfNotVisible(org.openqa.selenium.By selector)
      Returns the first element that matches the selector even if not visible.
      Specified by:
      findIfNotVisible in interface CapybaraPortingLayer
      Throws:
      org.openqa.selenium.NoSuchElementException - if the element is not found.
      See Also:
    • getElement

      public org.openqa.selenium.WebElement getElement(org.openqa.selenium.By selector)
      Works like find(org.openqa.selenium.By) but instead of throwing an exception, this method returns null.
      Specified by:
      getElement in interface CapybaraPortingLayer
    • fillIn

      public void fillIn(String formFieldName, Object value)
      Specified by:
      fillIn in interface CapybaraPortingLayer
    • check

      public void check(org.openqa.selenium.WebElement e)
      Checks the checkbox.
      Specified by:
      check in interface CapybaraPortingLayer
    • check

      public void check(org.openqa.selenium.WebElement e, boolean state)
      Sets the state of the checkbox to the specified value.
      Specified by:
      check in interface CapybaraPortingLayer
    • getPath

      @CheckForNull public String getPath(org.openqa.selenium.WebElement e)
      Resolves the path attribute from the element, handling radio buttons and checkboxes.
      Parameters:
      e - the element to retrieve the path from.
      Returns:
      the path attribute if it exists.
    • blur

      public void blur(org.openqa.selenium.WebElement e)
      Sends a blur event to the provided element
      Specified by:
      blur in interface CapybaraPortingLayer
    • all

      public List<org.openqa.selenium.WebElement> all(org.openqa.selenium.By selector)
      Finds all the elements that match the selector.

      Note that this method inherits the same restriction of the WebDriver.findElements(org.openqa.selenium.By), in that its execution is not synchronized with the JavaScript execution of the browser.

      For example, if you click something that's expected to populate additional DOM elements, and then call all() to find them, then all() can execute before those additional DOM elements are populated, thereby failing to find the elements you are looking for.

      In contrast, find(org.openqa.selenium.By) do not have this problem, because it waits until the element that matches the criteria appears.

      So if you are using this method, think carefully. Perhaps you can use find(org.openqa.selenium.By) to achieve what you are looking for (by making the query more specific), or perhaps you can combine this with waitForCond(java.util.concurrent.Callable) so that if you don't find the elements you are looking for in the list, you'll retry.

      Specified by:
      all in interface CapybaraPortingLayer
    • last

      public org.openqa.selenium.WebElement last(org.openqa.selenium.By selector)
      Picks up the last visible element that matches given selector.
      Specified by:
      last in interface CapybaraPortingLayer
    • lastIfNotVisible

      public org.openqa.selenium.WebElement lastIfNotVisible(org.openqa.selenium.By selector)
      Picks up the last visible element that matches given selector.
      Specified by:
      lastIfNotVisible in interface CapybaraPortingLayer
    • isStale

      protected static boolean isStale(org.openqa.selenium.WebElement element)
      Returns true if the webElement is stale (that is accessing it would cause a StaleElementReferenceException to be thrown
      Parameters:
      element - the element to check.
      Returns:
      true iff the element is stale.
    • isHiddenOrStale

      protected static boolean isHiddenOrStale(org.openqa.selenium.WebElement we)
    • executeScript

      public Object executeScript(String javaScript, Object... args)
      Executes JavaScript.
      Specified by:
      executeScript in interface CapybaraPortingLayer
    • clickLink

      public void clickLink(String locator)
      Specified by:
      clickLink in interface CapybaraPortingLayer
      Parameters:
      locator - Text, ID, or link.
    • check

      public void check(String locator)
      Checks the specified checkbox.
      Specified by:
      check in interface CapybaraPortingLayer
    • runThenHandleDialog

      public void runThenHandleDialog(Runnable runnable)
      Helper to execute something that shows a confirmation dialog with a "Yes" button or a classical browser confirm Executes the runnable, wait for "Yes" button of the modal dialog and presses the "Yes" button" In case the runnable opens a classical browse dialog this is accepted.
      Parameters:
      runnable -
    • runThenHandleInputDialog

      public void runThenHandleInputDialog(Runnable runnable, String input, String buttonLabel)
      Executes the runnable, then attempts to write input in a dialog's input field and click the button with the specified label buttonLabel. If an alert appears, instead the runnable is re-run, the input written to the alert, then the alert is submitted.
      Parameters:
      runnable - the runnable to run that causes a dialog to appear
      input - the text to input into the dialog or alert
      buttonLabel - the button of the dialog to click
    • handleAlert

      public void handleAlert(Consumer<org.openqa.selenium.Alert> action)
    • runThenHandleAlert

      public void runThenHandleAlert(Runnable runnable, Consumer<org.openqa.selenium.Alert> action)
    • runThenHandleAlert

      public void runThenHandleAlert(Runnable runnable, Consumer<org.openqa.selenium.Alert> action, int timeoutSeconds)
    • confirmAlert

      public void confirmAlert(int timeout)
      Description copied from interface: CapybaraPortingLayer
      Confirms an alert giving it some time to appear
      Specified by:
      confirmAlert in interface CapybaraPortingLayer
      Parameters:
      timeout - Maximum time to wait for the alert to appear, in seconds
    • runThenConfirmAlert

      public void runThenConfirmAlert(Runnable runnable)
    • runThenConfirmAlert

      public void runThenConfirmAlert(Runnable runnable, int timeout)
      Description copied from interface: CapybaraPortingLayer
      Do something that triggers an alert then giving it some time to appear
      Specified by:
      runThenConfirmAlert in interface CapybaraPortingLayer
      Parameters:
      runnable - Something that will trigger the alert
      timeout - Maximum time to wait for the alert to appear, in seconds
    • sleep

      public void sleep(long ms)
      Thread.sleep that masks exception.
    • elasticSleep

      public void elasticSleep(long ms)
    • newInstance

      protected <T> T newInstance(Class<T> type, Object... args)
      Finds matching constructor and invoke it.

      This is often useful for binding PageArea by taking the concrete type as a parameter.

    • findCaption

      protected <T> T findCaption(Class<?> type, Function<String,T> finder)
    • findCaption

      protected <T> T findCaption(Class<?> type, CapybaraPortingLayerImpl.Finder<T> call)
    • getPageSource

      public static String getPageSource(org.openqa.selenium.WebDriver driver)
    • getPageSource

      public String getPageSource()
      Description copied from interface: CapybaraPortingLayer
      Get all text of the page including markup.
      Specified by:
      getPageSource in interface CapybaraPortingLayer
    • getPageContent

      public static String getPageContent(org.openqa.selenium.WebDriver driver)
      Get visible text on the page.
    • resource

      public Resource resource(String path)
      Obtains a resource in a wrapper.