forms.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. casper.test.begin('Forms', 13, function (test) {
  2. casper
  3. .start('./fixtures/forms.html')
  4. .then(function () {
  5. // test initial value binding
  6. test.assertField('text', 'some text')
  7. test.assertField('checkbox', true)
  8. test.assertField('radio', 'b')
  9. test.assertField('select', 'b')
  10. // multiple select's value only returns first selected option
  11. test.assertField('multi', 'a')
  12. // manually retrieve value
  13. test.assertEvalEquals(function () {
  14. var s = document.querySelector('[name="multi"]'),
  15. opts = s.options
  16. return Array.prototype.filter.call(opts, function (o) {
  17. return o.selected
  18. }).map(function (o) {
  19. return o.value || o.text
  20. })
  21. }, ['a', 'c'])
  22. test.assertField('textarea', 'more some text')
  23. })
  24. .then(function () {
  25. this.fill('#forms', {
  26. 'text': 'changed text',
  27. 'checkbox': false,
  28. 'radio': 'a',
  29. 'select': 'a',
  30. 'textarea': 'more changed text'
  31. })
  32. // Sadly casper doesn't have modifier for clicks
  33. // manually select values and emit a change event...
  34. this.evaluate(function () {
  35. var s = document.querySelector('[name="multi"]'),
  36. o = s.options
  37. s.selectedIndex = -1
  38. o[1].selected = true
  39. o[3].selected = true
  40. var e = document.createEvent('HTMLEvents')
  41. e.initEvent('change', true, true)
  42. s.dispatchEvent(e)
  43. })
  44. })
  45. .then(function () {
  46. test.assertSelectorHasText('.text', 'changed text')
  47. test.assertSelectorHasText('.checkbox', 'false')
  48. test.assertSelectorHasText('.radio', 'a')
  49. test.assertSelectorHasText('.select', 'a')
  50. test.assertSelectorHasText('.multipleSelect', '["b","d"]')
  51. test.assertSelectorHasText('.textarea', 'more changed text')
  52. })
  53. .run(function () {
  54. test.done()
  55. })
  56. })