{"id":83931,"date":"2017-05-22T14:58:38","date_gmt":"2017-05-22T14:58:38","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=83931"},"modified":"2017-05-23T10:24:42","modified_gmt":"2017-05-23T10:24:42","slug":"write-modules-using-casperjs","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/","title":{"rendered":"How to write your own Modules using CasperJS"},"content":{"rendered":"<p>If you plan to write your own modules in CasperJS then don&#8217;t worry, It is possible in very less effort. \u00a0There are a lot of benefits to using modules instead of an interdependent codebase.\u00a0Updating a single module is much easier and Reusability is also the main factor for modularized the code.<\/p>\n<p>In this blog, we will learn how to write your own modules using CasperJS. For creating modules in CasperJS, you have to call the patchRequire<code class=\"docutils literal\"><span class=\"pre\">()<\/span><\/code> function. We have two js files, one is Module.js and another is Root.js. Under Module.js file, I wrote the script for login and for adding product. From Root.js file, we can get our result. In Root.js file we have to require the Module.js.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush:js\">\/**\r\n* Webkul Software.\r\n*\r\n* @category Webkul\r\n* @package Webkul_CasperJS\r\n* @author Shikha Bhardwaj\r\n* @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https:\/\/webkul.com)\r\n* @license https:\/\/store.webkul.com\/license.html\r\n*\/\r\n\/\/ my module, stored in Module.js\r\n\/\/ patching phantomjs' require()\r\nvar require = patchRequire(require);\r\nvar utils = require('utils');\r\n\r\nexports.login = function(casper, current) {\r\n    current.fill('form', {\r\n        username: 'admin',\r\n        password: 'admin'\r\n    }, true);\r\n    casper.wait(2000, function() {\r\n        val = this.getCurrentUrl();\r\n        val2 = val.split(\"token=\");\r\n        productadd(casper, current, val2[1]);\r\n        current.capture('home.png');\r\n        current.echo(current.getTitle());\r\n        current.echo(val2[1]);\r\n        current.echo(this.getCurrentUrl());\r\n        if (current.getTitle() == 'Dashboard') {\r\n            return true;\r\n        } else {\r\n            console.log(\"Login\");\r\n        }\r\n\r\n    });\r\n};\r\n\r\nvar productadd = function(casper, current, token) {\r\n    current.capture('home2.png');\r\n    var url = 'http:\/\/example.com\/admin\/index.php?route=catalog\/product\/add&amp;token=' + token;\r\n    console.log(url);\r\n    casper.start(url, function() {\r\n        console.log(this.getCurrentUrl());\r\n        this.capture('home3.png');\r\n        this.fill('form#form-product', {\r\n            'product_description[1][name]': 'casper-test',\r\n            'product_description[1][meta_title]': 'casper-test',\r\n            'model': 'casper-test'\r\n        }, true);\r\n        casper.wait(2000, function() {\r\n            this.capture('home4.png');\r\n            return this.getTitle();\r\n        });\r\n    });\r\n};\r\n\r\n<\/pre>\n<p>Here is the root Casper script.<\/p>\n<pre class=\"brush:js\">var obj = require('Module');\r\nvar casper = require('casper').create();\r\nvar url = 'http:\/\/example.com\/admin\/index.php?route=common\/login';\r\ncasper.start(url, function() {\r\n    loggedIn = obj.login(casper, this); \/\/adds product named as casper-test\r\n    });\r\ncasper.run();\r\n<\/pre>\n<p>Now you have to run a command on your terminal-<br \/>\ncasperjs Root.js <\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-72278\" src=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2017\/05\/image.png\" alt=\"module\" loading=\"lazy\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you plan to write your own modules in CasperJS then don&#8217;t worry, It is possible in very less effort. \u00a0There are a lot of benefits to using modules instead of an interdependent codebase.\u00a0Updating a single module is much easier and Reusability is also the main factor for modularized the code. In this blog, we <a href=\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":36,"featured_media":70036,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4313,4854],"tags":[3505,4856,4855],"class_list":["post-83931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-casperjs","category-module-in-casperjs","tag-casperjs","tag-casperjs-module","tag-modules-in-casperjs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to write your own Modules using CasperJS - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to write your own Modules using CasperJS - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"If you plan to write your own modules in CasperJS then don&#8217;t worry, It is possible in very less effort. \u00a0There are a lot of benefits to using modules instead of an interdependent codebase.\u00a0Updating a single module is much easier and Reusability is also the main factor for modularized the code. In this blog, we [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-05-22T14:58:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-05-23T10:24:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Shikha Bhardwaj\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shikha Bhardwaj\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/\"},\"author\":{\"name\":\"Shikha Bhardwaj\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/e44090b9dd8c2c58a3c78aa1d884c030\"},\"headline\":\"How to write your own Modules using CasperJS\",\"datePublished\":\"2017-05-22T14:58:38+00:00\",\"dateModified\":\"2017-05-23T10:24:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/\"},\"wordCount\":154,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png\",\"keywords\":[\"casperjs\",\"Casperjs Module\",\"Modules in casperjs\"],\"articleSection\":[\"CasperJS\",\"Module in casperjs\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/\",\"url\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/\",\"name\":\"How to write your own Modules using CasperJS - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png\",\"datePublished\":\"2017-05-22T14:58:38+00:00\",\"dateModified\":\"2017-05-23T10:24:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png\",\"width\":825,\"height\":260},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to write your own Modules using CasperJS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/e44090b9dd8c2c58a3c78aa1d884c030\",\"name\":\"Shikha Bhardwaj\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8ada4c680b1ae3947314d3784bd12a6c5787f89723b2479a4261308bae1c1e0e?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8ada4c680b1ae3947314d3784bd12a6c5787f89723b2479a4261308bae1c1e0e?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Shikha Bhardwaj\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/shikha\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to write your own Modules using CasperJS - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/","og_locale":"en_US","og_type":"article","og_title":"How to write your own Modules using CasperJS - Webkul Blog","og_description":"If you plan to write your own modules in CasperJS then don&#8217;t worry, It is possible in very less effort. \u00a0There are a lot of benefits to using modules instead of an interdependent codebase.\u00a0Updating a single module is much easier and Reusability is also the main factor for modularized the code. In this blog, we [...]","og_url":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2017-05-22T14:58:38+00:00","article_modified_time":"2017-05-23T10:24:42+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png","type":"image\/png"}],"author":"Shikha Bhardwaj","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Shikha Bhardwaj","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/"},"author":{"name":"Shikha Bhardwaj","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/e44090b9dd8c2c58a3c78aa1d884c030"},"headline":"How to write your own Modules using CasperJS","datePublished":"2017-05-22T14:58:38+00:00","dateModified":"2017-05-23T10:24:42+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/"},"wordCount":154,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png","keywords":["casperjs","Casperjs Module","Modules in casperjs"],"articleSection":["CasperJS","Module in casperjs"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/","url":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/","name":"How to write your own Modules using CasperJS - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png","datePublished":"2017-05-22T14:58:38+00:00","dateModified":"2017-05-23T10:24:42+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/12\/casperjs.png","width":825,"height":260},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/write-modules-using-casperjs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to write your own Modules using CasperJS"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/e44090b9dd8c2c58a3c78aa1d884c030","name":"Shikha Bhardwaj","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8ada4c680b1ae3947314d3784bd12a6c5787f89723b2479a4261308bae1c1e0e?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8ada4c680b1ae3947314d3784bd12a6c5787f89723b2479a4261308bae1c1e0e?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Shikha Bhardwaj"},"url":"https:\/\/webkul.com\/blog\/author\/shikha\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/83931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=83931"}],"version-history":[{"count":4,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/83931\/revisions"}],"predecessor-version":[{"id":84505,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/83931\/revisions\/84505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/70036"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=83931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=83931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=83931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}