Evan You пре 10 година
родитељ
комит
04b8a7533c

+ 1 - 0
build/nightwatch.config.js

@@ -2,6 +2,7 @@
 module.exports = {
   "src_folders": ["test/e2e/specs"],
   "output_folder": "test/e2e/reports",
+  "custom_commands_path": ["test/e2e/custom-commands"],
   "custom_assertions_path": ["test/e2e/custom-assertions"],
 
   "selenium": {

+ 18 - 0
test/e2e/custom-assertions/elementCount.js

@@ -0,0 +1,18 @@
+exports.assertion = function (selector, count) {
+  this.message = 'Testing if element <' + selector + '> has count: ' + count
+  this.expected = count
+  this.pass = function (val) {
+    return val === this.expected
+  }
+  this.value = function (res) {
+    return res.value
+  }
+  this.command = function (cb) {
+    var self = this
+    return this.api.execute(function (selector) {
+      return document.querySelectorAll(selector).length
+    }, [selector], function (res) {
+      cb.call(self, res)
+    })
+  }
+}

+ 18 - 0
test/e2e/custom-assertions/hasHTML.js

@@ -0,0 +1,18 @@
+exports.assertion = function (selector, html) {
+  this.message = 'Testing if element <' + selector + '> has HTML: ' + html
+  this.expected = html
+  this.value = function (res) {
+    return res.value
+  }
+  this.pass = function (val) {
+    return val.indexOf(html) > -1
+  }
+  this.command = function (cb) {
+    var self = this
+    return this.api.execute(function (selector) {
+      return document.querySelector(selector).innerHTML
+    }, [selector], function (res) {
+      cb.call(self, res)
+    })
+  }
+}

+ 27 - 0
test/e2e/custom-commands/waitFor.js

@@ -0,0 +1,27 @@
+var util = require('util')
+var events = require('events')
+
+var waitFor = function() {
+    events.EventEmitter.call(this)
+}
+
+util.inherits(waitFor, events.EventEmitter)
+
+waitFor.prototype.command = function(ms, cb) {
+    var self = this
+
+    ms = ms || 1000
+
+    setTimeout(function() {
+        // if we have a callback, call it right before the complete event
+        if (cb) {
+            cb.call(self.client.api)
+        }
+
+        self.emit('complete')
+    }, ms)
+
+    return this
+}
+
+module.exports = waitFor

+ 11 - 1
test/e2e/specs/markdown.js

@@ -3,7 +3,17 @@ module.exports = {
     browser
     .url('http://localhost:8080/examples/markdown/')
       .waitForElementVisible('#editor', 1000)
-      .assert.elementPresent('#editor')
+      .assert.value('textarea', '# hello')
+      .assert.hasHTML('#editor div', '<h1 id="hello">hello</h1>')
+      .setValue('textarea', '\n## foo\n\n- bar\n- baz')
+      // assert the output is not updated yet because of debounce
+      .assert.hasHTML('#editor div', '<h1 id="hello">hello</h1>')
+      .waitFor(500)
+      .assert.hasHTML('#editor div',
+        '<h1 id="hello">hello</h1>\n' +
+        '<h2 id="foo">foo</h2>\n' +
+        '<ul>\n<li>bar</li>\n<li>baz</li>\n</ul>'
+      )
       .end()
   }
 }