Can you imagine placing an order through CasperJS for your Opencart store? Yes, it is possible you can place an order for your Opencart store using CasperJS. In this blog, we will learn how to place an order with the help of CasperJS.
For placing an order, Firstly we have to sign in to our Opencart store and after that, we will add our desired product into the cart after that we will place the order. We will use the following functions for placing an order-
waitForSelector() Waits until an element matching the provided selector expression exists in remote DOM to process any next step
fill() Fills the fields of a form with given values and optionally submits it.
thenOpen() Adds a new navigation step for opening a new location.
click() Performs a click on the element matching the provided selector expression.
getCurrentUrl() Retrieves current page URL.
/** * Webkul Software. * * @category Webkul * @package Webkul_CasperJS * @author Shikha Bhardwaj * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ var casper = require("casper").create(); var urlLogin= casper.cli.get(0); casper.start(urlLogin, function() { this.echo(this.getTitle()); this.waitForSelector('form[action="' + urlLogin + '"]'); }); //Login as a customer casper.then(function(){ this.fill('form[action="' + urlLogin + '"]' ,{ email: 'example@example.com', password: 'xyz123' }, true); this.echo('frontend login'); }); //Select product from category casper.thenOpen('http://example.com/index.php?route=product/category&path=57', function(){ this.echo(this.getTitle(),"INFO"); }); //Add product into the cart casper.then(function(){ this.click('#content > div:nth-child(3) > div > div > div:nth-child(2) > div.button-group > button:nth-child(1)'); this.echo('Product added into the cart', "INFO"); }); casper.wait(2000, function(){ this.echo(this.getCurrentUrl()); }); //Open the shopping cart page casper.thenOpen('http://example.com/index.php?route=checkout/cart'); casper.wait(2000, function(){ this.echo(this.getCurrentUrl(),"INFO"); }); //Add billing address casper.wait(2000, function(){ this.click('#content > div.buttons.clearfix > div.pull-right > a'); }); //Add delivery details casper.wait(2000, function(){ this.click('#button-payment-address'); }); //Add delivery method casper.wait(2000, function(){ this.click('#button-shipping-address'); }); //Select payment method casper.wait(2000, function(){ this.click('#button-shipping-method'); }); casper.wait(2000, function(){ this.click('#button-shipping-method'); }); casper.then(function(){ this.click('#collapse-payment-method > div > div.buttons > div > input[type="checkbox"]:nth-child(2)'); this.echo(this.getCurrentUrl(),"INFO"); this.echo('Agree with terms and condition',"INFO"); this.click('#button-payment-method'); }); casper.wait(5000, function(){ this.capture('order.process.png'); this.click('#button-confirm'); this.echo('you have placed your order',"INFO"); }); casper.run();
Now you have to run a command on your terminal-
casperjs file_name.js http://login-url/
Here, you can see the result on your terminal –