QA/Execution/Web Testing/Docs/Automation/Testcases/AMO POM Libraries/Utilities: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(Created page with "= Utility classes<br> = == TestCaseBreadcrumbs<br> == A TestCaseBreadcrumbs object is a "stack" of two-tuples which are intended as a simple way to save the current index/key/i...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
= Utility classes<br> =
= Utility classes<br> =


== TestCaseBreadcrumbs<br> ==
== TestCaseBreadcrumbs<br> ==


A TestCaseBreadcrumbs object is a "stack" of two-tuples which are intended as a simple way to save the current index/key/iteration values while working with nested loops. Its string representation could be added to an exception message or printed during 'verbose mode' to create a 'breadcrumb trail' to help troubleshooting.
A TestCaseBreadcrumbs object is a "stack" of two-tuples which are intended as a simple way to save the current index/key/iteration values while working with nested loops. Its string representation could be added to an exception message or printed during 'verbose mode' to create a 'breadcrumb trail' to help troubleshooting.  


The first element in each tuple is the label for the option or item type, such as "sort", "persona" or "app". The second element in the tuple is the option's/item's value. An option might have a short value, such as "popular" or "firefox", while an item might combine multiple values such as position in the list, add-on name and ID.
The first element in each tuple is the label for the option or item type, such as "sort", "persona" or "app". The second element in the tuple is the option's/item's value. An option might have a short value, such as "popular" or "firefox", while an item might combine multiple values such as position in the list, add-on name and ID.  


Conceptually the breadcrumbs object behaves like a stack where a label appears only once. New breadcrumbs are always appended to the end. If a new breadcrumb duplicates an existing breadcrumb's label, the breadcrumb with the matching label is replaced and all crumbs after it are discarded.
Conceptually the breadcrumbs object behaves like a stack where a label appears only once. New breadcrumbs are always appended to the end. If a new breadcrumb duplicates an existing breadcrumb's label, the breadcrumb with the matching label is replaced and all crumbs after it are discarded.  


Most test cases will only need to use the '''add''' method.&nbsp; Sometimes calling '''str'''(&nbsp; )&nbsp;may be necessar to explictly get a string representation.
Most test cases will only need to use the add(&nbsp;) method.&nbsp; Sometimes calling '''str'''(&nbsp; )&nbsp;may be necessar to explictly get a string representation.  


<br>'''METHODS'''
<br>'''METHODS'''  


'''__str__'''<br>The string representation returns the breadcrumbs in the order they exist in the  
'''__str__'''<br>The string representation returns the breadcrumbs in the order they exist in the  
Line 17: Line 17:
list. Each breadcrumb is formated in the form "label: value" where label and  
list. Each breadcrumb is formated in the form "label: value" where label and  


value is seperated by a colon. Breadcrumbs are seperated by a comma.
value is seperated by a colon. Breadcrumbs are seperated by a comma.  


If the breadcrumb list is empty then null string is returned
If the breadcrumb list is empty then null string is returned  


<br>'''add'''( ''label, value'' )<br>Parameters:<br>''label'': name of a option or element, such as "sort", "persona", "app"<br>''value'': value of the option or item.
<br>'''add'''( ''label, value'' )<br>Parameters:<br>''label'': name of a option or element, such as "sort", "persona", "app"<br>''value'': value of the option or item.  


If label does not exist as the first element in any breadcrumb the tuple (label, value) is appended to the list. When the label exists then the existing tuple is replaced with (label, value) and all tuples after it in the list are removed.
If label does not exist as the first element in any breadcrumb the tuple (label, value) is appended to the list. When the label exists then the existing tuple is replaced with (label, value) and all tuples after it in the list are removed.  


<br>'''index'''( ''label'' )<br>Parameter<br>''label'': label to search for in breadcrumbs
<br>'''index'''( ''label'' )<br>Parameter<br>''label'': label to search for in breadcrumbs  


Returns the index if the breadcrumb with the label using an exact match. The first breadcrumb is at index 0. Returns None if label was not found.<br>  
Returns the index if the breadcrumb with the label using an exact match. The first breadcrumb is at index 0. Returns None if label was not found.<br>  


<br>


 
'''delete'''( ''label''&nbsp; )<br>Parameter<br>''label'': label to search for in breadcrumbs  
'''delete'''( ''label''&nbsp; )<br>Parameter<br>''label'': label to search for in breadcrumbs


Deletes breadcrumb with the specified label and all other breadcrumbs after it.<br>Nothing is deleted when label is not found.<br> <br>'''clear_list'''( )<br>Resets breadcrumbs to an empty list<br>  
Deletes breadcrumb with the specified label and all other breadcrumbs after it.<br>Nothing is deleted when label is not found.<br> <br>'''clear_list'''( )<br>Resets breadcrumbs to an empty list<br>  


== <br>Locators ==
== <br>Locators ==


The Locator class provides descriptive methods locator that require value insertion. Some page classes will define an additional class for locators where simple subsitution is not sufficent, such as a listing of items whose index needs an offset (e.g. the n-th item in the list is at index n+1
The Locator class provides descriptive methods locator that require value insertion. Some page classes will define an additional class for locators where simple subsitution is not sufficent, such as a listing of items whose index needs an offset (e.g. the n-th item in the list is at index n+1  


'''METHODS'''
'''METHODS'''  


with_option(option):<br>with_index(element_index):<br>Inserts a value into a locator using the % operator (e.g string % option). Both  
'''with_option'''( ''option'' )<br>'''with_index'''( ''element_index'' )<br>Inserts a value into a locator using the&nbsp;% operator (e.g string&nbsp;% option). Both methods operate the same.&nbsp;&nbsp;Both are defined to offer better method name when passing an index# vs. an option value. If a locator requires multiple insertions (e.g an indexed element on an item that's in a listing) a tuple that corresponds to the locator's format can be passed to either method.<br> <br>'''is_css_locator'''( )<br>Returns True if begins like a CSS locator, with "css=", otherwise returns False<br> <br>'''is_xpath_locator'''( )<br>Returns True if begins like a Xpath locator, with "/", otherwise returns False


methods function the same and both exist to offer better method name when
== <br>SavedExceptions  ==


passing an index# vs. an option value. If a locator requires multiple
The SavedExceptions class provides a holder for exceptions whenever accumulating exceptions is preferred over ending a test case after an exception.  


insertions (e.g an indexed element on an item that's in a listing) a tuple that
<br>


corresponds to the locator's format can be passed to either method.<br> <br>is_css_locator( )<br>returns True if begins like a CSS locator, with "css=", otherwise returns False<br> <br>is_xpath_locator( )<br>returns True if begins like a Xpath locator, with "/", otherwise returns False
'''ATTRIBUTES'''&nbsp;<br>  


<br>SavedExceptions
'''exceptions_saved''' - a list exceptions, stored as strings. New exceptions are appended to the end of the list. Each string is potentially the concatenation of three parts: 1) the exception title, 2)&nbsp;exception detail, 2)&nbsp;breadcrumbs trail. Only the exception title is essential when an exception is saved <br>'''exceptions_saved_count''' - a dictionary of counts of the exception titles.&nbsp; The attribute is explained further in the save_exception method<br>  


The SavedExceptions class provides a holder for exceptions whenever accumulating
'''breadcrumbs''' - A TestCaseBreadcrumbs data type to simplfy including a breadcrumb trail with saved exceptions. See the TestCaseBreadcrumbs class for methods. <br>


exceptions is preferred over ending a test case after an exception.
'''has_exceptions''' - True if exceptions_saved is not null, False if it's a null list.  


Attributes:<br>exceptions_saved - a list exceptions, earch represented by a string. New
<br>  


exceptions are appended to the end of the list. Each string is potentially the
'''METHODS'''


concatenation of three parts - the exception title, exception detail,
'''save_exception'''( ''ex_base, ex_detail''&nbsp; )<br>Parameters<br>''ex_base'': First part of string that's the appended to ''exceptions_saved''. It is also the key for the ''exceptions_saved_counts'' entry which is incremented by 1 by the method. Typically the value is a short generic description such as "element X not found" or "element X on pages A and B are different".


breadcrumbs. Only the exception title is essential when an exception is saved
''ex_detail'': The second part of the exception which provides more specifics about the error. It may include the locator for a missing element, or the values and/or regular expression of a failed comparason.&nbsp; Although it is a required parameter it may be null. It is expected to be a string or a data type with a string representation. <br>


exceptions_saved_count - the counts of the exception titles, saved as a
''include_breadcrumbs'' (optional): If True, the default, the breadcrumbs attribute are included as the third part of the exception string. If False or or the breadcrumbs list is empty then nothing is added for the third part.


dictionary.
<br>


breadcrumbs - A TestCaseBreadcrumbs data type to simplfy including a breadcrumb
'''print_exceptions_to_stderr'''( ''test_case_name'' )<br>Parameter


trail with saved exceptions. See the TestCaseBreadcrumbs class for methods.
''test_case_name'': normally the test case's __name__ attribute


has_exceptions - True if exceptions_saved is not null, False if it's a null
Prints the saved exceptions to the stderr channel.&nbsp; The primary steps are:<br>


list.
#Print "test case:" followed by ''test_case_name'' parameter
#Prints the ''exceptions_saved_count'' dictionary entries using the format of the numeric value is followed by the key value (e.g. ''ex_base'' parameter in save_exception( )).&nbsp; The entries are not printed in any particular order.
#Prints then entries in the exceptions_saved in the order they appear in the list.
#Raises an exception.


Methods:
If the exceptions_saved list is empty the methods print the message "no exceptions saved" and skips steps 2-4.


save_exception<br>Parameters: <br>ex_base: Beginning of the string that's the exception. Is also the key for a
<br>  


value in exceptions_saved_counts which is incremented whenever an exception is
<br>


saved. Typically the parameter value is short generic description such as
== DataFunctions - Data Manipulation  ==


"element X not found" or "element X on pages A and B are different".
Functions for searching text and converting to other data types.  


ex_detail: The second part of the exception which provides more specifics about
'''ATTRIBUTES'''


the error. It may include the locator for a missing element, or the values
Regular expressions for recurring data <br> '''RE_LONG_DATE''': Regular expression that matches a long date, ex: "January 1, 1999"<br> '''RE_MMDDYYYY''': Regular expression that matches a mm/dd/yyyy date<br> '''RE_COUNT''': Regular expression that matches a positive number or 0, optionally with commas<br> '''RE_NON_ZERO_COUNT''': Regular expression that matches a positive number, optionally with commas<br> '''RE_RATING_STARS_PATTERN''': Regular expression that matches standardized rating text "rated # out of 5 stars"<br> '''RE_PAGE_RESULTS_NUMBERS''': Regular expression that matches "# - # of #"<br>


and/or regular expression, where the ex_base may often a more generic. Although
Other constant values<br> '''STRF_LONG_DATE''': Formator for date.strptime to parse text into a date object<br>
<br>
'''METHODS'''<br>


it is a required parameter is may be null. It is expected to be a string or a  
'''combos_from_lists'''(''lists'' ) <br> Returns a list of all combinations of values from input ''lists''. Each combination in the list returned is a list. Parameters: <br> ''lists'': list of lists, such as parameter options lists from TCparams <br>


data type with a str( ) method which returns a string representation.
'''clean_non_ascii'''( ''text'' ) <br> Return ''text'' with special characters converted to plain ascii. Non-printable characters are replaced with #&lt;num&gt;; where &lt;num&gt; is their character code.&nbsp; New-line characters are replaced with the literal "\n". <br>


include_breadcrumbs (optional): if True, the default, the breadcrumbs attribute
'''encode_for_url'''( ''text, encoding_method, chars_to_replace'' ) <br> Returns a string replacing any characters that considered "special"&nbsp;for a URL. <br> Parameters: <br> ''text'': string to convert <br> ''encoding_method'': how characters are replaced. Recognized values are: <br>


are included as the third part of the exception string. Nothing is added to the
*"HEX_CODE" (default) replaces each special character with its hex code value in the format&nbsp;%dd where 'dd' are digits <br>
*"DASH" replaces each sequence of special characters with a '-'.&nbsp; <br>
*"UNDERSCORE" replaces each sequence of special characters with a '_'.&nbsp; It will return "a_b" for the text "a b" or "a &amp; b".<br>


exception string if the parameter is False or the breadcrumbs list is empty.
''chars_to_replace'' (optional): string of characters that should be replaced. Overrides pre-defined special characters in the method.<br>


print_exceptions_to_stderr(test_case_name)<br>prints the saved exceptions to the stderr channel.<br>Printing consistents of four steps:<br>1) Print "test case:" followed by the test_case_name parameter<br>2) Prints the exceptions_saved_count dictionary. For each value the numeric
If encoding_method is "DASH" and chars_to_replace include space and "&amp;", the input ''text'' "a b" or "a &amp; b" will return "a-b".&nbsp; Similarly, for the same input value the encoding_method "UNDERSCORE"&nbsp;will return "a_b"<br> <br>  


