mardi 21 avril 2015

Selenium npm auto test runner

Ahhhh this is driving me crazy.

I have got my windows machine setup with selenium-webdriver from npm.

I have my first test running nicely but then the obvious next step is to split out all the tests into different files and folders to allow growth and then all of sudden you need a test runner... but then there seems to be 100's of test runners, mocha, chai, selenium-test-runner, selenium-mocha... the list goes on and on.

I was hoping someone might point me in the right direction.

What is the most common stack used? I simply want to be able to build up a folder of tests and run against a remote website on my local machine.. eg run a single file via npm start on my local machine which in turn steps through each test in the tests folder one by one.

Any tips would be hugely appreciated.

I did try putting something together myself, each test file was a module.export which in turn loaded up a var webdriver = require('selenium-webdriver'); & var driver = new webdriver.Builder().forBrowser('firefox').build(); But even with a driver.quit() at the end of each test i was left with multiple firefox windows open until the last test completed, which when the tests reach med to lrg qty's i'm guessing that is going to kill my machine.

//get the test testConstants cached into a var to later pass to the test files.
GLOBAL.testConstants = require('./config/testConstants');

//walk files function
function filesWalk(currentDirPath, callback) {
    var fs = require('fs'), path = require('path');
    fs.readdirSync(currentDirPath).forEach(function(name) {
        var filePath = path.join(currentDirPath, name);
        var stat = fs.statSync(filePath);
        if (stat.isFile()) {
            callback(filePath, stat);
        } else if (stat.isDirectory()) {
            walk(filePath, callback);
        }
    });
}

//build up list of test files
var testFunctions = [];
filesWalk('tests/', function(filePath, stat) {
    // do something with "filePath"...
    if( filePath.slice(-3) == '.js' ){
        //pass function to testFiles array which would include the test then run the file
        console.log('pushing test to array: ' + filePath);
        //this array of functions will get passed onto waterfall
        testFunctions.push( function( callback ){
            console.log('Running test: '+ filePath);
            require( './' + filePath.substring(0, filePath.length - 3))( callback );
        } );
    }
});


//now pass the array of test function to waterfall async
var waterfall = require('async-waterfall');
waterfall( testFunctions , function(err,result){
    console.log( err );
    console.log( result );
} );

With an example test looking like:

module.exports = function( nextTest ){

    //Load up the rel modules and call the By and until into easily accessible variables
    var webdriver = require('selenium-webdriver');

    //start the driver
    var driver = new webdriver.Builder().forBrowser('firefox').build();

    //Load up the rel modules and call the By and until into easily accessible variables
    var By = webdriver.By,
        until = require('selenium-webdriver').until,
        testConstants = GLOBAL.testConstants;

    driver.get( testConstants.url );
    driver.findElement(By.css('.map-submit-button')).click();
    driver.wait(function () {
        return driver.isElementPresent(By.css("a#map-tab"));
    }, testConstants.timeout).then(function(){
        driver.findElement( By.css(".search-tab-container .selected") ).click().then(function(){
            driver.quit();
            if( typeof nextTest == 'function' ){
                nextTest( webdriver, driver );
            }
        });
    });
};

Aucun commentaire:

Enregistrer un commentaire