Skip to content

Instantly share code, notes, and snippets.

@lf94
Created April 2, 2015 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lf94/e4fc5fb299acc7289c43 to your computer and use it in GitHub Desktop.
Save lf94/e4fc5fb299acc7289c43 to your computer and use it in GitHub Desktop.
A CasperJS test case to determine if Reddit has cats on its front page or not.
// Your test cases begin in here.
casper.test.begin("Reddit Top Links Tests", 1, function(test) {
// Your first test case starts here.
casper.start("http://reddit.com/", function() {
// In here, we have access to the website's (reddit's) DOM!
/*
So what we're doing is selecting all the entry links,
and then extracting their text, and then filtering
the link's text based on if they have the word "cat"
or not.
*/
links = document.querySelectorAll(".entry p a");
links = Array.prototype.map.call(links, function(el) {
return el.childNodes[0].data;
});
links = links.filter(function(el) {
return (el.indexOf("cat") >= 0);
});
// We check to see if we have any cat-related links. If we do the test case passes!
test.assertEquals(links.length > 0, true, "Meowston we have cats!!!");
});
// This is how you run the test cases.
casper.run(function() {
test.done();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment