Package play.test

Class Helpers

java.lang.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 Details

    • 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
    • HTMLUNIT

      public static Class<? extends org.openqa.selenium.WebDriver> HTMLUNIT
    • FIREFOX

      public static Class<? extends org.openqa.selenium.WebDriver> FIREFOX
  • Constructor Details

    • Helpers

      public Helpers()
  • Method Details

    • 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 instead
      Creates 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 a ByteString.

      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 a ByteString.
      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 a ByteString.
      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 request
      router - The router
      requestBuilder - 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 request
      router - The router
      requestBuilder - The request builder
      timeout - 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 application
      call - the call to route
      Returns:
      the result
      See Also:
    • route

      public static Result route(Application app, Call call, long timeout)
      Route a call using the given application and timeout.
      Parameters:
      app - the application
      call - the call to route
      timeout - the time out
      Returns:
      the result
      See Also:
    • route

      public static Result route(Application app, Http.RequestBuilder requestBuilder)
      Route a request.
      Parameters:
      app - The application used while routing and executing the request
      requestBuilder - 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 request
      requestBuilder - the request builder
      timeout - 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.