Test.Functional

MVC.Test.Functional, Action, Blur, Change, Click, Contextmenu, Dblclick, Drag, Keyup, Keydown, Keypress, Mousedown, Mousemove, Mouseout, Mouseup, Select, Submit, Write

Related

Test.Assertions, Test.Unit, Test, Test.Controller

Functional tests are used to mimic user events.

Constructor

MVC.Test.Functional

new MVC.Test.Functional(name, tests) -> Test

Creates a new functional test case. A test case is a collection of test functions and helpers.

new MVC.Test.Functional('TestCaseName',{
  test_some_clicks : function(){
    this.Click('#button')
  }
})
- The unique name of the test. Make sure no two tests have the same name.
- An object with test functions. Functions that begin with test_ will be run as tests. Functions that don't begin with test are converted to helper functions. Do not name helper functions the same name as the test provided helpers and assertions such as assert or assertEqual as your functions will override these functions.

Inherited Methods

Test

fail, helpers, run, run_helper, run_test, toElement

Assertion Helpers

Action

Action(event_type, selector_or_element, eventoptions_or_number)

Creates a syntetic event on a HTMLElement.

this.Action('click','.todo',3) //calls a click on the third element with class '.todo'
- A lowercase event type. One of ['change', 'click', 'contextmenu', 'dblclick', 'keyup', 'keydown', 'keypress','mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'submit', 'focus', 'blur','drag', 'write'].
- An HTMLElement or a CSS selector.
- If a number is provided, it uses it to select that number from the array of elements returned by doing a CSSQuery with selector. If an object is provided, it passes those options to the SyntheticEvent for creation. If nothing is provided, the number defaults to 0.

Blur

Blur(selector_or_element, options_or_number)

Calls Action using 'blur' as the first parameter.

Change

Change(selector_or_element, options_or_number)

Calls Action using 'change' as the first parameter.

Click

Click(selector_or_element, options_or_number)

Calls Action using 'click' as the first parameter.

Contextmenu

Contextmenu(selector_or_element, options_or_number)

Calls Action using 'contextmenu' as the first parameter.

Dblclick

Dblclick(selector_or_element, options_or_number)

Calls Action using 'dblclick' as the first parameter.

Drag

Drag(selector_or_element, options)

Creates events that simulate a drag motion across the browser. The drag events are comprised of a mousedown from the from location, equal spaced mousemoves to the to location, and a mouseup at the to location. Drag is by default syncronous, but can be made asyncronous by providing a duration option.


test_drag_to_trash : function(){
  this.Drag('#/drag_handle', 
    {
      from: '/drag_handle',
      to: '/trash', 
      duration: 2, 
      callback: this.next_callback()
    })
},
make_sure_drag_worked : function(){
  this.assertNull(document.getElementById('/drag_handle'));
}

options

OptionDefaultDescription
callback null A callback that gets called when the drag motion has completed. This is only necessary with asyncronous drags. Typically, the callback can be provided by next_callback();
duration null The length of time in seconds the drag takes place. By setting this option, the events are dispatched in intervals, letting the browser update the screen. Typically, you want to provide a callback to test the results of the drag after completion.
from null The starting coordinates of the drag. This can be specified in three ways: first, as an object like {x: 123, y: 321}; second, as a HTMLElement; third, as the string ID of a HTMLElement. Providing an element or the id of an element, Drag will use the center of the element as is starting position.
steps 100 The number of mousemoves called to represent the drag. This parameter is void if duration is provided.
to null The ending coordinates of the drag. Use the to option the same way as the from option.

Keyup

Keyup(selector_or_element, options_or_number)

Calls Action using 'keyup' as the first parameter.

Keydown

Keydown(selector_or_element, options_or_number)

Calls Action using 'keydown' as the first parameter.

Keypress

Keypress(selector_or_element, character, options_or_number)

Calls Action using 'keypress' as the first parameter. In browsers other that Firefox, keypress support built into the browser doesn't actually write text into the input element. To make up for this shortcoming, this method manually inserts text into the input element's value attribute.

- The character to be written to the passed element, '\b' causes a character to be deleted, '\n' simulates pressing enter

options

OptionDefaultDescription
character false True to simulate pressing the alt key.
ctrlKey false True to simulate pressing the control key.
altKey false True to simulate pressing the alt key.
shiftKey false True to simulate pressing the shift key.
metaKey false True to simulate pressing the meta key.
keyCode 0 Used to simulate pressing other keys, this reference shows a list of keyCodes you can use.

Mousedown

Mousedown(selector_or_element, options_or_number)

Calls Action using 'mousedown' as the first parameter.

Mousemove

Mousemove(selector_or_element, options_or_number)

Calls Action using 'mousemove' as the first parameter.

Mouseout

Mouseout(selector_or_element, options_or_number)

Calls Action using 'mouseout' as the first parameter.

Mouseover

Mouseover(selector_or_element, options_or_number)

Calls Action using 'mouseover' as the first parameter.

Mouseup

Mouseup(selector_or_element, options_or_number)

Calls Action using 'mouseup' as the first parameter.

Select

Select(selector_or_element, options_or_number)

Calls Action using 'select' as the first parameter.

Submit

Submit(selector_or_element, options_or_number)

Calls Action using 'submit' as the first parameter.

Write

Write(selector_or_element, options_or_string)

Creates events that simulate writing into an input element. If a callback option is used in the second parameter, the event is asynchronous. Otherwise, it is synchronous.

// syntax 1: synchronous version
this.Write(input_params.element, 'Brian');
// syntax 2: asynchronous version
this.Write(input_params.element, {text: 'Brian', callback: this.next_callback()});

options_or_string

If a string is passed, it is the text written in the passed input element.

OptionDefaultDescription
callback null A callback that gets called when the write has completed. This is only necessary with asyncronous writes. Typically, the callback can be provided by next_callback();
duration null The length of time in seconds the write takes place. By setting this option, the events are dispatched in intervals, letting the browser update the screen. Typically, you want to provide a callback to test the results of the drag after completion.
text null The text to be written into the given input element.
'\b' - the backspace character is used to delete one character of text
'\n' - the newline character is used to simulate pressing enter