org.jboss.test.selenium
Class AbstractSeleniumTestCase

java.lang.Object
  extended by org.jboss.test.selenium.AbstractSeleniumTestCase

public abstract class AbstractSeleniumTestCase
extends java.lang.Object

Class that serves as basis for all Selenium-based tests.

Version:
$Revision$
Author:
Lukas Fryc, Pavol Pitonak

Field Summary
static long AJAX_LOAD
          Generic timeout in miliseconds used for AJAX timeouts
static int ELEM_TIMEOUT
          Element timeout in seconds used in waitForElement functions
protected  java.util.Properties locatorsProperties
           
protected  java.util.Properties messagesProperties
           
static java.lang.String PAGE_LOAD
          Generic timeout in miliseconds used in every selenium function waitForPageToLoad()
protected  com.thoughtworks.selenium.Selenium selenium
           
 
Constructor Summary
AbstractSeleniumTestCase()
           
 
Method Summary
 void addRequiredScript(java.lang.String script)
          Add required script to page once.
 boolean assertTextOrder(java.lang.String text)
          Asserts the text order on the page.
 boolean belongsClass(java.lang.String className, java.lang.String locator)
          Checks if element given by locator is member of CSS class given by className.
 void clickAndWait(java.lang.String locator)
          Click and wait.
 void clickIfVisible(java.lang.String id)
          Click on an element only in the case element is present on the screen.
 int countTableRows(java.lang.String tableLocation)
          Counts table rows for selected table.
 int findTableRow(java.lang.String tableLocation, java.lang.String searchName, int searchCol)
          Finding the correct row in a table due to input string and columns.
static java.lang.String format(java.lang.String format, java.lang.Object... args)
          Uses a MessageFormat.format() to prepare given format string and use it to format result with given arguments.
 java.lang.String formatLoc(java.lang.String prop, java.lang.Object... args)
          Gets the property from messagesProperties and use it to format Message with given arguments
 java.lang.String formatMsg(java.lang.String prop, java.lang.Object... args)
          Gets the property from messagesProperties and use it to format Message with given arguments
 java.lang.String getAttributeOrNull(java.lang.String attributeLocator)
          Gets the value of an element attribute.
 int getJQueryCount(java.lang.String selector)
          Returns the count of elements for given jQuery selector
 java.lang.String getLoc(java.lang.String prop)
          Gets the property from locatorsProperties
 java.lang.String getLoc(java.lang.String prop, java.lang.String subst)
          Gets the property from locatorsProperties
protected  java.util.Properties getLocatorsProperties()
          This method initializes properties with locators.
 java.lang.String getMess(java.lang.String prop)
          Deprecated. 
 java.lang.String getMess(java.lang.String prop, java.lang.String subst)
          Deprecated. 
protected  java.util.Properties getMessagesProperties()
          This method initializes properties with messages.
 java.lang.String getMsg(java.lang.String prop)
          Gets the property from messagesProperties.
 java.lang.String getMsg(java.lang.String prop, java.lang.String subst)
          Gets the property from messagesProperties.
protected
<T> java.util.Properties
getNamedPropertiesForClass(java.lang.Class<T> tClass, java.lang.String name)
          Loads properties from specified resource file in context of specified class' package.