value is followed by the key value (e.g. ex_base parameter in save_exception( ))
'''text_long_date_to_date'''( ''text'' ) <br> Returns the date, as a date type, found in the ''text''. Expects the text is in long date format (ex: January 1, 2010). Throws an exception if the conversion fails. <br>


The values are not printed in any particular order.<br>3) Prints then entries in the exceptions_saved in the order they appear in the  
'''find_long_date'''( ''text, leadingRegex, trailingRegex'') <br> Returns the string containing the first long date found.<br> Parameters:<br> ''text'': text toe be searched. <br> ''leadingRegex'' (optional): regular expression for text that must immediately proceed the date, if specified.<br> ''trailingRegex ''(optional):regular expression for text that must immediately follow the date, if specified.<br> Any text that matches leadingRegex or trailingRegex is not returned. An exception is thrown if the string match fails.


list.<br>4) Raises an exception.
<br>'''text_to_integer'''( ''text'' ) <br> Convert text to an integer. Thrown an exception if the conversion fails.<br>


If the exceptions_saved list is empty the methods print the message 'no
'''find_count'''( ''text, leadingRegex, trailingRegex '') <br>


exceptions saved' and steps 2-4 are skipped.
Returns the string containing the first positive integer or 0.<br> Parameters: text: the string to be searched.<br> leadingRegex (optional): regular expression for text that must immediately proceed the count, if specified. trailingRegex (optional): regular expression for text that must immediately follow the count, if specified.  


