Class JenkinsRule.WebClient
- All Implemented Interfaces:
Serializable,AutoCloseable
- Enclosing class:
- JenkinsRule
WebClient and provide convenience methods
for accessing Hudson.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.htmlunit.WebClient
org.htmlunit.WebClient.PooledCSS3Parser -
Field Summary
Fields inherited from class org.htmlunit.WebClient
TARGET_BLANK, TARGET_SELF -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.htmlunit.WebRequestaddCrumb(org.htmlunit.WebRequest req) Adds a security crumb to the request.voidaddWebResponseListener(WebResponseListener listener) voidassertFails(String url, int statusCode) Verify that the server rejects an attempt to load the given page.createCrumbedUrl(String relativePath) Creates a URL with crumb parameters relative to {getContextPath()org.htmlunit.html.HtmlPageMakes an HTTP request, process it with the given request handler, and returns the response.<V> VexecuteOnServer(Callable<V> c) Executes the given closure on the server, by the servlet request handling thread, in the context of an HTTP request.Returns the URL of the webapp top page.Get JSON from a Jenkins relative endpoint.org.htmlunit.html.HtmlPagegetPage(hudson.model.Item item) org.htmlunit.html.HtmlPageorg.htmlunit.html.HtmlPagegetPage(hudson.model.Node item) org.htmlunit.html.HtmlPageorg.htmlunit.html.HtmlPagegetPage(hudson.model.Run r) Short forgetPage(r,""), to access the top page of a build.org.htmlunit.html.HtmlPageAccesses a page insideRun.org.htmlunit.html.HtmlPagegetPage(hudson.model.View view) org.htmlunit.html.HtmlPageorg.htmlunit.PageDeprecated.This method expects a full URL.org.htmlunit.html.HtmlPageRequests an HTML page within Jenkins.org.htmlunit.PageRequests a page within Jenkins.org.htmlunit.xml.XmlPageLoads a page as XML.booleanReturnstrueif JavaScript is enabled and the script engine was loaded successfully.booleanReturns whether or not redirections will be followed automatically on receipt of a redirect status code from the server.booleanReturns true if an exception will be thrown in the event of a failing response code.org.htmlunit.WebResponseloadWebResponse(org.htmlunit.WebRequest webRequest) Logs in to Hudson, by using the user name as the password.Logs in to Jenkins.Logs in to Jenkins.POST JSON content to a Jenkins relative endpoint.Send JSON content to a Jenkins relative endpoint.org.htmlunit.html.HtmlPagevoidsetJavaScriptEnabled(boolean enabled) Enables/disables JavaScript support.voidsetRedirectEnabled(boolean enabled) Sets whether or not redirections will be followed automatically on receipt of a redirect status code from the server.voidsetThrowExceptionOnFailingStatusCode(boolean enabled) Changes the behavior of this webclient when a script error occurs.withBasicApiToken(hudson.model.User user) Retrieve theApiTokenPropertyfrom the user, derive credentials from it and place it in Basic authorization header.withBasicApiToken(String userId) Retrieve theApiTokenPropertyfrom the associated user, derive credentials from it and place it in Basic authorization headerwithBasicCredentials(String loginAndPassword) UseloginAndPasswordas login AND password, especially useful forJenkinsRule.DummySecurityRealmAdd the "Authorization" header with Basic credentials derived from login using Base64withBasicCredentials(String login, String passwordOrToken) Add the "Authorization" header with Basic credentials derived from login and password using Base64withJavaScriptEnabled(boolean enabled) Enables/disables JavaScript support.withRedirectEnabled(boolean enabled) Sets whether or not redirections will be followed automatically on receipt of a redirect status code from the server.withThrowExceptionOnFailingStatusCode(boolean enabled) Changes the behavior of this webclient when a script error occurs.Methods inherited from class org.htmlunit.WebClient
addCookie, addRequestHeader, addWebWindowListener, buildWebSocketAdapter, close, containsWebWindow, deregisterWebWindow, download, expandUrl, getAjaxController, getAlertHandler, getAttachmentHandler, getBroadcastChannels, getBrowserVersion, getCache, getClipboardHandler, getConfirmHandler, getCookieManager, getCookies, getCredentialsProvider, getCSS3Parser, getCssErrorHandler, getCurrentWindow, getExecutor, getFrameContentHandler, getHTMLParserListener, getIncorrectnessListener, getJavaScriptEngine, getJavaScriptErrorListener, getJavaScriptTimeout, getOnbeforeunloadHandler, getOptions, getPage, getPage, getPage, getPageCreator, getPrintHandler, getPromptHandler, getRefreshHandler, getScriptPreProcessor, getStatusHandler, getStorageHolder, getTopLevelWindows, getWebConnection, getWebConsole, getWebStartHandler, getWebWindowByName, getWebWindows, guessContentType, initialize, initializeEmptyWindow, isJavaScriptEngineEnabled, loadDownloadedResponses, loadHtmlCodeIntoCurrentWindow, loadWebResponseInto, loadWebResponseInto, loadXHtmlCodeIntoCurrentWindow, openDialogWindow, openTargetWindow, openWindow, openWindow, printContentIfNecessary, registerWebWindow, removeRequestHeader, removeWebWindowListener, reset, setAjaxController, setAlertHandler, setAttachmentHandler, setCache, setClipboardHandler, setConfirmHandler, setCookieManager, setCredentialsProvider, setCssErrorHandler, setCurrentWindow, setExecutor, setFrameContentHandler, setHTMLParserListener, setIncorrectnessListener, setJavaScriptEngine, setJavaScriptErrorListener, setJavaScriptTimeout, setOnbeforeunloadHandler, setPageCreator, setPrintHandler, setPromptHandler, setRefreshHandler, setScriptPreProcessor, setStatusHandler, setWebConnection, setWebSocketAdapter, setWebStartHandler, throwFailingHttpStatusCodeExceptionIfNecessary, waitForBackgroundJavaScript, waitForBackgroundJavaScriptStartingBefore, waitForBackgroundJavaScriptStartingBefore
-
Constructor Details
-
WebClient
public WebClient()
-
-
Method Details
-
addWebResponseListener
-
loadWebResponse
public org.htmlunit.WebResponse loadWebResponse(org.htmlunit.WebRequest webRequest) throws IOException - Overrides:
loadWebResponsein classorg.htmlunit.WebClient- Throws:
IOException
-
login
Logs in to Jenkins.- Throws:
Exception
-
isJavaScriptEnabled
public boolean isJavaScriptEnabled()Returnstrueif JavaScript is enabled and the script engine was loaded successfully. Short-hand method to ease discovery of feature + improve readability- Overrides:
isJavaScriptEnabledin classorg.htmlunit.WebClient- Returns:
trueif JavaScript is enabled- Since:
- 2.0
- See Also:
-
WebClientOptions.isJavaScriptEnabled()
-
setJavaScriptEnabled
public void setJavaScriptEnabled(boolean enabled) Enables/disables JavaScript support. Short-hand method to ease discovery of feature + improve readability- Parameters:
enabled-trueto enable JavaScript support- Since:
- 2.0
- See Also:
-
WebClientOptions.setJavaScriptEnabled(boolean)
-
withJavaScriptEnabled
Enables/disables JavaScript support. Fluent method to ease discovery of feature + improve readability- Parameters:
enabled-trueto enable JavaScript support- Returns:
- self for fluent method chaining
- Since:
- 2.42
- See Also:
-
WebClientOptions.setJavaScriptEnabled(boolean)
-
isThrowExceptionOnFailingStatusCode
public boolean isThrowExceptionOnFailingStatusCode()Returns true if an exception will be thrown in the event of a failing response code. Short-hand method to ease discovery of feature + improve readability- Returns:
trueif an exception will be thrown in the event of a failing response code- Since:
- 2.42
- See Also:
-
WebClientOptions.isThrowExceptionOnFailingStatusCode()
-
setThrowExceptionOnFailingStatusCode
public void setThrowExceptionOnFailingStatusCode(boolean enabled) Changes the behavior of this webclient when a script error occurs. Short-hand method to ease discovery of feature + improve readability- Parameters:
enabled-trueto enable this feature- Since:
- 2.42
- See Also:
-
WebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)
-
withThrowExceptionOnFailingStatusCode
Changes the behavior of this webclient when a script error occurs. Fluent method to ease discovery of feature + improve readability- Parameters:
enabled-trueto enable this feature- Returns:
- self for fluent method chaining
- Since:
- 2.42
- See Also:
-
WebClientOptions.setThrowExceptionOnFailingStatusCode(boolean)
-
isRedirectEnabled
public boolean isRedirectEnabled()Returns whether or not redirections will be followed automatically on receipt of a redirect status code from the server. Short-hand method to ease discovery of feature + improve readability- Returns:
trueif automatic redirection is enabled- Since:
- 2.42
- See Also:
-
WebClientOptions.isRedirectEnabled()
-
setRedirectEnabled
public void setRedirectEnabled(boolean enabled) Sets whether or not redirections will be followed automatically on receipt of a redirect status code from the server. Short-hand method to ease discovery of feature + improve readability- Parameters:
enabled-trueto enable automatic redirection- Since:
- 2.42
- See Also:
-
WebClientOptions.setRedirectEnabled(boolean)
-
withRedirectEnabled
Sets whether or not redirections will be followed automatically on receipt of a redirect status code from the server. Fluent method to ease discovery of feature + improve readability- Parameters:
enabled-trueto enable automatic redirection- Returns:
- self for fluent method chaining
- Since:
- 2.42
- See Also:
-
WebClientOptions.setRedirectEnabled(boolean)
-
login
public JenkinsRule.WebClient login(String username, String password, boolean rememberMe) throws Exception Logs in to Jenkins.- Throws:
Exception
-
login
Logs in to Hudson, by using the user name as the password.See
JenkinsRule.configureUserRealm()for how the container is set up with the user names and passwords. All the test accounts have the same user name and password.- Throws:
Exception
-
executeOnServer
Executes the given closure on the server, by the servlet request handling thread, in the context of an HTTP request.In
JenkinsRule, a thread that's executing the test code is different from the thread that carries out HTTP requests made throughJenkinsRule.WebClient. But sometimes you want to make assertions and other calls with side-effect from within the request handling thread.This method allows you to do just that. It is useful for testing some methods that require
StaplerRequest2andStaplerResponse2, or getting the credential of the current user (viaJenkins.getAuthentication(), and so on.- Parameters:
c- The closure to be executed on the server.- Returns:
- The return value from the closure.
- Throws:
Exception- If a closure throws any exception, that exception will be carried forward.
-
search
- Throws:
IOExceptionSAXException
-
getPage
Short forgetPage(r,""), to access the top page of a build.- Throws:
IOExceptionSAXException
-
getPage
public org.htmlunit.html.HtmlPage getPage(hudson.model.Run r, String relative) throws IOException, SAXException Accesses a page insideRun.- Parameters:
relative- Relative URL within the build URL, like "changes". Doesn't start with '/'. Can be empty.- Throws:
IOExceptionSAXException
-
getPage
- Throws:
IOExceptionSAXException
-
getPage
public org.htmlunit.html.HtmlPage getPage(hudson.model.Item item, String relative) throws IOException, SAXException - Throws:
IOExceptionSAXException
-
getPage
- Throws:
IOExceptionSAXException
-
getPage
public org.htmlunit.html.HtmlPage getPage(hudson.model.Node item, String relative) throws IOException, SAXException - Throws:
IOExceptionSAXException
-
getPage
- Throws:
IOExceptionSAXException
-
getPage
public org.htmlunit.html.HtmlPage getPage(hudson.model.View view, String relative) throws IOException, SAXException - Throws:
IOExceptionSAXException
-
getPage
@Deprecated public org.htmlunit.Page getPage(String url) throws IOException, org.htmlunit.FailingHttpStatusCodeException Deprecated.This method expects a full URL. This method is marked as deprecated to warn you that you probably should be usinggoTo(String)method, which accepts a relative path within the Hudson being tested. (IOW, if you really need to hit a website on the internet, there's nothing wrong with using this method.)- Overrides:
getPagein classorg.htmlunit.WebClient- Throws:
IOExceptionorg.htmlunit.FailingHttpStatusCodeException
-
goTo
Requests an HTML page within Jenkins.- Parameters:
relative- Relative path within Jenkins. Starts without '/'. For example, "job/test/" to go to a job top page.- Throws:
IOExceptionSAXException
-
goTo
public org.htmlunit.Page goTo(String relative, @CheckForNull String expectedContentType) throws IOException, SAXException Requests a page within Jenkins.- Parameters:
relative- Relative path within Jenkins. Starts without '/'. For example, "job/test/" to go to a job top page.expectedContentType- the expectedWebResponse.getContentType(), or null to do no such check- Throws:
IOExceptionSAXException
-
goToXml
Loads a page as XML. Useful for testing Jenkins's XML API, in concert with assertXPath(DomNode page, String xpath)- Parameters:
path- the path part of the url to visit- Returns:
- the XmlPage found at that url
- Throws:
IOExceptionSAXException
-
assertFails
Verify that the server rejects an attempt to load the given page.- Parameters:
url- a URL path (relative to Jenkins root)statusCode- the expected failure code (such asHttpURLConnection.HTTP_FORBIDDEN)- Throws:
Exception- Since:
- 1.504
-
getContextPath
Returns the URL of the webapp top page. URL ends with '/'.This is actually the same as
JenkinsRule.getURL()and should not be confused withJenkinsRule.contextPath.- Throws:
IOException
-
addCrumb
public org.htmlunit.WebRequest addCrumb(org.htmlunit.WebRequest req) Adds a security crumb to the request. UsecreateCrumbedUrl(java.lang.String)instead if you intend to callWebRequest.setRequestBody(java.lang.String), typical of a POST request. -
createCrumbedUrl
Creates a URL with crumb parameters relative to {getContextPath()- Throws:
IOException
-
withBasicCredentials
@NonNull public JenkinsRule.WebClient withBasicCredentials(@NonNull String login, @NonNull String passwordOrToken) Add the "Authorization" header with Basic credentials derived from login and password using Base64- Since:
- 2.32
-
withBasicCredentials
UseloginAndPasswordas login AND password, especially useful forJenkinsRule.DummySecurityRealmAdd the "Authorization" header with Basic credentials derived from login using Base64- Since:
- 2.32
-
withBasicApiToken
Retrieve theApiTokenPropertyfrom the user, derive credentials from it and place it in Basic authorization header. If there is not available token it will generate one using the new system.- Since:
- 2.32 (since TODO it creates a new token everytime it's called)
- See Also:
-
withBasicApiToken
Retrieve theApiTokenPropertyfrom the associated user, derive credentials from it and place it in Basic authorization header- Since:
- 2.32
- See Also:
-
eval
Makes an HTTP request, process it with the given request handler, and returns the response.- Throws:
IOExceptionSAXException
-
getJSON
Get JSON from a Jenkins relative endpoint. You can preconfigure the web client for example to set a token for authentication, or accept error HTTP status before calling this method.- Parameters:
path- relative path, should not start with '/'- Returns:
- The JSON response from server.
- Throws:
IOException
-
putJSON
public JenkinsRule.JSONWebResponse putJSON(@NonNull String path, @NonNull net.sf.json.JSON json) throws IOException Send JSON content to a Jenkins relative endpoint. You can preconfigure the web client for example to set a token for authentication, or accept error HTTP status before calling this method.- Parameters:
path- relative path, should not start with '/'json- the json payload to send- Returns:
- The JSON response from server.
- Throws:
IOException
-
postJSON
public JenkinsRule.JSONWebResponse postJSON(@NonNull String path, @NonNull net.sf.json.JSON json) throws IOException POST JSON content to a Jenkins relative endpoint. You can preconfigure the web client for example to set a token for authentication or pass a crumb before calling this method.- Parameters:
path- relative path, should not start with '/'json- the json payload to send- Returns:
- The JSON response from server.
- Throws:
IOException
-