diff --git a/.gitignore b/.gitignore index f356293..a72b52e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ logs results npm-debug.log +node_modules diff --git a/README.md b/README.md index f7edb78..11c3a91 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,42 @@ wd-query ======== + +jQuery style selectors for [wd](). + +### tl;dr + +In selenium tests with wd, wouldn't it be nice if you could do this: + +```javascript +browser.init() + .then(function(){ + return $('#email-input').val('you@something.io'); + }) + .then(function(){ + return $('#password-input').val('some_password'); + }) + .then(function(){ + return $('#login-form').submit(); + }) + .then(function(){ + return $('body').hasClass('loggedIn'); + }) + .then(function(body){ + assert.ok(body); + browser.quit(); + }); +``` + +### Why jQuery + +Because selector's make sense and so does chaining. Approaching selenium +webdriver interactions as starting with selectors makes it easy to combine +multi-step sequences into terse and expressive statements. + +### Why wd promises? + +Because the nested callback style is unruly for anything complex. Promises are +also portable--meaning they can be passed around between functions and scopes. + +By using the wd promise interface wd-query is able to chain multiple async calls +into a unified syntax. diff --git a/index.js b/index.js new file mode 100644 index 0000000..3c79e80 --- /dev/null +++ b/index.js @@ -0,0 +1,41 @@ +module.exports = function(browser){ + + var wdQuery = function( selector ){ + if( !(this instanceof wdQuery) ){ + return new wdQuery(selector); + } + + this.selector = selector; + return this; + }; + + var $ = browser.elementByCssSelector; + + wdQuery.fn = wdQuery.prototype = { + + get: function(selector){ + selector = selector || this.selector; + return browser.elementByCssSelector(selector); + }, + + val: function(value){ + return this.get() + .then(function(elem){ + return value ? elem.type(value) : elem.getValue(); + }); + }, + + click: function(){ + return this.get() + .then(function(elem){ + elem.click(); + }); + } + + }; + + return function(selector, context){ + return new wdQuery(selector, context); + }; + +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..f69b381 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "wd-query", + "version": "0.0.0", + "description": "A jQuery-esue wrapper for wd", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git@github.com:wookiehangover/wd-query.git" + }, + "keywords": [ + "wd", + "selenium", + "automation" + ], + "author": "wookiehangover", + "license": "MIT", + "bugs": { + "url": "https://github.com/wookiehangover/wd-query/issues" + }, + "devDependencies": { + "grunt": "~0.4.1", + "grunt-mocha-selenium": "~0.4.0" + } +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..e69de29