Class CapybaraPortingLayerImpl
- java.lang.Object
-
- org.jenkinsci.test.acceptance.po.CapybaraPortingLayerImpl
-
- All Implemented Interfaces:
CapybaraPortingLayer
- Direct Known Subclasses:
AbstractJUnitTest
,Control
,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
CapybaraPortingLayerImpl.Finder<R>
protected class
CapybaraPortingLayerImpl.Resolver
-
Field Summary
Fields Modifier and Type Field Description protected org.openqa.selenium.WebDriver
driver
WebDriver
that subtypes use to talk to the server.com.google.inject.Injector
injector
Access to the rest of the world.static String
LABEL_TO_INPUT_XPATH
protected ElasticTime
time
-
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
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 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 elementvoid
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
clickButton(String text)
void
clickLink(String locator)
void
confirmAlert(int timeout)
Confirms an alert giving it some time to appearvoid
elasticSleep(long ms)
Object
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
findCaption(Class<?> type, CapybaraPortingLayerImpl.Finder<T> call)
org.openqa.selenium.WebElement
findIfNotVisible(org.openqa.selenium.By selector)
Returns the first element that matches the selector even if not visible.String
getCurrentUrl()
Get a string representing the current URL that the browser is looking at.String
getCurrentUrlWithFragment()
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 likefind(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.String
getPageSource()
Get all text of the page including markup.static String
getPageSource(org.openqa.selenium.WebDriver driver)
String
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
isStale(org.openqa.selenium.WebElement element)
Returnstrue
if the webElement is stale (that is accessing it would cause aStaleElementReferenceException
to be thrownorg.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.Resource
resource(String path)
Obtains a resource in a wrapper.void
runThenConfirmAlert(Runnable runnable)
void
runThenConfirmAlert(Runnable runnable, int timeout)
Do something that triggers an alert then giving it some time to appearvoid
runThenHandleAlert(Runnable runnable, Consumer<org.openqa.selenium.Alert> action)
void
runThenHandleAlert(Runnable runnable, Consumer<org.openqa.selenium.Alert> action, int timeoutSeconds)
void
sleep(long ms)
Thread.sleep that masks exception.protected org.openqa.selenium.WebDriver
visit(URL url)
Navigates the browser to the page.Wait<CapybaraPortingLayer>
waitFor()
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>
voidwaitFor(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
waitForCond(Callable<T> block)
Deprecated.<T> T
waitForCond(Callable<T> block, int timeoutSec)
Deprecated.
-
-
-
Field Detail
-
LABEL_TO_INPUT_XPATH
public static final String LABEL_TO_INPUT_XPATH
- See Also:
- Constant Field Values
-
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 Detail
-
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 Detail
-
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 interfaceCapybaraPortingLayer
-
choose
public org.openqa.selenium.WebElement choose(String locator)
Select radio button by its name, id, or label text.- Specified by:
choose
in interfaceCapybaraPortingLayer
-
waitFor
public <T> Wait<T> waitFor(T subject)
Default waiting object configured with default timing.- Specified by:
waitFor
in interfaceCapybaraPortingLayer
- See Also:
Wait
-
waitFor
public Wait<CapybaraPortingLayer> waitFor()
- Specified by:
waitFor
in interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
timeoutSec
- 0 if left to the default value
-
waitForCond
@Deprecated public <T> T waitForCond(Callable<T> block)
Deprecated.- Specified by:
waitForCond
in interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
- Throws:
org.openqa.selenium.NoSuchElementException
- if the element is not found.- See Also:
if you don't want to see an exception
-
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 interfaceCapybaraPortingLayer
- Throws:
org.openqa.selenium.NoSuchElementException
- if the element is not found.- See Also:
if you don't want to see an exception
-
getElement
public org.openqa.selenium.WebElement getElement(org.openqa.selenium.By selector)
Works likefind(org.openqa.selenium.By)
but instead of throwing an exception, this method returns null.- Specified by:
getElement
in interfaceCapybaraPortingLayer
-
fillIn
public void fillIn(String formFieldName, Object value)
- Specified by:
fillIn
in interfaceCapybaraPortingLayer
-
check
public void check(org.openqa.selenium.WebElement e)
Checks the checkbox.- Specified by:
check
in interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
-
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 withwaitForCond(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 interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
-
isStale
protected static boolean isStale(org.openqa.selenium.WebElement element)
Returnstrue
if the webElement is stale (that is accessing it would cause aStaleElementReferenceException
to be thrown- Parameters:
element
- the element to check.- Returns:
true
iff the element is stale.
-
executeScript
public Object executeScript(String javaScript, Object... args)
Executes JavaScript.- Specified by:
executeScript
in interfaceCapybaraPortingLayer
-
clickLink
public void clickLink(String locator)
- Specified by:
clickLink
in interfaceCapybaraPortingLayer
- Parameters:
locator
- Text, ID, or link.
-
check
public void check(String locator)
Checks the specified checkbox.- Specified by:
check
in interfaceCapybaraPortingLayer
-
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 interfaceCapybaraPortingLayer
- 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 interfaceCapybaraPortingLayer
- Parameters:
runnable
- Something that will trigger the alerttimeout
- 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, 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 interfaceCapybaraPortingLayer
-
getPageContent
public static String getPageContent(org.openqa.selenium.WebDriver driver)
Get visible text on the page.
-
-