Class Helpers
- Object
-
- play.test.Helpers
-
- All Implemented Interfaces:
Http.HeaderNames
,Http.Status
public class Helpers extends Object implements Http.Status, Http.HeaderNames
Helper functions to run tests.
-
-
Field Summary
Fields Modifier and Type Field Description static long
DEFAULT_TIMEOUT
Default Timeout (milliseconds) for fake requests issued by these Helpers.static String
DELETE
static Class<? extends org.openqa.selenium.WebDriver>
FIREFOX
static String
GET
static String
HEAD
static Class<? extends org.openqa.selenium.WebDriver>
HTMLUNIT
static String
POST
static String
PUT
-
Fields inherited from interface play.mvc.Http.HeaderNames
ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, ACCEPT_RANGES, ACCESS_CONTROL_ALLOW_CREDENTIALS, ACCESS_CONTROL_ALLOW_HEADERS, ACCESS_CONTROL_ALLOW_METHODS, ACCESS_CONTROL_ALLOW_ORIGIN, ACCESS_CONTROL_EXPOSE_HEADERS, ACCESS_CONTROL_MAX_AGE, ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, AGE, ALLOW, AUTHORIZATION, CACHE_CONTROL, CONNECTION, CONTENT_DISPOSITION, CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION, CONTENT_MD5, CONTENT_RANGE, CONTENT_SECURITY_POLICY, CONTENT_SECURITY_POLICY_REPORT_ONLY, CONTENT_TRANSFER_ENCODING, CONTENT_TYPE, COOKIE, DATE, ETAG, EXPECT, EXPIRES, FORWARDED, FROM, HOST, IF_MATCH, IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, LAST_MODIFIED, LINK, LOCATION, MAX_FORWARDS, ORIGIN, PRAGMA, PROXY_AUTHENTICATE, PROXY_AUTHORIZATION, RANGE, REFERER, REFERRER_POLICY, RETRY_AFTER, SERVER, SET_COOKIE, SET_COOKIE2, STRICT_TRANSPORT_SECURITY, TE, TRAILER, TRANSFER_ENCODING, UPGRADE, USER_AGENT, VARY, VIA, WARNING, WWW_AUTHENTICATE, X_CONTENT_SECURITY_POLICY_NONCE_HEADER, X_CONTENT_TYPE_OPTIONS, X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PORT, X_FORWARDED_PROTO, X_FRAME_OPTIONS, X_PERMITTED_CROSS_DOMAIN_POLICIES, X_REQUESTED_WITH, X_XSS_PROTECTION
-
Fields inherited from interface play.mvc.Http.Status
ACCEPTED, BAD_GATEWAY, BAD_REQUEST, CONFLICT, CONTINUE, CREATED, EXPECTATION_FAILED, FAILED_DEPENDENCY, FORBIDDEN, FOUND, GATEWAY_TIMEOUT, GONE, HTTP_VERSION_NOT_SUPPORTED, IM_A_TEAPOT, INSUFFICIENT_STORAGE, INTERNAL_SERVER_ERROR, LENGTH_REQUIRED, LOCKED, METHOD_NOT_ALLOWED, MOVED_PERMANENTLY, MULTI_STATUS, MULTIPLE_CHOICES, NETWORK_AUTHENTICATION_REQUIRED, NO_CONTENT, NON_AUTHORITATIVE_INFORMATION, NOT_ACCEPTABLE, NOT_FOUND, NOT_IMPLEMENTED, NOT_MODIFIED, OK, PARTIAL_CONTENT, PAYMENT_REQUIRED, PERMANENT_REDIRECT, PRECONDITION_FAILED, PRECONDITION_REQUIRED, PROXY_AUTHENTICATION_REQUIRED, REQUEST_ENTITY_TOO_LARGE, REQUEST_HEADER_FIELDS_TOO_LARGE, REQUEST_TIMEOUT, REQUEST_URI_TOO_LONG, REQUESTED_RANGE_NOT_SATISFIABLE, RESET_CONTENT, SEE_OTHER, SERVICE_UNAVAILABLE, SWITCHING_PROTOCOLS, TEMPORARY_REDIRECT, TOO_MANY_REQUESTS, UNAUTHORIZED, UNPROCESSABLE_ENTITY, UNSUPPORTED_MEDIA_TYPE, UPGRADE_REQUIRED, USE_PROXY
-
-
Constructor Summary
Constructors Constructor Description Helpers()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static ByteString
contentAsBytes(Result result)
Extracts the content as aByteString
.static ByteString
contentAsBytes(Result result, Materializer mat)
Extracts the content as aByteString
.static ByteString
contentAsBytes(Result result, Materializer mat, long timeout)
Extracts the content as aByteString
.static ByteString
contentAsBytes(play.twirl.api.Content content)
Extracts the content as bytes.static String
contentAsString(Result result)
Extracts the content as a String.static String
contentAsString(Result result, Materializer mat)
Extracts the content as a String.static String
contentAsString(Result result, Materializer mat, long timeout)
Extracts the content as a String.static String
contentAsString(play.twirl.api.Content content)
Extracts the content as a String.static play.core.j.JavaContextComponents
contextComponents()
Deprecated.Deprecated as of 2.8.0.static Application
fakeApplication()
Builds a new fake application, using GuiceApplicationBuilder.static Application
fakeApplication(Map<String,?> additionalConfiguration)
Build a new fake application.static Http.RequestBuilder
fakeRequest()
Builds a new "GET /" fake request.static Http.RequestBuilder
fakeRequest(String method, String uri)
Builds a new fake request.static Http.RequestBuilder
fakeRequest(Call call)
Builds a new fake request corresponding to a given route call.static Map<String,String>
inMemoryDatabase()
Constructs a in-memory (h2) database configuration to add to a fake application.static Map<String,String>
inMemoryDatabase(String name)
Constructs a in-memory (h2) database configuration to add to a fake application.static Map<String,String>
inMemoryDatabase(String name, Map<String,String> options)
Constructs a in-memory (h2) database configuration to add to a fake application.static Result
route(Application app, Call call)
Route a call using the given application.static Result
route(Application app, Call call, long timeout)
Route a call using the given application and timeout.static Result
route(Application app, Http.RequestBuilder requestBuilder)
Route a request.static Result
route(Application app, Http.RequestBuilder requestBuilder, long timeout)
Route the request considering the given timeout.static Result
routeAndCall(Application app, Router router, Http.RequestBuilder requestBuilder)
Route and call the request.static Result
routeAndCall(Application app, Router router, Http.RequestBuilder requestBuilder, long timeout)
Route and call the request, respecting the given timeout.static void
running(Application application, Runnable block)
Executes a block of code in a running application.static void
running(TestServer server, Class<? extends org.openqa.selenium.WebDriver> webDriver, Consumer<TestBrowser> block)
Executes a block of code in a running server, with a test browser.static void
running(TestServer server, Runnable block)
Executes a block of code in a running server.static void
running(TestServer server, org.openqa.selenium.WebDriver webDriver, Consumer<TestBrowser> block)
Executes a block of code in a running server, with a test browser.static void
start(Application application)
Starts a new application.static void
start(TestServer server)
Starts a Test server.static void
stop(Application application)
Stops an application.static void
stop(TestServer server)
Stops a Test server.static MessagesApi
stubMessagesApi()
Constructs an empty messagesApi instance.static MessagesApi
stubMessagesApi(Map<String,Map<String,String>> messages, Langs langs)
Constructs a MessagesApi instance containing the given keys and values.static TestBrowser
testBrowser()
Creates a Test Browser.static TestBrowser
testBrowser(int port)
Creates a Test Browser.static TestBrowser
testBrowser(Class<? extends org.openqa.selenium.WebDriver> webDriver)
Creates a Test Browser.static TestBrowser
testBrowser(Class<? extends org.openqa.selenium.WebDriver> webDriver, int port)
Creates a Test Browser.static TestBrowser
testBrowser(org.openqa.selenium.WebDriver of)
Creates a Test Browser.static TestBrowser
testBrowser(org.openqa.selenium.WebDriver of, int port)
Creates a Test Browser.static TestServer
testServer()
Creates a new Test server listening on port defined by configuration setting "testserver.port" (defaults to a random port).static TestServer
testServer(int port)
Creates a new Test server.static TestServer
testServer(int port, int sslPort)
Creates a new Test server.static TestServer
testServer(int port, Application app)
Creates a new Test server.static TestServer
testServer(Application app)
Creates a new Test server listening on port defined by configuration setting "testserver.port" (defaults to a random port) and using the given Application.
-
-
-
Field Detail
-
DEFAULT_TIMEOUT
public static final long DEFAULT_TIMEOUT
Default Timeout (milliseconds) for fake requests issued by these Helpers. This value is determined from System property test.timeout. The default value is 30000 (30 seconds).
-
GET
public static String GET
-
POST
public static String POST
-
PUT
public static String PUT
-
DELETE
public static String DELETE
-
HEAD
public static String HEAD
-
HTMLUNIT
public static Class<? extends org.openqa.selenium.WebDriver> HTMLUNIT
-
FIREFOX
public static Class<? extends org.openqa.selenium.WebDriver> FIREFOX
-
-
Method Detail
-
fakeRequest
public static Http.RequestBuilder fakeRequest()
Builds a new "GET /" fake request.- Returns:
- the request builder.
-
fakeRequest
public static Http.RequestBuilder fakeRequest(String method, String uri)
Builds a new fake request.- Parameters:
method
- the request method.uri
- the relative URL.- Returns:
- the request builder.
-
fakeRequest
public static Http.RequestBuilder fakeRequest(Call call)
Builds a new fake request corresponding to a given route call.- Parameters:
call
- the route call.- Returns:
- the request builder.
-
contextComponents
@Deprecated public static play.core.j.JavaContextComponents contextComponents()
Deprecated.Deprecated as of 2.8.0. Inject MessagesApi, Langs, FileMimeTypes or HttpConfiguration insteadCreates a new JavaContextComponents using play.api.Configuration.reference and play.api.Environment.simple as defaults- Returns:
- the newly created JavaContextComponents
-
fakeApplication
public static Application fakeApplication()
Builds a new fake application, using GuiceApplicationBuilder.- Returns:
- an application from the current path with no additional configuration.
-
inMemoryDatabase
public static Map<String,String> inMemoryDatabase()
Constructs a in-memory (h2) database configuration to add to a fake application.- Returns:
- a map of String containing database config info.
-
inMemoryDatabase
public static Map<String,String> inMemoryDatabase(String name)
Constructs a in-memory (h2) database configuration to add to a fake application.- Parameters:
name
- the database name.- Returns:
- a map of String containing database config info.
-
inMemoryDatabase
public static Map<String,String> inMemoryDatabase(String name, Map<String,String> options)
Constructs a in-memory (h2) database configuration to add to a fake application.- Parameters:
name
- the database name.options
- the database options.- Returns:
- a map of String containing database config info.
-
stubMessagesApi
public static MessagesApi stubMessagesApi()
Constructs an empty messagesApi instance.- Returns:
- a messagesApi instance containing no values.
-
stubMessagesApi
public static MessagesApi stubMessagesApi(Map<String,Map<String,String>> messages, Langs langs)
Constructs a MessagesApi instance containing the given keys and values.- Returns:
- a messagesApi instance containing given keys and values.
-
fakeApplication
public static Application fakeApplication(Map<String,?> additionalConfiguration)
Build a new fake application. Uses GuiceApplicationBuilder.- Parameters:
additionalConfiguration
- map containing config info for the app.- Returns:
- an application from the current path with additional configuration.
-
contentAsBytes
public static ByteString contentAsBytes(Result result)
Extracts the content as aByteString
.This method is only capable of extracting the content of results with strict entities. To extract the content of results with streamed entities, use
contentAsBytes(Result, Materializer)
.- Parameters:
result
- The result to extract the content from.- Returns:
- The content of the result as a ByteString.
- Throws:
UnsupportedOperationException
- if the result does not have a strict entity.
-
contentAsBytes
public static ByteString contentAsBytes(Result result, Materializer mat)
Extracts the content as aByteString
.- Parameters:
result
- The result to extract the content from.mat
- The materializer to use to extract the body from the result stream.- Returns:
- The content of the result as a ByteString.
-
contentAsBytes
public static ByteString contentAsBytes(Result result, Materializer mat, long timeout)
Extracts the content as aByteString
.- Parameters:
result
- The result to extract the content from.mat
- The materializer to use to extract the body from the result stream.timeout
- The amount of time, in milliseconds, to wait for the body to be produced.- Returns:
- The content of the result as a ByteString.
-
contentAsBytes
public static ByteString contentAsBytes(play.twirl.api.Content content)
Extracts the content as bytes.- Parameters:
content
- the content to be turned into bytes.- Returns:
- the body of the content as a byte string.
-
contentAsString
public static String contentAsString(play.twirl.api.Content content)
Extracts the content as a String.- Parameters:
content
- the content.- Returns:
- the body of the content as a String.
-
contentAsString
public static String contentAsString(Result result)
Extracts the content as a String.This method is only capable of extracting the content of results with strict entities. To extract the content of results with streamed entities, use
contentAsString(Result, Materializer)
.- Parameters:
result
- The result to extract the content from.- Returns:
- The content of the result as a String.
- Throws:
UnsupportedOperationException
- if the result does not have a strict entity.
-
contentAsString
public static String contentAsString(Result result, Materializer mat)
Extracts the content as a String.- Parameters:
result
- The result to extract the content from.mat
- The materializer to use to extract the body from the result stream.- Returns:
- The content of the result as a String.
-
contentAsString
public static String contentAsString(Result result, Materializer mat, long timeout)
Extracts the content as a String.- Parameters:
result
- The result to extract the content from.mat
- The materializer to use to extract the body from the result stream.timeout
- The amount of time, in milliseconds, to wait for the body to be produced.- Returns:
- The content of the result as a String.
-
routeAndCall
public static Result routeAndCall(Application app, Router router, Http.RequestBuilder requestBuilder)
Route and call the request.- Parameters:
app
- The application used while routing and executing the requestrouter
- The routerrequestBuilder
- The request builder- Returns:
- the result
-
routeAndCall
public static Result routeAndCall(Application app, Router router, Http.RequestBuilder requestBuilder, long timeout)
Route and call the request, respecting the given timeout.- Parameters:
app
- The application used while routing and executing the requestrouter
- The routerrequestBuilder
- The request buildertimeout
- The amount of time, in milliseconds, to wait for the body to be produced.- Returns:
- the result
-
route
public static Result route(Application app, Call call)
Route a call using the given application.- Parameters:
app
- the applicationcall
- the call to route- Returns:
- the result
- See Also:
GuiceApplicationBuilder
-
route
public static Result route(Application app, Call call, long timeout)
Route a call using the given application and timeout.- Parameters:
app
- the applicationcall
- the call to routetimeout
- the time out- Returns:
- the result
- See Also:
GuiceApplicationBuilder
-
route
public static Result route(Application app, Http.RequestBuilder requestBuilder)
Route a request.- Parameters:
app
- The application used while routing and executing the requestrequestBuilder
- the request builder- Returns:
- the result.
-
route
public static Result route(Application app, Http.RequestBuilder requestBuilder, long timeout)
Route the request considering the given timeout.- Parameters:
app
- The application used while routing and executing the requestrequestBuilder
- the request buildertimeout
- the amount of time, in milliseconds, to wait for the body to be produced.- Returns:
- the result
-
start
public static void start(Application application)
Starts a new application.- Parameters:
application
- the application to start.
-
stop
public static void stop(Application application)
Stops an application.- Parameters:
application
- the application to stop.
-
running
public static void running(Application application, Runnable block)
Executes a block of code in a running application.- Parameters:
application
- the application context.block
- the block to run after the Play app is started.
-
testServer
public static TestServer testServer()
Creates a new Test server listening on port defined by configuration setting "testserver.port" (defaults to a random port).- Returns:
- the test server.
-
testServer
public static TestServer testServer(Application app)
Creates a new Test server listening on port defined by configuration setting "testserver.port" (defaults to a random port) and using the given Application.- Parameters:
app
- the application.- Returns:
- the test server.
-
testServer
public static TestServer testServer(int port)
Creates a new Test server.- Parameters:
port
- the port to run the server on.- Returns:
- the test server.
-
testServer
public static TestServer testServer(int port, int sslPort)
Creates a new Test server.- Parameters:
port
- the port to run the server on.sslPort
- the port to run the server on.- Returns:
- the test server.
-
testServer
public static TestServer testServer(int port, Application app)
Creates a new Test server.- Parameters:
port
- the port to run the server on.app
- the Play application.- Returns:
- the test server.
-
start
public static void start(TestServer server)
Starts a Test server.- Parameters:
server
- the test server to start.
-
stop
public static void stop(TestServer server)
Stops a Test server.- Parameters:
server
- the test server to stop.a
-
running
public static void running(TestServer server, Runnable block)
Executes a block of code in a running server.- Parameters:
server
- the server to start.block
- the block of code to run after the server starts.
-
running
public static void running(TestServer server, Class<? extends org.openqa.selenium.WebDriver> webDriver, Consumer<TestBrowser> block)
Executes a block of code in a running server, with a test browser.- Parameters:
server
- the test server.webDriver
- the web driver class.block
- the block of code to execute.
-
running
public static void running(TestServer server, org.openqa.selenium.WebDriver webDriver, Consumer<TestBrowser> block)
Executes a block of code in a running server, with a test browser.- Parameters:
server
- the test server.webDriver
- the web driver instance.block
- the block of code to execute.
-
testBrowser
public static TestBrowser testBrowser()
Creates a Test Browser.Be aware: If set, the port the test browser is using is defined by the system property "testserver.port". Starting with Play 2.9, if this property is not set, the port by default is 0, which means the operating system will assign a random port. Thus, you should only use this method here if you did explicitly set the "testserver.port" property, otherwise you should use the testBrowser(port) method (which takes a port param).
- Returns:
- the test browser.
-
testBrowser
public static TestBrowser testBrowser(int port)
Creates a Test Browser.- Parameters:
port
- the local port.- Returns:
- the test browser.
-
testBrowser
public static TestBrowser testBrowser(Class<? extends org.openqa.selenium.WebDriver> webDriver)
Creates a Test Browser.Be aware: If set, the port the test browser is using is defined by the system property "testserver.port". Starting with Play 2.9, if this property is not set, the port by default is 0, which means the operating system will assign a random port. Thus, you should only use this method here if you did explicitly set the "testserver.port" property, otherwise you should use the testBrowser(webDriver, port) method (which takes a port param).
- Parameters:
webDriver
- the class of webdriver.- Returns:
- the test browser.
-
testBrowser
public static TestBrowser testBrowser(Class<? extends org.openqa.selenium.WebDriver> webDriver, int port)
Creates a Test Browser.- Parameters:
webDriver
- the class of webdriver.port
- the local port to test against.- Returns:
- the test browser.
-
testBrowser
public static TestBrowser testBrowser(org.openqa.selenium.WebDriver of, int port)
Creates a Test Browser.- Parameters:
of
- the web driver to run the browser with.port
- the port to run against http://localhost- Returns:
- the test browser.
-
testBrowser
public static TestBrowser testBrowser(org.openqa.selenium.WebDriver of)
Creates a Test Browser.Be aware: If set, the port the test browser is using is defined by the system property "testserver.port". Starting with Play 2.9, if this property is not set, the port by default is 0, which means the operating system will assign a random port. Thus, you should only use this method here if you did explicitly set the "testserver.port" property, otherwise you should use the testBrowser(of, port) method (which takes a port param).
- Parameters:
of
- the web driver to run the browser with.- Returns:
- the test browser.
-
-