GeneralFunctions

From MozillaWiki
Jump to: navigation, search

Constants

data formats

  • RE_LONG_DATE - regular expression for a long date, ex: January 12, 2000.
  • STRF_LONG_DATE - formator for strptime to parse text into datetime object.
  • RE_COUNT - regular expression for a count including zero.
  • RE_NON_ZERO_COUNT - regular expression for a count greater that zero.

Verify Text in Pages

verifyText(str, sel)

Looks for text on the web-page

  • str - text to look for
  • sel - selenium instance
  • Returns True if str is found else False

verifyTextNotPresent(str, sel)

Looks for text on the web-page

  • str - text to look for
  • sel - selenium instance
  • Returns True if str is not found else False

verifyPageTitle(title_regexp, actual_title, sel)

Verify a page title with a regular expression

  • title_regexp - regular expression used in match
  • actual_title - text used in match
  • Returns True if actual_title matches the regular expression title_regexp, otherwise False.

Data Conversions

textToInteger(inText)

Searches text for an integer and converts to a number

  • inText - text which contains an integer, may include commas
  • Returns the tuple ( results , number )

If a number was found the returned tuple is the null string and the number as an integer. If the string contains multiple numbers only the first is returned. If no number was found the tuple is an error message string and 0.

longDateTextToDatetime(inText)

Searches text for a date in the 'long date' format and converts to a datetime data type.

  • inText - text which contains the date
  • Returns the tuple ( results, date )

If a date with a format similar to 'January 1, 2000' is found the returned tuple is the null string and the date as a datetime date type. If the string contains multiple dates of the format only the first is returned. If no date of the format was found the tuple is an error message string and the date January 1, 0001 as a datetime data type.

cleanNonAscii(inText)

Converts non-printable characters

  • inText - input text
  • Returns inText with any non-printable characters replaced with their character code, in the format #d; where d is the character code.
  • uses inText.encode('ascii', 'xmlcharrefreplace') to convert

equalURL(URL1, URL2)

Return True if two URLs are functionally equal

  • converts special characters to character code found in URLs (ex: space => %20) before testing if they are equal.

Exception Handling

saveException(exceptionKey, exceptionMsg, exceptionList, exceptionCounts)

Saves exception messages into exception list and exception type count

  • exceptionKey - type of exception. used as is a key into exceptionCounts dictionary
  • exceptionMsg - information about the particular exception, such as values and testcase breadcrumb
  • exceptionList - list of exceptions, assumed to be of type list. exceptionKey and exceptionMsg are concatenated together and appended to the list.
  • exceptionCounts - count of exception types, assumed to be a dictionary. the entry with a key of exceptionKey is incremented by 1. if an entry with the key does not exist, an entry is created as initialized to 1.

fatalException(exceptionKey, exceptionMsg, exceptionList, exceptionCounts)

Handle fatal exception

  • Calls saveException(). Prints last exception. Ends program by raising AssertionError.