protected static java.util.Properties getProperties(java.lang.String resource)
          Loads properties from specified resource file
 java.lang.String getStyle(java.lang.String locator, java.lang.String property)
          Get current style value of element given by locator.
 java.lang.String getTextOrNull(java.lang.String locator)
          Gets the text of an element.
 boolean isDisplayed(java.lang.String locator)
          Returns whether the element is displayed on the page.
 void mouseOverAt(java.lang.String locator, java.lang.String coordString)
          Simulates a user hovering a mouse over the specified element at specific coordinates relative to element.
 void openAndWait(java.lang.String locator)
          Open and wait.
 java.lang.String removeJQueryPrefix(java.lang.String jqueryLocator)
          Remove *jquery= prefix of locators given by jQuery selector
 void safeSelect(java.lang.String locator, java.lang.String value)
          Selects a value only if the value is present in the select.
 void scrollIntoView(java.lang.String locator, boolean alignToTop)
          Aligns screen to top (resp.
 void selectIfNotSelected(java.lang.String selector, java.lang.String label)
          Select if not selected.
 void waitFor(long time)
          Waits for specified time in ms.
 void waitForAttributeChanges(java.lang.String attributeLocator, java.lang.String attributeValue)
          Wait for attribute of element given by attributeLocator changes from attributeValue to another value.
 java.lang.String waitForAttributeChangesAndReturn(java.lang.String attributeLocator, java.lang.String attributeValue)
          Wait for attribute of element given by attributeLocator changes from attributeValue to another value and returns this new value.
 void waitForElement(java.lang.String locator)
          Waits for element to appear on the screen.
 void waitForElement(java.lang.String locator, int step)
          Waits for element to appear on the screen.
 void waitForElement(java.lang.String locatorElem, long timeToWait)
          Waits for element to appear on the screen.
 void waitForElement(java.lang.String locatorElem, java.lang.String locatorLink, long timeToWait, long n)
          Deprecated. 
 void waitForText(java.lang.String text)
          Waits for text to appear on the screen.
 void waitForTextChanges(java.lang.String locator, java.lang.String lastText)
          Wait for text of element given by locator changes from lastText to another text.
 java.lang.String waitForTextChangesAndReturn(java.lang.String locator, java.lang.String lastText)
          Wait for text of element given by locator changes from lastText to another text and returns this new text.
 void waitForTextEquals(java.lang.String locator, java.lang.String pattern)
          Waits for element specified by locator does contain text specified by pattern
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

locatorsProperties

protected java.util.Properties locatorsProperties

messagesProperties

protected java.util.Properties messagesProperties

selenium

protected com.thoughtworks.selenium.Selenium selenium

PAGE_LOAD

public static java.lang.String PAGE_LOAD
Generic timeout in miliseconds used in every selenium function waitForPageToLoad()


AJAX_LOAD

public static long AJAX_LOAD
Generic timeout in miliseconds used for AJAX timeouts


ELEM_TIMEOUT

public static int ELEM_TIMEOUT
Element timeout in seconds used in waitForElement functions

Constructor Detail

AbstractSeleniumTestCase

public AbstractSeleniumTestCase()
Method Detail

getMessagesProperties

protected java.util.Properties getMessagesProperties()
This method initializes properties with messages. Should be overridden to properly initialize properties.

Returns:
initialized properties

getLocatorsProperties

protected java.util.Properties getLocatorsProperties()
This method initializes properties with locators. Should be overridden to properly initialize properties.

Returns:
initialized properties

getProperties

protected static java.util.Properties getProperties(java.lang.String resource)
                                             throws java.io.IOException
Loads properties from specified resource file

Parameters:
resource - where in classpath the file is located
Returns:
loaded properties
Throws:
java.io.IOException - if an error occurred when reading resource file

getNamedPropertiesForClass

protected <T> java.util.Properties getNamedPropertiesForClass(java.lang.Class<T> tClass,
                                                              java.lang.String name)
                                                   throws java.lang.IllegalStateException
Loads properties from specified resource file in context of specified class' package.

Parameters:
tClass - named resource will be searched in context of this class
name - name of resource contained in current class' package
Returns:
loaded properties
Throws:
java.lang.IllegalStateException - if an error occurred when reading resource file

getMess

@Deprecated
public java.lang.String getMess(java.lang.String prop,
                                           java.lang.String subst)
Deprecated. 

Gets the property from messagesProperties. Use getMsg(String, String) instead of this deprecated method.

Parameters:
prop - the name of the property
subst - the value which is returned in the case the property isn't set
Returns:
the property
Throws:
java.lang.IllegalStateException - when property wasn't found and substitution isn't set
See Also:
getMsg(String, String), getMessagesProperties()

getMess

@Deprecated
public java.lang.String getMess(java.lang.String prop)
Deprecated. 

Gets the property from messagesProperties. Use getMess(String) instead of this deprecated method.

Parameters:
prop - the name of the property
Returns:
the property
Throws:
java.lang.IllegalStateException - when property wasn't found
See Also:
getMsg(String), getMessagesProperties()

getMsg

public java.lang.String getMsg(java.lang.String prop,
                               java.lang.String subst)
Gets the property from messagesProperties.

Parameters:
prop - the name of the property
subst - the value which is returned in the case the property isn't set
Returns:
the property
Throws:
java.lang.IllegalStateException - when property wasn't found and substitution isn't set
See Also:
getMessagesProperties()

getMsg

public java.lang.String getMsg(java.lang.String prop)
Gets the property from messagesProperties.

Parameters:
prop - the name of the property
Returns:
the property
Throws:
java.lang.IllegalStateException - when property wasn't found
See Also:
getMessagesProperties()

formatMsg

public java.lang.String formatMsg(java.lang.String prop,
                                  java.lang.Object... args)
Gets the property from messagesProperties and use it to format Message with given arguments

Parameters:
prop - the name of the property with message format.
args - an array of atributes to be formatted and substituted to prop
Returns:
the property
Throws:
java.lang.IllegalStateException - when property wasn't found

getLoc

public java.lang.String getLoc(java.lang.String prop,
                               java.lang.String subst)
Gets the property from locatorsProperties

Parameters:
prop - the name of the property
subst - the value which is returned in the case the property isn't set
Returns:
the property
Throws:
java.lang.IllegalStateException - when property wasn't found and substitution isn't set
See Also:
getLocatorsProperties()

getLoc

public java.lang.String getLoc(java.lang.String prop)
Gets the property from locatorsProperties

Parameters:
prop - the name of the property
Returns:
the property
Throws:
java.lang.IllegalStateException - when property wasn't found
See Also:
getLocatorsProperties()

formatLoc

public java.lang.String formatLoc(java.lang.String prop,
                                  java.lang.Object... args)
Gets the property from messagesProperties and use it to format Message with given arguments

Parameters:
prop - the name of the property with message format.
args - an array of atributes to be formatted and substituted to prop
Returns:
the property
Throws:
java.lang.IllegalStateException - when property wasn't found
See Also:
getLocatorsProperties()

format

public static java.lang.String format(java.lang.String format,
                                      java.lang.Object... args)
Uses a MessageFormat.format() to prepare given format string and use it to format result with given arguments.

Parameters:
format - string used in MessageFormat.format()
args - used to formatting given format string
Returns:
string formatted using given arguments

clickIfVisible

public void clickIfVisible(java.lang.String id)
Click on an element only in the case element is present on the screen.

Parameters:
id - element locator

findTableRow

public int findTableRow(java.lang.String tableLocation,
                        java.lang.String searchName,
                        int searchCol)
Finding the correct row in a table due to input string and columns. The function uses selenium javascript extension findTableRow. It tries to look up the input string in the chosen column in selected table. After the first occurrence of input string, it returns the number of row where it was found.

Parameters:
tableLocation - the table locator
searchName - the string which the function searches for
searchCol - the column in which the function searches for
Returns:
the number of row with the first occurrence of searchName string.

countTableRows

public int countTableRows(java.lang.String tableLocation)
Counts table rows for selected table.

Parameters:
tableLocation - the table locator
Returns:
the number of rows of the table

safeSelect

public void safeSelect(java.lang.String locator,
                       java.lang.String value)
Selects a value only if the value is present in the select. If the value is not present, selection fails.

Parameters:
locator - the locator of the select tag
value - the value which should be selected

clickAndWait

public void clickAndWait(java.lang.String locator)
Click and wait. Substitution for two selenium commands click and waitForPageToLoad.

Parameters:
locator - the locator of the element to be clicked on

openAndWait

public void openAndWait(java.lang.String locator)
Open and wait. Substitution for two selenium commands open and waitForPageToLoad.

Parameters:
locator - the address to be opened

selectIfNotSelected

public void selectIfNotSelected(java.lang.String selector,
                                java.lang.String label)
Select if not selected. If the value is already selected, the method does nothing.

Parameters:
selector - the selector locator
label - the label to be selected

assertTextOrder

public boolean assertTextOrder(java.lang.String text)
Asserts the text order on the page. The messages which order is to be determined are inputed in a comma separated way. i.e. "string1;string2;string3". The method asserts that they appear on the page in the same order as typed in the input string. It returns true if the order is the same or false otherwise.

Parameters:
text - input text in comma separated format which order is to be asserted
Returns:
true if the order is the same or false otherwise

waitFor

public void waitFor(long time)
Waits for specified time in ms. Used mostly in AJAX based tests.

Parameters:
time - the time (in ms) to be waited for.

waitForElement

public void waitForElement(java.lang.String locator)
Waits for element to appear on the screen. Used mostly in AJAX based tests.

Parameters:
locator - the locator of the element to be waited for

waitForElement

public void waitForElement(java.lang.String locator,
                           int step)
Waits for element to appear on the screen. Used mostly in AJAX based tests.

Parameters:
locator - the locator of the element to be waited for
step - interval between two pollings

waitForElement

public void waitForElement(java.lang.String locatorElem,
                           long timeToWait)
Waits for element to appear on the screen. Used mostly in AJAX based tests.

Parameters:
locatorElem - the element locator
timeToWait - the time (in ms) to wait until timeout is reached

waitForElement

@Deprecated
public void waitForElement(java.lang.String locatorElem,
                                      java.lang.String locatorLink,
                                      long timeToWait,
                                      long n)
Deprecated. 

Waits for element to appear on the screen. Used mostly in AJAX based tests.

Parameters:
locatorElem - the element locator
locatorLink - the link locator - not used
timeToWait - the time (in ms) to wait until timeout is reached
n - the n
See Also:
waitForElement(String, long)

waitForText

public void waitForText(java.lang.String text)
Waits for text to appear on the screen. Used mostly in AJAX based tests.

Parameters:
text - the text to be waited for

waitForTextEquals

public void waitForTextEquals(java.lang.String locator,
                              java.lang.String pattern)
Waits for element specified by locator does contain text specified by pattern

Parameters:
locator - text of this locator's element will be tested for equivalence to pattern text
pattern - pattern text, which will be tested to equivalence to element's text given by locator

waitForTextChanges

public void waitForTextChanges(java.lang.String locator,
                               java.lang.String lastText)
Wait for text of element given by locator changes from lastText to another text.

Parameters:
locator - locator of element
lastText - current text what we are waiting for change

waitForTextChangesAndReturn

public java.lang.String waitForTextChangesAndReturn(java.lang.String locator,
                                                    java.lang.String lastText)
Wait for text of element given by locator changes from lastText to another text and returns this new text.

Parameters:
locator - locator of element
lastText - current text what we are waiting for change
Returns:
new value of text

waitForAttributeChanges

public void waitForAttributeChanges(java.lang.String attributeLocator,
                                    java.lang.String attributeValue)
Wait for attribute of element given by attributeLocator changes from attributeValue to another value.

Parameters:
attributeLocator - locator of attribute
attributeValue - current value attribute what we are waiting for change

waitForAttributeChangesAndReturn

public java.lang.String waitForAttributeChangesAndReturn(java.lang.String attributeLocator,
                                                         java.lang.String attributeValue)
Wait for attribute of element given by attributeLocator changes from attributeValue to another value and returns this new value.

Parameters:
attributeLocator - locator of attribute
attributeValue - current value attribute what we are waiting for change
Returns:
new value of attribute

getStyle

public java.lang.String getStyle(java.lang.String locator,
                                 java.lang.String property)
Get current style value of element given by locator. Use CSS style notation instead of JavaScript's camel notation! This methods of getting current style value haven't absolute browser compatibility. E.g.: use property "background-color" instead of "backgroundColor"

Parameters:
locator - of element from what we want to get current style value
property - CSS style property what we can to recognize
Returns:
current value of property if its element exists and has this property value set; null value otherwise
Throws:
java.lang.IllegalStateException - if is caught unrecognized throwable

scrollIntoView

public void scrollIntoView(java.lang.String locator,
                           boolean alignToTop)
Aligns screen to top (resp. bottom) of element given by locator. TODO should be reimplemented to use of JQuery.scrollTo

Parameters:
locator - of element which should be screen aligned to
alignToTop - should be top border of screen aligned to top border of element

getJQueryCount

public int getJQueryCount(java.lang.String selector)
Returns the count of elements for given jQuery selector

Parameters:
selector - jQuery selector
Returns:
count of elements matching given selector

belongsClass

public boolean belongsClass(java.lang.String className,
                            java.lang.String locator)
Checks if element given by locator is member of CSS class given by className.

Parameters:
className - name of CSS class
locator - element's locator
Returns:
true if element given by locator is member of CSS class given by className

isDisplayed

public boolean isDisplayed(java.lang.String locator)
Returns whether the element is displayed on the page.

Parameters:
locator - element locator
Returns:
if style contains "display: none;" returns false, else returns true

mouseOverAt

public void mouseOverAt(java.lang.String locator,
                        java.lang.String coordString)
Simulates a user hovering a mouse over the specified element at specific coordinates relative to element.

Parameters:
locator - element's locator
coordString - specifies the x,y position (i.e. - 10,20) of the mouse event relative to the element returned by the locator.

getTextOrNull

public java.lang.String getTextOrNull(java.lang.String locator)
Gets the text of an element. This works for any element that contains text. This command uses either the textContent (Mozilla-like browsers) or the innerText (IE-like browsers) of the element, which is the rendered text shown to the user. If no element with given locator is found, returns null.

Parameters:
locator - an element locator
Returns:
the text of the element or null if element's wasn't found

getAttributeOrNull

public java.lang.String getAttributeOrNull(java.lang.String attributeLocator)
Gets the value of an element attribute. The value of the attribute may differ across browsers (this is the case for the "style" attribute, for example). If no element with given attribute's locator is found, returns null.

Parameters:
attributeLocator - an element's attribute locator
Returns:
the attribute of the element or null if element's attribute wasn't found

addRequiredScript

public void addRequiredScript(java.lang.String script)
Add required script to page once. (This code will refuse adding script to the page duplicitly due to usage of hash, so you can add it thought you are not sure script is already added or not - this is usefull for adding libraries directly to the page). This script will be appended to end of the body tag within the script tag.

Parameters:
script - what should be added to the page

removeJQueryPrefix

public java.lang.String removeJQueryPrefix(java.lang.String jqueryLocator)
Remove *jquery= prefix of locators given by jQuery selector

Parameters:
jqueryLocator - locator in jQuery selector's syntax
Returns:
jQuery selector without ^jquery= locator prefix


Copyright © 2010. All Rights Reserved.