org.xwiki.test.ui
Class TestUtils

java.lang.Object
  extended by org.xwiki.test.ui.TestUtils

public class TestUtils
extends Object

Helper methods for testing, not related to a specific Page Object. Also made available to tests classes.

Since:
3.2M3
Version:
$Id: d22ce579106d66c79070099ac1cb05ebbc67cbca $

Nested Class Summary
 class TestUtils.Session
          This class represents all cookies stored in the browser.
 
Field Summary
static org.apache.commons.httpclient.UsernamePasswordCredentials ADMIN_CREDENTIALS
           
static String BASE_BIN_URL
           
static String BASE_REST_URL
           
static String BASE_URL
           
static org.apache.commons.httpclient.UsernamePasswordCredentials SUPER_ADMIN_CREDENTIALS
           
 
Constructor Summary
TestUtils()
           
 
Method Summary
 ClassEditPage addClassProperty(String space, String page, String propertyName, String propertyType)
           
 void addObject(String space, String page, String className, Map<String,?> properties)
           
 void addObject(String space, String page, String className, Object... properties)
           
 void addQueryStringEntry(StringBuilder builder, String key, Object value)
           
 void addQueryStringEntry(StringBuilder builder, String key, String value)
           
 void assertOnPage(String pageURL)
          After successful completion of this function, you are guaranteed to be logged in as the given user and on the page passed in pageURL.
 void attachFile(String space, String page, String name, File file, boolean failIfExists)
           
 void attachFile(String space, String page, String name, InputStream is, boolean failIfExists)
           
 void attachFile(String space, String page, String name, InputStream is, boolean failIfExists, org.apache.commons.httpclient.UsernamePasswordCredentials credentials)
           
 ViewPage createPage(String space, String page, String content, String title)
           
 ViewPage createPage(String space, String page, String content, String title, String syntaxId)
           
 ViewPage createPage(String space, String page, String content, String title, String syntaxId, String parentFullPageName)
           
 ViewPage createPageWithAttachment(String space, String page, String content, String title, String attachmentName, InputStream attachmentData)
           
 ViewPage createPageWithAttachment(String space, String page, String content, String title, String attachmentName, InputStream attachmentData, org.apache.commons.httpclient.UsernamePasswordCredentials credentials)
           
 ViewPage createPageWithAttachment(String space, String page, String content, String title, String syntaxId, String parentFullPageName, String attachmentName, InputStream attachmentData)
           
 ViewPage createPageWithAttachment(String space, String page, String content, String title, String syntaxId, String parentFullPageName, String attachmentName, InputStream attachmentData, org.apache.commons.httpclient.UsernamePasswordCredentials credentials)
           
 void createUser(String username, String password, Object... properties)
           
 void deleteObject(String space, String page, String className, int objectNumber)
           
 void deletePage(String space, String page)
           
 void deleteSpace(String space)
          Accesses the URL to delete the specified space.
 ClassEditPage editClass(String space, String page)
           
 ObjectEditPage editObjects(String space, String page)
           
 String escapeURL(String s)
          Encodes a given string so that it may be used as a URL component.
protected  org.apache.commons.httpclient.methods.GetMethod executeGet(String uri, int expectedCode)
           
protected  org.apache.commons.httpclient.methods.PutMethod executePut(String uri, InputStream content, String mediaType, int... expectedCodes)
           
 List<org.openqa.selenium.WebElement> findElementsWithoutWaiting(org.openqa.selenium.WebDriver driver, org.openqa.selenium.By by)
           
 List<org.openqa.selenium.WebElement> findElementsWithoutWaiting(org.openqa.selenium.WebDriver driver, org.openqa.selenium.WebElement element, org.openqa.selenium.By by)
           
 org.openqa.selenium.WebElement findElementWithoutWaiting(org.openqa.selenium.WebDriver driver, org.openqa.selenium.By by)
           
 org.openqa.selenium.WebElement findElementWithoutWaiting(org.openqa.selenium.WebDriver driver, org.openqa.selenium.WebElement element, org.openqa.selenium.By by)
           
 void forceGuestUser()
          Forces the current user to be the Guest user by clearing all coookies.
 String getAttachmentURL(String space, String page, String attachment)
           
 String getAttachmentURL(String space, String page, String attachment, String action)
           
 String getAttachmentURL(String space, String page, String attachment, String action, String queryString)
           
protected  org.openqa.selenium.WebDriver getDriver()
           
 String getLoggedInUserName()
           
 String getMavenVersion()
           
 byte[] getRESTBuffer(String resourceUri, Map<String,Object[]> queryParams, Object... elements)
           
 InputStream getRESTInputStream(String resourceUri, Map<String,Object[]> queryParams, Object... elements)
           
<T> T
getRESTResource(String resourceUri, Map<String,Object[]> queryParams, Object... elements)
           
 String getSecretToken()
          Get the secret token used for CSRF protection.
 TestUtils.Session getSession()
           
 int getTimeout()
           
 String getURL(String space, String page)
          Get the URL to view a page.
 String getURL(String space, String page, String action)
          Get the URL of an action on a page.
 String getURL(String space, String page, String action, Map<String,?> queryParameters)
          Get the URL of an action on a page with specified parameters.
 String getURL(String space, String page, String action, String queryString)
          Get the URL of an action on a page with a specified query string.
 String getURLToDeletePage(String space, String page)
           
 String getURLToDeleteSpace(String space)
           
 String getURLToLoginAndGotoPage(String username, String password, String pageURL)
           
 String getURLToLoginAs(String username, String password)
           
 String getURLToLoginAsAdmin()
           
 String getURLToLoginAsAdminAndGotoPage(String pageURL)
           
 String getURLToLoginAsSuperAdmin()
           
 String getURLToLoginAsSuperAdminAndGotoPage(String pageURL)
           
 String getURLToLogout()
          Consider using setSession(null) because it will drop the cookies which is faster than invoking a logout action.
 String getURLToNonExistentPage()
           
 String getVersion()
           
 ViewPage gotoPage(String space, String page)
           
 void gotoPage(String space, String page, String action)
           
 void gotoPage(String space, String page, String action, Map<String,?> queryParameters)
           
 void gotoPage(String space, String page, String action, Object... queryParameters)
           
 void gotoPage(String space, String page, String action, String queryString)
           
 boolean hasElement(org.openqa.selenium.By by)
          Should be used when the result is supposed to be true (otherwise you'll incur the timeout).
 boolean hasElement(org.openqa.selenium.WebElement element, org.openqa.selenium.By by)
          Should be used when the result is supposed to be true (otherwise you'll incur the timeout).
 void importXar(File file)
           
 boolean isInClassEditMode()
           
 boolean isInCreateMode()
           
 boolean isInDeleteMode()
           
 boolean isInInlineEditMode()
           
 boolean isInObjectEditMode()
           
 boolean isInRenameMode()
           
 boolean isInRightsEditMode()
           
 boolean isInViewMode()
           
 boolean isInWikiEditMode()
           
 boolean isInWYSIWYGEditMode()
           
 boolean pageExists(String space, String page)
           
 void recacheSecretToken()
          (Re)-cache the secret token used for CSRF protection.
 void registerLoginAndGotoPage(String username, String password, String pageURL)
          Deprecated. starting with 5.0M2 use createUser(String, String, Object...) instead
static void setContext(PersistentTestContext context)
          Used so that AllTests can set the persistent test context.
 void setDriverImplicitWait(org.openqa.selenium.WebDriver driver)
          Forces the passed driver to wait for a getTimeout() number of seconds when looking up page elements before declaring that it cannot find them.
 void setSession(TestUtils.Session session)
           
 void setTimeout(int timeout)
           
 String toQueryParameterKey(String className, Integer objectNumber, String key)
           
 Map<String,?> toQueryParameters(Object... properties)
           
 Map<String,?> toQueryParameters(String className, Integer objectNumber, Map<String,?> properties)
           
 Map<String,?> toQueryParameters(String className, Integer objectNumber, Object... properties)
           
 String toQueryString(Map<String,?> queryParameters)
           
 String toQueryString(Object... queryParameters)
           
 void updateObject(String space, String page, String className, int objectNumber, Map<String,?> properties)
           
 void updateObject(String space, String page, String className, int objectNumber, Object... properties)
           
<T> void
waitUntilCondition(org.openqa.selenium.support.ui.ExpectedCondition<T> condition)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ADMIN_CREDENTIALS

public static final org.apache.commons.httpclient.UsernamePasswordCredentials ADMIN_CREDENTIALS
Since:
5.0M2

SUPER_ADMIN_CREDENTIALS

public static final org.apache.commons.httpclient.UsernamePasswordCredentials SUPER_ADMIN_CREDENTIALS
Since:
5.1M1

BASE_URL

public static final String BASE_URL
Since:
5.0M2

BASE_BIN_URL

public static final String BASE_BIN_URL
Since:
5.0M2

BASE_REST_URL

public static final String BASE_REST_URL
Since:
5.0M2
Constructor Detail

TestUtils

public TestUtils()
Method Detail

setContext

public static void setContext(PersistentTestContext context)
Used so that AllTests can set the persistent test context.


getDriver

protected org.openqa.selenium.WebDriver getDriver()

getSession

public TestUtils.Session getSession()

setSession

public void setSession(TestUtils.Session session)

getURLToLogout

public String getURLToLogout()
Consider using setSession(null) because it will drop the cookies which is faster than invoking a logout action.


getURLToLoginAsAdmin

public String getURLToLoginAsAdmin()

getURLToLoginAsSuperAdmin

public String getURLToLoginAsSuperAdmin()

getURLToLoginAs

public String getURLToLoginAs(String username,
                              String password)

getURLToLoginAsAdminAndGotoPage

public String getURLToLoginAsAdminAndGotoPage(String pageURL)
Parameters:
pageURL - the URL of the page to go to after logging in.
Returns:
URL to accomplish login and goto.

getURLToLoginAsSuperAdminAndGotoPage

public String getURLToLoginAsSuperAdminAndGotoPage(String pageURL)
Parameters:
pageURL - the URL of the page to go to after logging in.
Returns:
URL to accomplish login and goto.

getURLToLoginAndGotoPage

public String getURLToLoginAndGotoPage(String username,
                                       String password,
                                       String pageURL)
Parameters:
username - the name of the user to log in as.
password - the password for the user to log in.
pageURL - the URL of the page to go to after logging in.
Returns:
URL to accomplish login and goto.

getURLToNonExistentPage

public String getURLToNonExistentPage()
Returns:
URL to a non existent page that loads very fast (we are using plain mode so that we don't even have to display the skin ;))

assertOnPage

public void assertOnPage(String pageURL)
After successful completion of this function, you are guaranteed to be logged in as the given user and on the page passed in pageURL.

Parameters:
pageURL -

waitUntilCondition

public <T> void waitUntilCondition(org.openqa.selenium.support.ui.ExpectedCondition<T> condition)

getLoggedInUserName

public String getLoggedInUserName()

createUser

public void createUser(String username,
                       String password,
                       Object... properties)

registerLoginAndGotoPage

@Deprecated
public void registerLoginAndGotoPage(String username,
                                                String password,
                                                String pageURL)
Deprecated. starting with 5.0M2 use createUser(String, String, Object...) instead


gotoPage

public ViewPage gotoPage(String space,
                         String page)

gotoPage

public void gotoPage(String space,
                     String page,
                     String action)

gotoPage

public void gotoPage(String space,
                     String page,
                     String action,
                     Object... queryParameters)
Since:
3.5M1

gotoPage

public void gotoPage(String space,
                     String page,
                     String action,
                     Map<String,?> queryParameters)

gotoPage

public void gotoPage(String space,
                     String page,
                     String action,
                     String queryString)

getURLToDeletePage

public String getURLToDeletePage(String space,
                                 String page)

getURLToDeleteSpace

public String getURLToDeleteSpace(String space)
Parameters:
space - the name of the space to delete
Returns:
the URL that can be used to delete the specified pace
Since:
4.5

createPage

public ViewPage createPage(String space,
                           String page,
                           String content,
                           String title)

createPage

public ViewPage createPage(String space,
                           String page,
                           String content,
                           String title,
                           String syntaxId)

createPage

public ViewPage createPage(String space,
                           String page,
                           String content,
                           String title,
                           String syntaxId,
                           String parentFullPageName)

createPageWithAttachment

public ViewPage createPageWithAttachment(String space,
                                         String page,
                                         String content,
                                         String title,
                                         String syntaxId,
                                         String parentFullPageName,
                                         String attachmentName,
                                         InputStream attachmentData)
                                  throws Exception
Throws:
Exception
Since:
5.1M2

createPageWithAttachment

public ViewPage createPageWithAttachment(String space,
                                         String page,
                                         String content,
                                         String title,
                                         String syntaxId,
                                         String parentFullPageName,
                                         String attachmentName,
                                         InputStream attachmentData,
                                         org.apache.commons.httpclient.UsernamePasswordCredentials credentials)
                                  throws Exception
Throws:
Exception
Since:
5.1M2

createPageWithAttachment

public ViewPage createPageWithAttachment(String space,
                                         String page,
                                         String content,
                                         String title,
                                         String attachmentName,
                                         InputStream attachmentData)
                                  throws Exception
Throws:
Exception
Since:
5.1M2

createPageWithAttachment

public ViewPage createPageWithAttachment(String space,
                                         String page,
                                         String content,
                                         String title,
                                         String attachmentName,
                                         InputStream attachmentData,
                                         org.apache.commons.httpclient.UsernamePasswordCredentials credentials)
                                  throws Exception
Throws:
Exception
Since:
5.1M2

deletePage

public void deletePage(String space,
                       String page)

deleteSpace

public void deleteSpace(String space)
Accesses the URL to delete the specified space.

Parameters:
space - the name of the space to delete
Since:
4.5

pageExists

public boolean pageExists(String space,
                          String page)

getURL

public String getURL(String space,
                     String page)
Get the URL to view a page.

Parameters:
space - the space in which the page resides.
page - the name of the page.

getURL

public String getURL(String space,
                     String page,
                     String action)
Get the URL of an action on a page.

Parameters:
space - the space in which the page resides.
page - the name of the page.
action - the action to do on the page.

getURL

public String getURL(String space,
                     String page,
                     String action,
                     String queryString)
Get the URL of an action on a page with a specified query string.

Parameters:
space - the space in which the page resides.
page - the name of the page.
action - the action to do on the page.
queryString - the query string to pass in the URL.

getURL

public String getURL(String space,
                     String page,
                     String action,
                     Map<String,?> queryParameters)
Get the URL of an action on a page with specified parameters. If you need to pass multiple parameters with the same key, this function will not work.

Parameters:
space - the space in which the page resides.
page - the name of the page.
action - the action to do on the page.
queryParameters - the parameters to pass in the URL, these will be automatically URL encoded.

getAttachmentURL

public String getAttachmentURL(String space,
                               String page,
                               String attachment,
                               String action,
                               String queryString)
Parameters:
space - the name of the space that contains the page with the specified attachment
page - the name of the page that holds the attachment
attachment - the attachment name
action - the action to perform on the attachment
queryString - the URL query string
Returns:
the URL that performs the specified action on the specified attachment

getAttachmentURL

public String getAttachmentURL(String space,
                               String page,
                               String attachment,
                               String action)
Parameters:
space - the name of the space that contains the page with the specified attachment
page - the name of the page that holds the attachment
attachment - the attachment name
action - the action to perform on the attachment
Returns:
the URL that performs the specified action on the specified attachment

getAttachmentURL

public String getAttachmentURL(String space,
                               String page,
                               String attachment)
Parameters:
space - the name of the space that contains the page with the specified attachment
page - the name of the page that holds the attachment
attachment - the attachment name
Returns:
the URL to download the specified attachment

recacheSecretToken

public void recacheSecretToken()
(Re)-cache the secret token used for CSRF protection. A user with edit rights on Main.WebHome must be logged in. This method must be called before getSecretToken() is called and after each re-login.

See Also:
getSecretToken()

getSecretToken

public String getSecretToken()
Get the secret token used for CSRF protection. Remember to call recacheSecretToken() first.

Returns:
anti-CSRF secret token, or empty string if the token was not cached
See Also:
recacheSecretToken()

escapeURL

public String escapeURL(String s)
Encodes a given string so that it may be used as a URL component. Compatable with javascript decodeURIComponent, though more strict than encodeURIComponent: all characters except [a-zA-Z0-9], '.', '-', '*', '_' are converted to hexadecimal, and spaces are substituted by '+'.

Parameters:
s -

getTimeout

public int getTimeout()

setTimeout

public void setTimeout(int timeout)
Parameters:
timeout - the number of seconds after which we consider the action to have failed

setDriverImplicitWait

public void setDriverImplicitWait(org.openqa.selenium.WebDriver driver)
Forces the passed driver to wait for a getTimeout() number of seconds when looking up page elements before declaring that it cannot find them.


isInWYSIWYGEditMode

public boolean isInWYSIWYGEditMode()

isInWikiEditMode

public boolean isInWikiEditMode()

isInViewMode

public boolean isInViewMode()

isInInlineEditMode

public boolean isInInlineEditMode()

isInRightsEditMode

public boolean isInRightsEditMode()

isInObjectEditMode

public boolean isInObjectEditMode()

isInClassEditMode

public boolean isInClassEditMode()

isInDeleteMode

public boolean isInDeleteMode()

isInRenameMode

public boolean isInRenameMode()

isInCreateMode

public boolean isInCreateMode()

forceGuestUser

public void forceGuestUser()
Forces the current user to be the Guest user by clearing all coookies.


addObject

public void addObject(String space,
                      String page,
                      String className,
                      Object... properties)

addObject

public void addObject(String space,
                      String page,
                      String className,
                      Map<String,?> properties)

deleteObject

public void deleteObject(String space,
                         String page,
                         String className,
                         int objectNumber)

updateObject

public void updateObject(String space,
                         String page,
                         String className,
                         int objectNumber,
                         Map<String,?> properties)

updateObject

public void updateObject(String space,
                         String page,
                         String className,
                         int objectNumber,
                         Object... properties)

addClassProperty

public ClassEditPage addClassProperty(String space,
                                      String page,
                                      String propertyName,
                                      String propertyType)

toQueryString

public String toQueryString(Object... queryParameters)
Since:
3.5M1

toQueryString

public String toQueryString(Map<String,?> queryParameters)
Since:
3.5M1

addQueryStringEntry

public void addQueryStringEntry(StringBuilder builder,
                                String key,
                                Object value)

addQueryStringEntry

public void addQueryStringEntry(StringBuilder builder,
                                String key,
                                String value)

toQueryParameters

public Map<String,?> toQueryParameters(Object... properties)
Since:
3.5M1

toQueryParameters

public Map<String,?> toQueryParameters(String className,
                                       Integer objectNumber,
                                       Object... properties)

toQueryParameters

public Map<String,?> toQueryParameters(String className,
                                       Integer objectNumber,
                                       Map<String,?> properties)

toQueryParameterKey

public String toQueryParameterKey(String className,
                                  Integer objectNumber,
                                  String key)

findElementWithoutWaiting

public org.openqa.selenium.WebElement findElementWithoutWaiting(org.openqa.selenium.WebDriver driver,
                                                                org.openqa.selenium.By by)

findElementsWithoutWaiting

public List<org.openqa.selenium.WebElement> findElementsWithoutWaiting(org.openqa.selenium.WebDriver driver,
                                                                       org.openqa.selenium.By by)

findElementWithoutWaiting

public org.openqa.selenium.WebElement findElementWithoutWaiting(org.openqa.selenium.WebDriver driver,
                                                                org.openqa.selenium.WebElement element,
                                                                org.openqa.selenium.By by)

findElementsWithoutWaiting

public List<org.openqa.selenium.WebElement> findElementsWithoutWaiting(org.openqa.selenium.WebDriver driver,
                                                                       org.openqa.selenium.WebElement element,
                                                                       org.openqa.selenium.By by)

hasElement

public boolean hasElement(org.openqa.selenium.By by)
Should be used when the result is supposed to be true (otherwise you'll incur the timeout).


hasElement

public boolean hasElement(org.openqa.selenium.WebElement element,
                          org.openqa.selenium.By by)
Should be used when the result is supposed to be true (otherwise you'll incur the timeout).


editObjects

public ObjectEditPage editObjects(String space,
                                  String page)

editClass

public ClassEditPage editClass(String space,
                               String page)

getVersion

public String getVersion()
                  throws Exception
Throws:
Exception

getMavenVersion

public String getMavenVersion()
                       throws Exception
Throws:
Exception

attachFile

public void attachFile(String space,
                       String page,
                       String name,
                       File file,
                       boolean failIfExists)
                throws Exception
Throws:
Exception

attachFile

public void attachFile(String space,
                       String page,
                       String name,
                       InputStream is,
                       boolean failIfExists,
                       org.apache.commons.httpclient.UsernamePasswordCredentials credentials)
                throws Exception
Throws:
Exception
Since:
5.1M2

attachFile

public void attachFile(String space,
                       String page,
                       String name,
                       InputStream is,
                       boolean failIfExists)
                throws Exception
Throws:
Exception

importXar

public void importXar(File file)
               throws Exception
Throws:
Exception

getRESTInputStream

public InputStream getRESTInputStream(String resourceUri,
                                      Map<String,Object[]> queryParams,
                                      Object... elements)
                               throws Exception
Throws:
Exception

getRESTBuffer

public byte[] getRESTBuffer(String resourceUri,
                            Map<String,Object[]> queryParams,
                            Object... elements)
                     throws Exception
Throws:
Exception

getRESTResource

public <T> T getRESTResource(String resourceUri,
                             Map<String,Object[]> queryParams,
                             Object... elements)
                  throws Exception
Throws:
Exception

executeGet

protected org.apache.commons.httpclient.methods.GetMethod executeGet(String uri,
                                                                     int expectedCode)
                                                              throws Exception
Throws:
Exception

executePut

protected org.apache.commons.httpclient.methods.PutMethod executePut(String uri,
                                                                     InputStream content,
                                                                     String mediaType,
                                                                     int... expectedCodes)
                                                              throws Exception
Throws:
Exception


Copyright © 2004-2013 XWiki. All Rights Reserved.