dalekjs / dalek

[unmaintained] DalekJS Base framework
MIT License
695 stars 63 forks source link

Added low level API to use node promises and functions (redux) #99

Closed deedubbleyoo closed 10 years ago

deedubbleyoo commented 10 years ago

Re-implemented PR #35 to include feedback in comments, also cleaned up a few typos while I was there.

Updated implementation example to better show a page object-like pattern:

Page Object [login_page.js]

'use strict';

function LoginPage () {}

LoginPage.prototype = {

    email: function() {
        return '#login-email';
    },

    password: function () {
        return '#login-password';
    },

    submit: function () {
        return '#login-submit';
    },

    login: function() {
        return this.open('http://localhost:3000/login')
            .type(this.email(), 'user')
            .type(this.password(), 'secret')
            .click(this.submit)
            .waitForElement('#logoutNavLink');
    },

    logout: function() {
        // same implementation style as login
    }
};

module.exports LoginPage

The test is then abstracted away from DOM selection, creating separation of concerns:

Test Code

var LoginPage = require('../lib/login_page');

module.exports = {

    'customer can login and logout' : function(test){
        var loginPage = new LoginPage(); // if there is a way to do setup, this would go there
        test.start(loginPage.login())
            .assert.visible('#logoutNavLink')
            .assert.attr('#logoutNavLink img', 'alt', 'Logout')
            .andThen(loginPage.logout())// if there was a way to do teardown, this would go there
            .done();
    }
};

Adds node/promise to call regular node code

var db = require('db');

var LoginPage = require('../lib/login_page');

module.exports = {
    'has a customer profile': function(test){
        var loginPage = new LoginPage();
        test.node(function(done) {
            db.createTestUser(done);
        })
            .start(loginPage.login())
            .andThen(loginPage.verifyCustomerProfile()) //this would be a new entry in the page object
            .andThen(loginPage.logout())
            .done();
    }
};
deedubbleyoo commented 10 years ago

Thanks for the feedback, I've addressed the changes.

I've not really used JSDoc before, so it didn't jump out at me that they were wrong, sorry about that! Hopefully this is all good now and it will help those waiting for the functionality.