Skip to content

Instantly share code, notes, and snippets.

@ArsalanDotMe
Last active August 29, 2015 14:19
Show Gist options
  • Save ArsalanDotMe/4a83d8c95ee33bb73634 to your computer and use it in GitHub Desktop.
Save ArsalanDotMe/4a83d8c95ee33bb73634 to your computer and use it in GitHub Desktop.
Casper Test Case
casper.test.begin('Results are within price range', function (test) {
var MAX_PRICE = 5000, MIN_PRICE = 1000;
function getPrices() {
var priceNodes = document.querySelectorAll('.productinfo > h2');
return Array.prototype.map.call(priceNodes, function (e) {
var pt = e.textContent;
var num = /[\d,]+/.exec(pt);
var cleanNumber = num[0].replace(/,/g, '');
return Number(cleanNumber);
});
}
casper.start('http://www.dealreveal.pk/search/banana', function () {
test.assertExists('header-search > form');
this.sendKeys('input[name="priceLowLimit"]', String(MIN_PRICE));
this.sendKeys('input[name="priceUppLimit"]', String(MAX_PRICE));
this.click('button[ng-click="filterSearch()"]');
}).wait(5000, function () {
var prices = this.evaluate(getPrices);
var okay = true;
for (var i = prices.length - 1; i >= 0; i--) {
var p = prices[i];
this.echo(p);
if (p > MAX_PRICE || p < MIN_PRICE) {
okay = false;
}
};
test.assertEquals(okay, true, 'All prices within range');
}).run(function() {
test.done();
});
});
casper.test.begin('Test Next Page Render',4, function suite(test) {
casper.start('http://dealreveal.pk/search/iphone', function() {
test.assertExists('.pagination', "multiple pages exists");
test.assertExists('a.ng-binding',"Page Number 1");
test.assertEquals(this.click('.pagination li:last-child a'),true);
});
casper.then( function() {
this.captureSelector('screenshot.png', 'body');
var pageNumber = this.getHTML('ul.pagination > li.active > a');
test.assertEquals(pageNumber, "2");
});
casper.run(function() {
test.done();
});
});
//check for valid min and max range
casper.test.begin('Test Max Price should greater than Min Price', function (test){
var MAX_PRICE = 5000, MIN_PRICE = 1000;
casper.start('http://dealreveal.pk/search/samsung', function() {
test.assertExists('header-search > form');
this.sendKeys('input[name="priceLowLimit"]', String(MIN_PRICE));
this.sendKeys('input[name="priceUppLimit"]', String(MAX_PRICE));
}).wait(1000, function(){
var expected = false;
if(MAX_PRICE > MIN_PRICE){
expected = true;
}
test.assertEquals(expected, true, 'Maximum Price is greater than minimum price');
}).run(function() {
test.done();
});
});
//check for negative values
casper.test.begin('Price should not be negative value', function(test){
var MAX_PRICE = 5000, MIN_PRICE = -5;
casper.start('http://dealreveal.pk/search/samsung', function() {
test.assertExists('header-search > form');
this.sendKeys('input[name="priceLowLimit"]', String(MIN_PRICE));
this.sendKeys('input[name="priceUppLimit"]', String(MAX_PRICE));
}).wait(1000, function(){
var expected = false;
if(MAX_PRICE < 0 || MIN_PRICE < 0){
expected = false;
}
test.assertEquals(expected, true, 'Price cannot in negative');
}).run(function() {
test.done();
});
});
casper.test.begin('Search', function (test) {
var links = [];
function getLinks() {
var links = document.querySelectorAll('img');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('alt');
});
}
casper.start('http://dealreveal.pk/', function() {
test.assertUrlMatch(/^http:\/\/dealreveal.pk\//, 'Home URL = http://dealreveal.pk/');
test.assertExists('form[ng-submit="search()"]', "Search form Exists");
this.sendKeys('form[ng-submit="search()"] input[type="text"]', "Samsung" );
this.click('form[ng-submit="search()"] button[type="submit"]');
}).wait(5000, function() {
test.assertUrlMatch(/^http:\/\/dealreveal.pk\/search\/Samsung/, 'New URL = http://dealreveal.pk/search/Samsung');
test.assertEval(function() {
return __utils__.findAll("img").length >= 1;
}, "Search for Samsung retrieves atleast 1 product");
links = this.evaluate(getLinks);
}).run(function() {
this.echo(links.length + ' links found:');
this.echo(links.join('\n')).exit();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment