Back to Top

How To Place An Order In Magento2 Using CasperJs

Updated 6 January 2017

Manual testing is a boring task to do because in manual testing we perform the same task many times which is time-consuming. To overcome this problem, we can automate testing with the help CasperJs. CasperJs allows you to build full navigation scenarios using high-level functions and a straight forward interface to accomplish all sizes of tasks.

In Magento there are various “repetitive” tasks to check such as customer registration process, customer log in to the account, adding different types of products to the shopping cart, checkout steps etc. These repetitive actions take a lot of time when we have done manually but now it can be done in few minutes by automation.

For the customer sign-up click here. Now we’ll proceed with the testing process. Here we’ll learn how we can log in and place an order using CasperJs into a Magento2 store.

The code procedure is as follows-

Open home page of your Magento2 store, click on “Sign In” link, open “Customer Login” page to fill out the Email and Password, submit it, wait to be redirected to “My Account Dashboard” and then get url of the Dashboard page.

Searching for an experienced
Magento Company ?
Find out More

Here we have used some CasperJs methods like-

click() method is used to click a link and buttons.

fill() method is used to fill the forms.

getCurrentUrl() is used to get the current page url.

echo() is used to print.

getTitle() is used to get Title of tha page.

wait() is used to wait for given amount of time.

* Webkul Software.
* @category Webkul
* @package Webkul_CasperJS
* @author Webkul
* @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (
* @license

var casper = require("casper").create({
  verbose: true

var login = '';   // Enter Your Magento Customer Login URL

//Go to Login page
casper.start(login, function () {
  this.echo('Current Page Title: ' + this.getTitle(), "COMMENT");

//Login form fillup
casper.then(function () {
  this.fill('form#login-form', {
    'login[username]': '[email protected]',
    'login[password]': 'Xyz123'

  }, false);

//Click On Sign in button
casper.then(function () {'#send2');
casper.wait(3000, function () {
  this.echo('Current Page Title: ' + this.getTitle(), "COMMENT");
  this.echo('Current location is: ' + this.getCurrentUrl(), "PARAMETER");

// Click On Category
casper.then(function () {'#ui-id-7');
casper.wait(2000, function () {
  this.echo('Category Name: ' + this.getTitle(), "COMMENT");

//  Click On Simple Product
casper.then(function () {'#maincontent > div.columns > div.column.main > div.products.wrapper.grid.products-grid > ol > li:nth-child(9) > div > a > span > span > img');
casper.wait(3000, function () {
  this.echo('Product Name: ' + this.getTitle(), "COMMENT");

// Click On Add to cart button
casper.then(function () {'#product-addtocart-button');
casper.wait(5000, function () {
  this.echo(this.fetchText('#maincontent > > div:nth-child(2) > div:nth-child(1) > div'), "INFO");

// Call Cart Page
casper.thenOpen(''); // Enter Your Cart Page URL
casper.wait(2000, function () {
  this.echo('Current Page Title: ' + this.getTitle(), "COMMENT");

// Click On Procced To Checkout Button
casper.then(function () {'#maincontent > div.columns > div > div.cart-container > div.cart-summary > ul > li:nth-child(1) > button');
casper.wait(5000, function () {
  this.echo('Current location is: ' + this.getCurrentUrl(), "PARAMETER");

// Select Address
casper.wait(5000, function () {'#checkout-step-shipping > div.field.addresses > div > div > div:nth-child(2) > button');

//Click On Next Button // call payment page
casper.then(function () {'#shipping-method-buttons-container > div > button');
casper.wait(2000, function () {
  this.echo('Current Page Title: ' + this.fetchText('#payment > div.step-title'), "COMMENT");

// Click on Proceed Button
casper.wait(8000, function () {'#checkout-payment-method-load > div > div.payment-method._active > div.payment-method-content > div.actions-toolbar > div > button');
// Success messgage 
casper.wait(10000, function () {
  this.echo(this.fetchText('#maincontent > div.columns > div > div.checkout-success > p:nth-child(1)'), "INFO_BAR");
  this.echo('Current Page Title: ' + this.getTitle(), "INFO_BAR");

Now you have to run a command on your terminal-

casperjs your_file_name.js 

Here, you can see the result on your terminal –

After running the script you will get the Order Number and Success message on terminal.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home