Skip to content

Instantly share code, notes, and snippets.

@jsgao0
Last active June 16, 2016 01:42
Show Gist options
  • Save jsgao0/7f9b584f65927266e9dfbd596d10b407 to your computer and use it in GitHub Desktop.
Save jsgao0/7f9b584f65927266e9dfbd596d10b407 to your computer and use it in GitHub Desktop.
Test the user behavior when a user click a tag and redirect.
/*
Created by jsgao0 on 2016/06/15.
The intallation guide: http://docs.casperjs.org/en/latest/installation.html
Steps:
1: Install PhantomJS globally. npm install phantomjs -g
2: Install CasperJS globally. npm install casperjs -g
3: Execute tests. casperjs test testRedirectAndBack.js
*/
casper.test.begin('Test redirect and back to URLs.', 5, function suite(test) {
casper
.start('https://github.com/', function() { // Check HTTP status code is 200.
test.assertHttpStatus(200);
})
.then(function() { // Check page content.
test.assertTitle('How people build software · GitHub');
test.assertUrlMatch(/^https:\/\/github.com/, 'We are at GitHub home page.');
}).then(function() { // Test click /personal and go to Personal page. Then, back to home page.
this.click('a[href$=personal]'); // #1 Trigger click on a[href$=personal].
this.wait(2000, function() { // #2 Wait for 2 seconds and check we are at Personal page.
test.assertUrlMatch(/^https:\/\/github.com\/personal/, 'We are at Personal page.');
});
this.wait(100, function() { // #3 This is important. Use this.wait for synchronized testing or this will be fired after #1 and before #2.
casper.back();
});
this.wait(2000, function() { // #4 Wait for 2 seconds and check we are at GitHub home page.
test.assertUrlMatch(/^https:\/\/github.com/, 'We are at GitHub home page.');
});
}).run(function() {
test.done();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment