Skip to content

Instantly share code, notes, and snippets.

@jsgao0
Created June 28, 2016 15:31
Show Gist options
  • Save jsgao0/d22b27b493d7efbb03260a56d2f964a1 to your computer and use it in GitHub Desktop.
Save jsgao0/d22b27b493d7efbb03260a56d2f964a1 to your computer and use it in GitHub Desktop.
CasperJS tests Facebook authorization.
casper.test.begin('Test enroll with Facebook.', 1, function suite(test) {
casper
.start('http://localhost/enroll')
.then(function() { // #1 Test: Register using Facebook but user click cancel button on auth page.
casper.click('#facebookLogin'); // Simulate an user click button 'Login with Facebook'.
casper.waitForPopup(/^https:\/\/www.facebook.com\/login/, function() { // Wait for Facebook login popup.
this.echo('Open Facebook login page when not login yet.'); // The user has not login yet.
});
casper.withPopup(/^https:\/\/www.facebook.com\/login/, function() { // Do something within the popup.
this.evaluate(function() { // Simulate the user enters email and password. Then, click login button of Facebook.
document.getElementById("email").value="email"; // Simulate the user fills in email field.
document.getElementById("pass").value="password"; // Simulate the user fills in password field.
document.getElementById("loginbutton").children[0].click(); // Simulate the user clicks login button of Facebook.
});
this.echo('Entered email and password.'); // Just show message when the simulation of login has been finished.
});
casper.waitForPopup(/^https:\/\/www.facebook.com\/v2\.6\/dialog\/oauth/, function() { // It shows oauth page of Facebook when login successfully.
this.echo('Login successfully.');
});
casper.withPopup(/^https:\/\/www.facebook.com\/v2\.6\/dialog\/oauth/, function() { // Do something withing this popup.
casper.waitForSelector('[name=__CANCEL__]', function() { // Wait for the CANCEL button gets ready.
casper.waitUntilVisible('[name=__CANCEL__]', function() { // Wait for the CANCEL button gets visible.
this.evaluate(function() { // Simulate the user clicks CANCEL button.
document.querySelector('[name=__CANCEL__]').click();
});
this.echo('Clicked cancel button.'); // Show something when the user clicks CANCEL button.
});
});
});
casper.waitUntilVisible('.fail-popup', function() { // Test the things you do in login fail callback function.
test.assertSelectorHasText('.fail-popup', 'Login fail.', 'Test register by Facebook but user click cancel button on auth page.');
});
})
.run(function() {
test.done();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment