Monday, 9 September 2013

How to modularize CasperJS test steps?

How to modularize CasperJS test steps?

I am pretty new to CasperJS and have started to create a suite of tests.
Some of the test steps (like logging in to the application) will be
re-used a lot, so we would like to manage them in library files which we
include to our test files.
Plus, as we have multiple environments running (dev, integration,
production etc.) we need to parametrize the test steps for this, so it
shall be possible to pass parameters to the modules.
I researched the documentation and stackoverflow (I am aware there are
similar questions), but my Javascript skills are too limited obviously and
I was not able to get this up and running. This is my example test file:
// googletesting.js
casper.test.begin('Google search retrieves 10 or more results', 5,
function suite(test) {
casper.start("http://www.google.fr/", function() {
test.assertTitle("Google", "google homepage title is the one
expected");
test.assertExists('form[action="/search"]', "main form is found");
this.fill('form[action="/search"]', {
q: "casperjs"
}, true);
});
casper.then(function() {
test.assertTitle("casperjs - Recherche Google", "google title is
ok");
test.assertUrlMatch(/q=casperjs/, "search term has been submitted");
test.assertEval(function() {
return __utils__.findAll("h3.r").length >= 10;
}, "google search for \"casperjs\" retrieves 10 or more results");
});
casper.run(function() {
test.done();
});
});
And this is how it should like (or similar):
// googletesting2.js
casper.test.begin('Google search retrieves 10 or more results', 5,
function suite(test) {
doGoogleSearch('casperjs'); // pass a search term
doChecks();
casper.run(function() {
test.done();
});
});

No comments:

Post a Comment