<br>
Any text that matches leadingRegex or trailingRegex is not returned. An exception is thrown if the string match fails. <br> '''find_page_results_tuple'''( ''text'' )<br> Returns a string three-tuple of the three values found in ''text'' that are returned by matching the pattern "# - # of #" where # are positive integers or 0. An exception is thrown if the pattern match fails.

Latest revision as of 00:47, 12 October 2010

Utility classes

TestCaseBreadcrumbs

A TestCaseBreadcrumbs object is a "stack" of two-tuples which are intended as a simple way to save the current index/key/iteration values while working with nested loops. Its string representation could be added to an exception message or printed during 'verbose mode' to create a 'breadcrumb trail' to help troubleshooting.

The first element in each tuple is the label for the option or item type, such as "sort", "persona" or "app". The second element in the tuple is the option's/item's value. An option might have a short value, such as "popular" or "firefox", while an item might combine multiple values such as position in the list, add-on name and ID.

Conceptually the breadcrumbs object behaves like a stack where a label appears only once. New breadcrumbs are always appended to the end. If a new breadcrumb duplicates an existing breadcrumb's label, the breadcrumb with the matching label is replaced and all crumbs after it are discarded.

Most test cases will only need to use the add( ) method.  Sometimes calling str(  ) may be necessar to explictly get a string representation.


