Intro

In this post I want to share with you a working example of “How to login to Amazon using CasperJS”. This post is part of post series about PhantomJS and CasperJS. If you are working with PhantomJS and you want to login to Amazon website, then read my post How to login to Amazon using PhantomJS. Also, if you want to know why CasperJS is better than PhantomJS, then read this post.

Working example and explanation

When you need to handle login system with CasperJS you have to think about the following things:

  1. Submit login form
  2. Store cookies received by server (this step is automatically performed by CasperJS)
  3. Resend cookies upon every new request (this step is automatically performed by CasperJS)
  4. Save cookies to file (optional)

Sometimes, login forms are loaded by AJAX requests, which makes your life much harder. In that case, you can use other available functions in CasperJS.

Available functions

wait() – Pause steps suite execution for a given amount of time, and optionally execute a step on done
waitFor() – Waits until a function returns true to process any next step
waitForAlert() – Waits until a JavaScript alert is triggered.
waitForPopup() – Waits for a popup having its url matching the provided pattern to be opened and loaded
waitForResource() – Wait until a resource that matches a resource matching constraints defined by testFx are satisfied to process a next step
waitForUrl() – Waits for the current page url to match the provided argument
waitForSelector() – Waits until an element matching the provided selector expression exists in remote DOM to process any next step
waitWhileSelector() – Waits until an element matching the provided selector expression does not exist in remote DOM to process a next step
waitForSelectorTextChange() – Waits until the text on an element matching the provided selector expression is changed to a different value before processing the next step
waitForText() – Waits until the passed text is present in the page contents before processing the immediate next step
waitUntilVisible() – Waits until an element matching the provided selector expression is visible in the remote DOM to process a next step
waitWhileVisible() – Waits until an element matching the provided selector expression is no longer visible in remote DOM to process a next step

Working example

The following CasperJS working example will login you to Amazon, and will take a screenshot when user is logged in:

 Cookie Management

Every time when your CasperJS script ends, all cookie information are deleted, and by next script execution you have to log-in again. If you repeat this process multiple times, you may end up with Captcha screen, or some other “Confirm you are human” page.

To avoid this, it would be good that you save cookies in file before you script ends. Next time when your script is executed, check if cookie file exists, and if yes, then load the cookies from the file, and you will be automatically logged in into amazon website.

I wrote JavaScript prototype for cookie management. You can copy and past this code at the top of your CasperJS script, or you can save it into separate file, and then load it from your script. I prefer to save it in another file, and then load it from several scripts where I need cookie management functionality.

Here is example of usage (code above is saved into separate file called DCookieManagement.js, that is loaded/imported from my CasperJS script)

If you want to save cookies, then you just have to call the following line:

and your cookies will be saved into liveCookies.txt file.

Cookies are saved as JSON string, and you can check cookie values simply by opening the .txt file, or you can use cookies in other applications.

 

How to login to Amazon using CasperJS - Working examplehttp://code-epicenter.com/wp-content/uploads/2015/08/amazon.pnghttp://code-epicenter.com/wp-content/uploads/2015/08/amazon-150x150.pngAmir DuranJavaScriptLibrariesProgrammingTutorialsAmazon,CasperJS,Java Script,Web scraping
Intro In this post I want to share with you a working example of 'How to login to Amazon using CasperJS'. This post is part of post series about PhantomJS and CasperJS. If you are working with PhantomJS and you want to login to Amazon website, then read my post How...