QA/Mozmill Test Automation/Test Modules Refactor/Style Guide: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
Line 1: Line 1:
== Mozmill Style Guide v0.1 ==
== Mozmill Style Guide v0.1 ==
Goal: Write clear code
Goal:  
* Write clear code
* Please use good coding practice - see http://neil.rashbrook.org/JS.htm


Exceptions: Rules can be broken or modified, however, be prepared to defend your position
Exceptions to the Rule:  
* If you don't agree with something in this styleguide, or you think something could be done "better", be prepared to explain why
* The styleguide is not written in stone and is open to improvements


Lines of code should not exceed 80 characters (or thereabouts)
Rules:
* Lines of code should not exceed 80 characters (or thereabouts)
* sleep() should never be used in a test, unless absolutely necessary -- try to use waitFor() instead
* Each test file should only contain a single test
* When performing an action on an element, be sure to declare an instance of the element first
* Coding flow should follow a template:
** MPL > REQUIRES > CONSTANTS > SETUP_MODULE > TEARDOWN_MODULE > TEST
* Use Array.forEach() to iterate array elements
* Use for() to iterate string characters
* License Block
** If you create a test
*** Initial Author = The Mozilla Foundation, if you are a Mozilla employee
*** Initial Author = YOUR NAME, if you are not a Mozilla employee
** If you are contributing to a test
*** Add your name and email to the bottom of the contributors list<br /><pre>*  Firstname Lastname <email@domain.com></pre>
* Error messages should be positive in nature
** GOOD: Expected the notification bar to be visible
** BAD: Didn't find a notification bar
* There should be no whitespace at the end of a line
* In general, all lines should be indented 2 spaces from their parent
* All files should have 1 newline, no more, no less
* If an XPath string needs to be concatenated, split the string on a /
* If anything needs concatenation:
** the operator should trail the line with one space
** each line should be left-aligned with the first
* All functions should be named and use camelCase with the opening brace trailing the signature by a single space
* All variables should be named using camelCase
* All constants should be named using ALL_CAPS
* All unchanging variables/values should be declared as constant
* All components should be declared in setupModule()
** If it needs concatenation, split on the . leaving it on the first line
** ''Ideally, necessary component loading should be handled by the appropriate API''
* Commenting code should be done inline //
* Commenting functions should be done using [http://jsdoc.sourceforge.net/#tagref JSDoc Toolkit] style


=== Variables ===
== Refactor ==
; Local variables
* Replace gDelay and 100 with DELAY
* Use camelCase when naming local variables and functions.
* Replace gTimeout and 5000 with TIMEOUT
<pre>
* Replace other timeouts with CONSTANTS
  var someVariable = value;
* Replace sleep() parameter with CONSTANT
</pre>
* Replace unchanging values with CONSTANT
 
* Move tests from testGeneral into appropriate subfolder
; Constants
* Encapsulate all test pages in a LOCAL_TEST_PAGES array
* Use ALL_CAPS when naming all constants
* Replace failing waitForEval() with waitFor()
<pre>
* Make sure proper use of "The Mozilla Foundation" in tests
  const SOME_CONSTANT_VARIABLE  = 'some value';
* Make sure contributor list is aligned on N in Contributor(s)
</pre>
* Make sure assertion messages are positive
 
* Remove any trailing whitespace
; Unchanging Strings & Numbers
* Make sure all tests have 1 newline EOF
* Any specific strings or numbers should be declared as constants
* Make sure all lines of code respect the 2-space indent rule
* In other words, don't use bare strings or numbers in functional code
<pre>
  const SOME_STRING = 'the string';
    ...
  controller.doSomething(SOME_STRING);
</pre>
 
=== Commenting ===
; Commenting of functional code
* Single line comment describing the purpose of the code
* Comment should be left-aligned with the code block
<pre>
  // Download a file
  downloads.downloadFileOfUnknownType(controller, URL);
</pre>
 
; Commenting of functions
* All functions should be preceded with a JSDoc style comment block
* This should include any references to parameters and returns
<pre>
  /**
  * Clear a user set preference
  *
  * @param {string} prefName
  *        The user-set preference to clear
  *
  * @return False if the preference had the default value
  * @type boolean
  **/
  clearUserPref : function preferences_clearUserPref(prefName) {
</pre>
* See the [http://jsdoc.sourceforge.net/#tagref JSDoc Toolkit] for further information about commenting style
 
=== Functions ===
; Function naming
* All functions should be named
* All function names should use camelCase
* Opening brace should follow the function signature by a single space on the same line
<pre>
  function someFunctionToPerform(param1, paramN) {
</pre>
 
; Parameter Concatenation
* If a parameter list is too long, it should be concatenated
* The operator (+) should follow the line by a single space
* The string sections should be aligned to the left
<pre>
  var someVariable = controller.someFunction('some' +
                                            'concatenated' +
                                            'string');
</pre>
* If the string is an XPath or contains '/', split on the '/'
<pre>
  var someVariable = controller.someFunction('/some/concatenated' +
                                            '/XPath/string');
</pre>
 
 
=== Assertions ===
; waitFor()
* Use whenever you are waiting for a particular element to appear
<pre>
  controller.waitFor(function () {
    return someProperty == 'some value';
  }, "A positive error message about the expected state");
</pre>


== To be Added ==
== To be Added ==

Revision as of 02:34, 19 November 2010

Mozmill Style Guide v0.1

Goal:

Exceptions to the Rule:

  • If you don't agree with something in this styleguide, or you think something could be done "better", be prepared to explain why
  • The styleguide is not written in stone and is open to improvements

Rules:

  • Lines of code should not exceed 80 characters (or thereabouts)
  • sleep() should never be used in a test, unless absolutely necessary -- try to use waitFor() instead
  • Each test file should only contain a single test
  • When performing an action on an element, be sure to declare an instance of the element first
  • Coding flow should follow a template:
    • MPL > REQUIRES > CONSTANTS > SETUP_MODULE > TEARDOWN_MODULE > TEST
  • Use Array.forEach() to iterate array elements
  • Use for() to iterate string characters
  • License Block
    • If you create a test
      • Initial Author = The Mozilla Foundation, if you are a Mozilla employee
      • Initial Author = YOUR NAME, if you are not a Mozilla employee
    • If you are contributing to a test
      • Add your name and email to the bottom of the contributors list
        *   Firstname Lastname <email@domain.com>
  • Error messages should be positive in nature
    • GOOD: Expected the notification bar to be visible
    • BAD: Didn't find a notification bar
  • There should be no whitespace at the end of a line
  • In general, all lines should be indented 2 spaces from their parent
  • All files should have 1 newline, no more, no less
  • If an XPath string needs to be concatenated, split the string on a /
  • If anything needs concatenation:
    • the operator should trail the line with one space
    • each line should be left-aligned with the first
  • All functions should be named and use camelCase with the opening brace trailing the signature by a single space
  • All variables should be named using camelCase
  • All constants should be named using ALL_CAPS
  • All unchanging variables/values should be declared as constant
  • All components should be declared in setupModule()
    • If it needs concatenation, split on the . leaving it on the first line
    • Ideally, necessary component loading should be handled by the appropriate API
  • Commenting code should be done inline //
  • Commenting functions should be done using JSDoc Toolkit style

Refactor

  • Replace gDelay and 100 with DELAY
  • Replace gTimeout and 5000 with TIMEOUT
  • Replace other timeouts with CONSTANTS
  • Replace sleep() parameter with CONSTANT
  • Replace unchanging values with CONSTANT
  • Move tests from testGeneral into appropriate subfolder
  • Encapsulate all test pages in a LOCAL_TEST_PAGES array
  • Replace failing waitForEval() with waitFor()
  • Make sure proper use of "The Mozilla Foundation" in tests
  • Make sure contributor list is aligned on N in Contributor(s)
  • Make sure assertion messages are positive
  • Remove any trailing whitespace
  • Make sure all tests have 1 newline EOF
  • Make sure all lines of code respect the 2-space indent rule

To be Added

13. Component declarations should be indented in line with the parent 15. Use waitFor() as much as possible. Only use sleep() when a waitFor() inappropriate or impossible

  • 16,17. DELAY/TIMEOUT
    - if 100/5000, remove
    - sleep() use a global const
    - other values use a global const
    * for refactoring only, not in style guide

18/19 - leave out of style guide

    * not needed in refactor

20. Dont use for each() in arrays - strings use traditional for()

  • 21. no testGeneral subfolder
    * refactoring only

22. Only one test per file

    - ok for style guide, ignore for refactoring

23. Element before action 24. Local test pages encapsulated in Array 25. Constants between requires and setupModule()

         - MPL > REQ > CONST > SETUP > TEARDOWN > TEST
         - put into a template

27. Use waitFor() when asserting for timeouts

         - leave out of style guide for now
         - refactoring: replace waitForEval() if failing

28. Use traditional for() when iterating characters of a string 29. License block

         - Initial Author: community member (Employee: Mozilla Foundation)
         - Everyone in contributor
         - contributor list in order of contribution

31. waitFor() & assert() error messages

         * refactor: ensure messages are positive
         - style guide: generally mention that error messages should be positive

32. Trailing whitespace

    - no trailing whitespace on a line
    - 2-space indent in general
    - 1 newline at EOF
         
  • Please use good coding practice
  • Reference http://neil.rashbrook.org/JS.htm
  • Point is clear code
  • If style guide is "wrong", be prepared to explain why
  • Style guide is modular and modifiable