Casperjs instructions-use command line

Source: Internet
Author: User

Use the command line Casperjs to use the built-in phantomjs command line parser. In the cli module, it passes the parameter location naming options, but do not worry that it cannot skillfully control the API of the CLI module, A casper instance already contains the cli attribute, allowing you to easily use its parameters. Let's look at this simple casper Script: var casper = require ("casper "). create (); casper. echo ("Casper CLI passed args:"); require ("utils "). dump (casper. cli. args); casper. echo ("Casper CLI passed options:"); require ("utils "). dump (casper. cli. options); casper. exit (); Note: Pay attention to the two parameters casper-path and cli. They execute c Execution result of asperjs script executed by python: $ casperjs test. js arg1 arg2 arg3 -- foo = bar -- plop anotherargCasper CLI passed args: ["arg1", "arg2", "arg3", "anotherarg"] Casper CLI passed options: {"casper-path": "/Users/niko/Sites/casperjs", "cli": true, "foo": "bar", "plop": true} get, check and delete the parameter: var casper = require ("casper "). create (); casper. echo (casper. cli. has (0); casper. echo (casper. cli. get (0); casper. echo (casper. cli. Has (3); casper. echo (casper. cli. get (3); casper. echo (casper. cli. has ("foo"); casper. echo (casper. cli. get ("foo"); casper. cli. drop ("foo"); casper. echo (casper. cli. has ("foo"); casper. echo (casper. cli. get ("foo"); casper. exit (); execution result: $ casperjs test. js arg1 arg2 arg3 -- foo = bar -- plop anotherargtruearg1trueanotherargtruebarfalseundefined: If you want to check that some parameters are used in your script, see the following: casper. cli. drop ("cli"); casper. cli. drop ("ca Sper-path "); if (casper. cli. args. length = 0 & Object. keys (casper. cli. options ). length = 0) {casper. echo ("No arg nor option passed "). exit ();} casper self-owned parameter: New in version 1.1: The casperjs command has three common parameters: -- direct: output the log message to the console -- log-level = [debug | info | warning | error] to set the log level -- engine = [phantomjs | slimerjs] and select the browser engine you want to use, casperjs supports webkit supported by phantomjs by default. SlimerJS runs the webkit kernel. Note: Version 1.1 has been deprecated. The direct parameter has been renamed to verbose. -- Although direct is still available, it is considered to be discarded. Example: $ casperjs -- verbose -- log-level = debug myscript. the last but equally important thing about js is that, you can also use all the phantomjs and cli command line parameters, just like running other phantomjs scripts $ casperjs -- web-security = no -- cookies-file =/tmp/mycookies.txt myscript. useful js suggestions: to know which phantomjs command line parameters are available, run the phantomjs -- help Command. The command line parameters supported by SlimerJS are almost the same as the original parameter values of phantomjs: by default, the cli object will convert all parameters to the type he thinks is the most appropriate. For example, var casper = require ('casper '). create (); var utils = require ('utils'); utils. dump (casper. cli. get ('foo'); casper. exit (); run this script: $ casperjs c. js -- foo = 012345671234567 as you can see, 01234567 is converted to numbers. Sometimes, you only want to get the original string. You can use the raw attribute of the cli object, it contains the original value of this parameter var casper = require ('casper '). create (); var utils = require ('utils'); utils. dump (casper. cli. get ('foo'); utils. dump (casper. cli. raw. get ('foo'); casper. exit (); example: $ casperjs c. js -- fool = 012345671234567 "01234567"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.