Permalink
Browse files

Initial Code Commit

  • Loading branch information...
1 parent c4bc68c commit 0c1496a5ede6bd929cace0983bc4d5cc59204220 Douglas Martin committed Jan 10, 2011
Showing with 694 additions and 0 deletions.
  1. 0 README
  2. +198 −0 README.md
  3. +57 −0 format/jsonFormat.js
  4. +1 −0 index.js
  5. +121 −0 src/runner.js
  6. +19 −0 src/sodaWrapper.js
  7. +59 −0 src/util/parser.js
  8. +55 −0 test.js
  9. +96 −0 test/runner.js
  10. +42 −0 test/testFiles/externalCommand.json
  11. +36 −0 test/testFiles/googleTest.json
  12. +10 −0 test/testHelper.js
View
0 README
No changes.
View
198 README.md
@@ -0,0 +1,198 @@
+
+# soda-runner
+
+ A runner for SodaJS to allow developers to define tests in SelniumIDE using the formatter located in /formatter.
+ The formatter allows one to save tests between the IDE into a corresponding json file that soda-runner can run.
+
+## Runner Options
+
+ /*testDir - the base directory where your tests are located
+ client - the client to create 'client' or 'sauceClient'
+ port - defaults to 4444
+ host - the location of the rc server defaults to locahost
+ url - the url of the site your are testing
+ browserType - ie firefox
+ baseUrl - the url to open the browser to initially
+ mimeTypes - supported mimetype, do not change this, here to support multiple times in the future*/
+
+ var runner = new Runner(options);
+
+## Native Commands
+
+googleTest.json
+
+ [
+ {
+ "command":"type",
+ "target":"q",
+ "value":"hello world"
+ },
+ {
+ "command" : "setSpeed",
+ "target" : "1000"
+ },
+ {
+ "command":"click",
+ "target":"btnG",
+ "value":""
+ },
+ {
+ "command" : "setSpeed",
+ "target" : "10"
+ },
+ {
+ "command":"waitForElementPresent",
+ "target":"sflas",
+ "value":""
+ },
+ {
+ "command":"clickAndWait",
+ "target":"sflas",
+ "value":""
+ },
+ {
+ "command":"assertValue",
+ "target":"as_q",
+ "value":"hello world"
+ }
+ ]
+
+test.js
+
+ var Runner = require('../index.js'),
+ var runner = new Runner({url : "http://www.google.com",browserType : 'firefox', testDir : __dirname + "/tests"});
+ var tests = runner.run("all", ["googleTest"]);
+
+## External Commands
+
+externalCommands.json
+
+ [
+ {
+ "command":"type",
+ "target":"q",
+ "value":"hello world"
+ },
+ {
+ "command" : "setSpeed",
+ "target" : "1000"
+ },
+ {
+ "command":"click",
+ "target":"btnG",
+ "value":""
+ },
+ {
+ "command" : "and",
+ "file" : "../../test/testHelper",
+ "fun" : "assertTitle",
+ "args" : ["hello world"]
+ },
+ {
+ "command":"waitForElementPresent",
+ "target":"sflas",
+ "value":""
+ },
+ {
+ "command" : "setSpeed",
+ "target" : "10"
+ },
+ {
+ "command":"clickAndWait",
+ "target":"sflas",
+ "value":""
+ },
+ {
+ "command":"assertValue",
+ "target":"as_q",
+ "value":"hello world"
+ }
+ ]
+
+
+
+## testHelper.js
+
+ exports.assertTitle = function(title)
+ {
+ return function(browser) {
+ browser.getTitle(function(title) {
+ assert.ok(~title.indexOf(title), 'Title did not include the query');
+ });
+ }
+ }
+
+test.js
+
+ var Runner = require('../index.js'),
+ var runner = new Runner({url : "http://www.google.com",browserType : 'firefox', testDir : __dirname + "/testFiles"});
+ var tests = runner.run("all", ["externalCommand"]);
+
+To run all tests in a directory
+
+ var runner = new Runner({url : "http://www.google.com",browserType : 'firefox', testDir : __dirname + "/testFiles"});
+ var tests = runner.run("all");
+
+To run all tests in a directory
+
+ var runner = new Runner({url : "http://www.google.com",browserType : 'firefox', testDir : __dirname + "/testFiles"});
+ var tests = runner.run("all", ["googleTest", "externalCommands"]);
+
+If you want to define a particular suite, create a directory in your base test directory, i.e:
+
+ - Tests
+ - internal
+ - googleTest.json
+ - googleTest2.json
+ - googleTest3.json
+ - external
+ - externalCommands.json
+
+To run the internal suite
+
+ var runner = new Runner({url : "http://www.google.com",browserType : 'firefox', testDir : __dirname + "/testFiles"});
+ var tests = runner.run("internal");
+
+To run a particular tests in a suite
+ var runner = new Runner({url : "http://www.google.com",browserType : 'firefox', testDir : __dirname + "/testFiles"});
+ var tests = runner.run("internal", ["googleTest, "googleTest3]);
+
+
+## Command line
+
+ node test.js suite=all tests=test1,test2,.... testDir=locationOfTests url=url browser=browserType
+
+executing inner suites
+
+ node test.js suite=suite.innerSuite tests=test1,test2,.... testDir=locationOfTests url=url browser=browserType
+
+
+## More Information
+
+ - [SodaJS](https://github.com/learnboost/soda)
+
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2010 LearnBoost <dev@learnboost.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,57 @@
+/**
+ * Helper for developers to copy JSON from selenium,
+ * add this format to Selenium IDE.
+ */
+
+
+/**
+ * Parse source and update TestCase. Throw an exception if any error occurs.
+ *
+ * @param testCase TestCase to update
+ * @param source The source to parse
+ */
+function parse(testCase, source) {
+ source = JSON.parse(source);
+ var commands = [];
+ for(var i in source){
+ var obj = source[i];
+ if(typeof obj == 'object'){
+ for(var j in obj){
+ var cmd = obj[j];
+ var command = new Command();
+ command.command = j;
+ command.target = cmd.target;
+ command.value = cmd.value;
+ commands.push(command);
+ }
+ }
+ }
+ testCase.commands = commands;
+ return testCase;
+}
+
+/**
+ * Format an array of commands to the snippet of source.
+ * Used to copy the source into the clipboard.
+ *
+ * @param The array of commands to sort.
+ */
+function formatCommands(commands) {
+ var cmds = [], len = commands.length;
+ for (i = 0; i < len; i++) {
+ var cmd = commands[i];
+ cmds.push({command : cmd.command, target : cmd.target, value : cmd.value});
+ }
+ return JSON.stringify(cmds, null, 4);
+}
+
+/**
+ * Format TestCase and return the source.
+ * The 3rd and 4th parameters are used only in default HTML format.
+ *
+ * @param testCase TestCase to format
+ * @param name The name of the test case, if any. It may be used to embed title into the source.
+ */
+function format(testCase, name) {
+ return formatCommands(testCase.commands);
+}
View
@@ -0,0 +1 @@
+exports = module.exports = require("./src/runner");
View
@@ -0,0 +1,121 @@
+var soda = require('./sodaWrapper').soda,
+ path = require("path"),
+ assert = require('assert'),
+ parser = require('./util/parser'),
+ fs = require('fs');
+
+soda.addCommand('wait');
+soda.addCommand('assertTextNotPresent');
+soda.addCommand('refreshAndWait');
+
+
+var resolvePath = function(testDir, suite, test) {
+ return testDir + '/' + suite + '/' + test + '.json';
+}
+
+var getAllTests = function(dirName, mimeTypes) {
+ var tests = [];
+
+ function canParse(fileName) {
+ var ret = false, indexOf = fileName.lastIndexOf(".");
+ indexOf > 0 && (ret = fileName.slice(indexOf + 1));
+ return ret;
+ }
+
+ if (fs.statSync(dirName).isDirectory()) {
+ /* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */
+ var files = fs.readdirSync(dirName);
+ for (var i = 0; i < files.length; i++) {
+ var currFileName = dirName + "/" + files[i],
+ currFile = fs.statSync(currFileName);
+ if (currFile.isDirectory()) {
+ tests = tests.concat(getAllTests(currFileName));
+ } else if (canParse(currFileName)) {
+ tests.push(currFileName);
+ }
+ }
+ }
+ return tests;
+}
+
+var runner = exports = module.exports = function Runner(args) {
+ var props = {}, args = args || {};
+ props.testDir = args.testDir || __dirname;
+ props.client = args.client || "createClient";
+ props.port = args.port || 4444;
+ props.host = args.host || "localhost";
+ props.url = args.url || "http://{host}".replace("{host}", props.host);
+ props.browserType = args.browserType || 'firefox';
+ props.baseUrl = args.baseUrl || '/';
+ props.mimeTypes = args.mimeTypes || { json : 1};
+ function defineSetGet(prop) {
+ this.__defineGetter__(prop, function() {
+ return props[prop]
+ })
+ this.__defineSetter__(prop, function(val) {
+ props[prop] = val;
+ })
+ }
+
+ for (var i in props) {
+ defineSetGet.apply(this, [i]);
+ }
+ this.__defineGetter__("browser", function() {
+ var browser;
+ if (soda[this.client]) {
+ browser = soda[this.client]({
+ host: this.host,
+ port: this.port,
+ url: this.url,
+ browser: this.browserType
+ });
+ // Log commands as they are fired
+ browser.on('command', function(cmd, args) {
+ console.log(' \x1b[33m%s\x1b[0m: %s', cmd, args.join(', '));
+ });
+ } else {
+ throw new Error("Client of type '" + client + "' is not supported!");
+ }
+ return browser;
+ });
+}
+
+runner.prototype.run = function(suite, tests) {
+ if (suite) {
+ var testFiles = [];
+ if (suite && suite instanceof Array) {
+ suite.forEach(function(s) {
+ testFiles = testFiles.concat(getAllTests(this.testDir + '/' + s, this.mimeTypes));
+ });
+ } else if (tests && tests.length == 0 || tests == 'all') {
+ testFiles = getAllTests(this.testDir + '/' + suite, this.mimeTypes);
+ } else if (tests && tests.length && suite != "all") {
+ testFiles = tests.map(function(t) {
+ return resolvePath(this.testDir, suite, t);
+ }, this);
+ } else if (tests && tests.length && suite == "all") {
+ testFiles = tests.map(function(t) {
+ return this.testDir + "/" + t + ".json"
+ }, this);
+ } else {
+ testFiles = getAllTests(this.testDir, this.mimeTypes)
+ }
+ if (testFiles.length) {
+ var browser = this.browser;
+ browser.chain.session()
+ .windowMaximize()
+ .open(this.baseUrl);
+ parser.parse(testFiles, browser);
+ browser.testComplete()
+ .end(function(err) {
+ if (err) console.error(err)
+ else console.log("All tests passed");
+ });
+ } else {
+ throw new Error("No tests found")
+ }
+ } else {
+ throw new Error("No suite defined")
+ }
+ return testFiles;
+};
Oops, something went wrong.

0 comments on commit 0c1496a

Please sign in to comment.