METHODS

__str__
The string representation returns the breadcrumbs in the order they exist in the

list. Each breadcrumb is formated in the form "label: value" where label and

value is seperated by a colon. Breadcrumbs are seperated by a comma.

If the breadcrumb list is empty then null string is returned


add( label, value )
Parameters:
label: name of a option or element, such as "sort", "persona", "app"
value: value of the option or item.

If label does not exist as the first element in any breadcrumb the tuple (label, value) is appended to the list. When the label exists then the existing tuple is replaced with (label, value) and all tuples after it in the list are removed.


index( label )
Parameter
label: label to search for in breadcrumbs

Returns the index if the breadcrumb with the label using an exact match. The first breadcrumb is at index 0. Returns None if label was not found.


delete( label  )
Parameter
label: label to search for in breadcrumbs

Deletes breadcrumb with the specified label and all other breadcrumbs after it.
Nothing is deleted when label is not found.

clear_list( )
Resets breadcrumbs to an empty list


Locators

The Locator class provides descriptive methods locator that require value insertion. Some page classes will define an additional class for locators where simple subsitution is not sufficent, such as a listing of items whose index needs an offset (e.g. the n-th item in the list is at index n+1

METHODS

with_option( option )
with_index( element_index )
Inserts a value into a locator using the % operator (e.g string % option). Both methods operate the same.  Both are defined to offer better method name when passing an index# vs. an option value. If a locator requires multiple insertions (e.g an indexed element on an item that's in a listing) a tuple that corresponds to the locator's format can be passed to either method.

is_css_locator( )
Returns True if begins like a CSS locator, with "css=", otherwise returns False

is_xpath_locator( )
Returns True if begins like a Xpath locator, with "/", otherwise returns False


SavedExceptions

The SavedExceptions class provides a holder for exceptions whenever accumulating exceptions is preferred over ending a test case after an exception.


ATTRIBUTES 

exceptions_saved - a list exceptions, stored as strings. New exceptions are appended to the end of the list. Each string is potentially the concatenation of three parts: 1) the exception title, 2) exception detail, 2) breadcrumbs trail. Only the exception title is essential when an exception is saved
exceptions_saved_count - a dictionary of counts of the exception titles.  The attribute is explained further in the save_exception method

breadcrumbs - A TestCaseBreadcrumbs data type to simplfy including a breadcrumb trail with saved exceptions. See the TestCaseBreadcrumbs class for methods.

