From 61028608e6ed2ef83544a457ece462eadfe63d6c Mon Sep 17 00:00:00 2001 From: Peter Cottle Date: Sun, 5 Apr 2015 22:27:36 -0700 Subject: [PATCH] sweet casper tests with keypresses --- src/__tests__/casperjs/casperUtils.js | 10 ++- src/__tests__/casperjs/command_type_test.js | 32 ++++++++++ .../casperjs/level_dropdown_keyboard_test.js | 46 ++++++++++++++ .../level_dropdown_page_keyboard_test.js | 62 +++++++++++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/casperjs/command_type_test.js create mode 100644 src/__tests__/casperjs/level_dropdown_keyboard_test.js create mode 100644 src/__tests__/casperjs/level_dropdown_page_keyboard_test.js diff --git a/src/__tests__/casperjs/casperUtils.js b/src/__tests__/casperjs/casperUtils.js index fe7460dc..ef8a6fb0 100644 --- a/src/__tests__/casperjs/casperUtils.js +++ b/src/__tests__/casperjs/casperUtils.js @@ -31,6 +31,13 @@ var CasperUtils = { this.test.done(); }, + enterCommand: function(command) { + return function then() { + this.sendKeys('#commandTextField', command, { keepFocus: true }); + this.page.sendEvent('keypress', this.page.event.key.Enter); + }; + }, + multiAssert: function() { // Copy the args into a variable we can reference inside // our closure. @@ -74,7 +81,8 @@ var CasperUtils = { selectorContainsText: function(selector, text) { return function then() { this.test.assertEvalEquals(function(selector) { - return document.querySelector(selector).innerText; + return document.querySelector(selector).innerText + .replace(/^\s+/g, '').replace(/\s+$/g, ''); }, text, 'Checking that selector "' + selector + '" contains "' + diff --git a/src/__tests__/casperjs/command_type_test.js b/src/__tests__/casperjs/command_type_test.js new file mode 100644 index 00000000..2d390cc0 --- /dev/null +++ b/src/__tests__/casperjs/command_type_test.js @@ -0,0 +1,32 @@ +var CasperUtils = require('./casperUtils').CasperUtils; + +casper.start( + CasperUtils.getUrl(), + function() { + this.test.assertTitle('Learn Git Branching'); + + casper.waitFor(CasperUtils.waits.jsMount) + .wait(300) + .then(CasperUtils.enterCommand('level intro1')) + .wait(1500) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView div h2', + 'Git Commits' + )) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.inside p', + "A commit in a git repository records a snapshot of all the files in your directory. It's like a giant copy and paste, but even better!" + )) + .then(CasperUtils.screenshot.entirePage) + .then(function() { + this.page.sendEvent('keypress', this.page.event.key.Escape); + }) + .wait(700) + .then(CasperUtils.screenshot.entirePage) + .then(CasperUtils.asserts.selectorContainsText( + 'div.levelNameWrapper', + "Level Introduction to Git Commits" + )) + .then(CasperUtils.testDone); + +}).run(); diff --git a/src/__tests__/casperjs/level_dropdown_keyboard_test.js b/src/__tests__/casperjs/level_dropdown_keyboard_test.js new file mode 100644 index 00000000..e4d9173d --- /dev/null +++ b/src/__tests__/casperjs/level_dropdown_keyboard_test.js @@ -0,0 +1,46 @@ +var CasperUtils = require('./casperUtils').CasperUtils; + +casper.start( + CasperUtils.getUrl(), + function() { + this.test.assertTitle('Learn Git Branching'); + + casper.waitFor(CasperUtils.waits.jsMount) + .wait(300) + .then(CasperUtils.enterCommand('levels')) + .wait(800) + .then(CasperUtils.screenshot.entirePage) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.toolbar', + 'Select a level' + )) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.displayName h3', + 'Introduction Sequence' + )) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.seriesView p', + "A nicely paced introduction to the majority of git commands" + )) + .then(function() { + this.page.sendEvent('keypress', this.page.event.key.Right); + }) + .wait(700) + .then(CasperUtils.screenshot.entirePage) + // Now we have selected the first level + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.seriesView p', + "Introduction to Git Commits" + )) + .then(function() { + this.page.sendEvent('keypress', this.page.event.key.Enter); + }) + .wait(700) + .then(CasperUtils.screenshot.entirePage) + .then(CasperUtils.asserts.selectorContainsText( + 'div.levelNameWrapper', + "Level Introduction to Git Commits" + )) + .then(CasperUtils.testDone); + +}).run(); diff --git a/src/__tests__/casperjs/level_dropdown_page_keyboard_test.js b/src/__tests__/casperjs/level_dropdown_page_keyboard_test.js new file mode 100644 index 00000000..9fa7b1c5 --- /dev/null +++ b/src/__tests__/casperjs/level_dropdown_page_keyboard_test.js @@ -0,0 +1,62 @@ +var CasperUtils = require('./casperUtils').CasperUtils; + +casper.start( + CasperUtils.getUrl(), + function() { + this.test.assertTitle('Learn Git Branching'); + + casper.waitFor(CasperUtils.waits.jsMount) + .wait(300) + .then(CasperUtils.enterCommand('levels')) + .wait(800) + .then(CasperUtils.screenshot.entirePage) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.toolbar', + 'Select a level' + )) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.displayName h3', + 'Introduction Sequence' + )) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.seriesView p', + "A nicely paced introduction to the majority of git commands" + )) + // Hit right 5 times + .then(function() { + this.page.sendEvent('keypress', this.page.event.key.Right); + }) + .then(function() { + this.page.sendEvent('keypress', this.page.event.key.Right); + }) + .then(function() { + this.page.sendEvent('keypress', this.page.event.key.Right); + }) + .then(function() { + this.page.sendEvent('keypress', this.page.event.key.Right); + }) + .then(function() { + this.page.sendEvent('keypress', this.page.event.key.Right); + }) + .wait(300) + .then(CasperUtils.screenshot.entirePage) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.displayName h3', + "Push & Pull -- Git Remotes!" + )) + .then(CasperUtils.asserts.selectorContainsText( + 'div.modalView.inFront div.seriesView p', + "Clone Intro" + )) + .then(function() { + this.page.sendEvent('keypress', this.page.event.key.Enter); + }) + .wait(700) + .then(CasperUtils.screenshot.entirePage) + .then(CasperUtils.asserts.selectorContainsText( + 'div.levelNameWrapper', + "Level Clone Intro" + )) + .then(CasperUtils.testDone); + +}).run();