has_exceptions - True if exceptions_saved is not null, False if it's a null list.


METHODS

save_exception( ex_base, ex_detail  )
Parameters
ex_base: First part of string that's the appended to exceptions_saved. It is also the key for the exceptions_saved_counts entry which is incremented by 1 by the method. Typically the value is a short generic description such as "element X not found" or "element X on pages A and B are different".

ex_detail: The second part of the exception which provides more specifics about the error. It may include the locator for a missing element, or the values and/or regular expression of a failed comparason.  Although it is a required parameter it may be null. It is expected to be a string or a data type with a string representation.

include_breadcrumbs (optional): If True, the default, the breadcrumbs attribute are included as the third part of the exception string. If False or or the breadcrumbs list is empty then nothing is added for the third part.


print_exceptions_to_stderr( test_case_name )
Parameter

test_case_name: normally the test case's __name__ attribute

Prints the saved exceptions to the stderr channel.  The primary steps are:

  1. Print "test case:" followed by test_case_name parameter
  2. Prints the exceptions_saved_count dictionary entries using the format of the numeric value is followed by the key value (e.g. ex_base parameter in save_exception( )).  The entries are not printed in any particular order.
  3. Prints then entries in the exceptions_saved in the order they appear in the list.
  4. Raises an exception.

If the exceptions_saved list is empty the methods print the message "no exceptions saved" and skips steps 2-4.



DataFunctions - Data Manipulation

Functions for searching text and converting to other data types.

ATTRIBUTES

Regular expressions for recurring data
RE_LONG_DATE: Regular expression that matches a long date, ex: "January 1, 1999"
RE_MMDDYYYY: Regular expression that matches a mm/dd/yyyy date
RE_COUNT: Regular expression that matches a positive number or 0, optionally with commas
RE_NON_ZERO_COUNT: Regular expression that matches a positive number, optionally with commas
RE_RATING_STARS_PATTERN: Regular expression that matches standardized rating text "rated # out of 5 stars"
RE_PAGE_RESULTS_NUMBERS: Regular expression that matches "# - # of #"

Other constant values
STRF_LONG_DATE: Formator for date.strptime to parse text into a date object

METHODS

combos_from_lists(lists )
Returns a list of all combinations of values from input lists. Each combination in the list returned is a list. Parameters:
lists: list of lists, such as parameter options lists from TCparams

clean_non_ascii( text )
Return text with special characters converted to plain ascii. Non-printable characters are replaced with #<num>; where <num> is their character code.  New-line characters are replaced with the literal "\n".

encode_for_url( text, encoding_method, chars_to_replace )
Returns a string replacing any characters that considered "special" for a URL.
Parameters:
text: string to convert
encoding_method: how characters are replaced. Recognized values are:

  • "HEX_CODE" (default) replaces each special character with its hex code value in the format %dd where 'dd' are digits
  • "DASH" replaces each sequence of special characters with a '-'. 
  • "UNDERSCORE" replaces each sequence of special characters with a '_'.  It will return "a_b" for the text "a b" or "a & b".

chars_to_replace (optional): string of characters that should be replaced. Overrides pre-defined special characters in the method.

If encoding_method is "DASH" and chars_to_replace include space and "&", the input text "a b" or "a & b" will return "a-b".  Similarly, for the same input value the encoding_method "UNDERSCORE" will return "a_b"

text_long_date_to_date( text )
Returns the date, as a date type, found in the text. Expects the text is in long date format (ex: January 1, 2010). Throws an exception if the conversion fails.

find_long_date( text, leadingRegex, trailingRegex)
Returns the string containing the first long date found.
Parameters:
text: text toe be searched.
leadingRegex (optional): regular expression for text that must immediately proceed the date, if specified.
trailingRegex (optional):regular expression for text that must immediately follow the date, if specified.
Any text that matches leadingRegex or trailingRegex is not returned. An exception is thrown if the string match fails.


text_to_integer( text )
Convert text to an integer. Thrown an exception if the conversion fails.

find_count( text, leadingRegex, trailingRegex )

Returns the string containing the first positive integer or 0.
Parameters: text: the string to be searched.
leadingRegex (optional): regular expression for text that must immediately proceed the count, if specified. trailingRegex (optional): regular expression for text that must immediately follow the count, if specified.

Any text that matches leadingRegex or trailingRegex is not returned. An exception is thrown if the string match fails.
find_page_results_tuple( text )
Returns a string three-tuple of the three values found in text that are returned by matching the pattern "# - # of #" where # are positive integers or 0. An exception is thrown if the pattern match fails.