.. _casper_module: ===================== The ``casper`` module ===================== .. index:: Casper The ``Casper`` class ++++++++++++++++++++ The easiest way to get a casper instance is to use the module's ``create()`` method:: var casper = require('casper').create(); But you can also retrieve the main Function and instantiate it by yourself:: var casper = new require('casper').Casper(); .. hint:: Also, check out :doc:`how to extend Casper with your own methods <../extending>`. Both the ``Casper`` constructor and the ``create()`` function accept a single ``options`` argument which is a standard javascript object:: var casper = require('casper').create({ verbose: true, logLevel: "debug" }); .. _casper_options: .. index:: Casper options, options ``Casper.options`` ++++++++++++++++++ An ``options`` object can be passed to the ``Casper`` constructor, eg.:: var casper = require('casper').create({ clientScripts: [ 'includes/jquery.js', // These two scripts will be injected in remote 'includes/underscore.js' // DOM on every request ], pageSettings: { loadImages: false, // The WebPage instance used by Casper will loadPlugins: false // use these settings }, logLevel: "info", // Only "info" level messages will be logged verbose: true // log messages will be printed out to the console }); You can also alter options at runtime:: var casper = require('casper').create(); casper.options.waitTimeout = 1000; The whole list of available options is detailed below. .. index:: Client scripts .. _casper_option_clientscripts: ``clientScripts`` ------------------------------------------------------------------------------- **Type:** ``Array`` **Default:** ``[]`` A collection of script filepaths to include in every page loaded. .. index:: exit, error ``exitOnError`` ------------------------------------------------------------------------------- **Type:** ``Boolean`` **Default:** ``true`` Sets if CasperJS must exit when an uncaught error has been thrown by the script. .. index:: HTTP ``httpStatusHandlers`` ------------------------------------------------------------------------------- **Type:** ``Object`` **Default:** ``{}`` A javascript Object containing functions to call when a requested resource has a given HTTP status code. A dedicated sample is provided as an example. .. index:: Logging ``logLevel`` ------------------------------------------------------------------------------- **Type:** ``String`` **Default:** ``"error"`` Logging level (see the logging section for more information) ``onAlert`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``null`` **Signature:** ``onAlert(Object Casper, String message)`` A function to be called when a javascript alert() is triggered ``onDie`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``null`` **Signature:** ``onDie(Object Casper, String message, String status)`` A function to be called when Casper#die() is called .. index:: error, Error handling ``onError`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``null`` **Signature:** ``onError(Object Casper, String msg, Array backtrace)`` A function to be called when an "error" level event occurs .. index:: error, Error handling ``onLoadError`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``null`` **Signature:** ``onLoadError(Object Casper, String casper.requestUrl, String status)`` A function to be called when a requested resource cannot be loaded ``onPageInitialized`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``null`` **Signature:** ``onPageInitialized(Object page)`` A function to be called after ``WebPage`` instance has been initialized .. index:: HTTP ``onResourceReceived`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``null`` **Signature:** ``onResourceReceived(Object Casper, Object resource)`` Proxy method for PhantomJS' ``WebPage#onResourceReceived()`` callback, but the current Casper instance is passed as first argument. .. index:: HTTP ``onResourceRequested`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``null`` **Signature:** ``onResourceRequested(Object Casper, Object resource)`` Proxy method for PhantomJS' WebPage#onResourceRequested() callback, but the current Casper instance is passed as first argument. .. index:: Step stack ``onStepComplete`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``null`` **Signature:** ``onStepComplete(Object Casper, stepResult)`` A function to be executed when a step function execution is finished. .. index:: Step stack, Error handling, timeout ``onStepTimeout`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``Function`` **Signature:** ``onStepTimeout(Integer timeout, Integer stepNum)`` A function to be executed when a step function execution time exceeds the value of the stepTimeout option, if any has been set. By default, on timeout the script will exit displaying an error, except in test environment where it will just add a failure to the suite results. .. index:: Error handling, timeout ``onTimeout`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``Function`` **Signature:** ``onTimeout(Integer timeout)`` A function to be executed when script execution time exceeds the value of the timeout option, if any has been set. By default, on timeout the script will exit displaying an error, except in test environment where it will just add a failure to the suite results. .. index:: Error handling, timeout ``onWaitTimeout`` ------------------------------------------------------------------------------- **Type:** ``Function`` **Default:** ``Function`` **Signature:** ``onWaitTimeout(Integer timeout)`` A function to be executed when a ``waitFor*`` function execution time exceeds the value of the waitTimeout option, if any has been set. By default, on timeout the script will exit displaying an error, except in test environment where it will just add a failure to the suite results. ``page`` ------------------------------------------------------------------------------- **Type:** ``WebPage`` **Default:** ``null`` An existing PhantomJS ``WebPage`` instance .. warning:: **Overriding** the ``page`` properties can cause some of the casper features **may not work**. For example, overriding the ``onUrlChanged`` property will cause the ``waitForUrl`` feature not work. .. index:: settings, PhantomJS, SSL, auth, XSS ``pageSettings`` ------------------------------------------------------------------------------- **Type:** ``Object`` **Default:** ``{}`` PhantomJS's WebPage settings object. Available settings are: - ``javascriptEnabled`` defines whether to execute the script in the page or not (default to ``true``) - ``loadImages`` defines whether to load the inlined images or not - ``localToRemoteUrlAccessEnabled`` defines whether local resource (e.g. from file) can access remote URLs or not (default to ``false``) - ``userAgent`` defines the user agent sent to server when the web page requests resources - ``userName`` sets the user name used for HTTP authentication - ``password`` sets the password used for HTTP authentication - ``resourceTimeout`` (in milli-secs) defines the timeout after which any resource requested will stop trying and proceed with other parts of the page. onResourceTimeout callback will be called on timeout. undefined (default value) means default gecko parameters. PhantomJS specific settings : - ``XSSAuditingEnabled`` defines whether load requests should be monitored for cross-site scripting attempts (default to ``false``) - ``webSecurityEnabled`` defines whether web security should be enabled or not (defaults to true) SlimerJS specific settings : - ``allowMedia`` false to deactivate the loading of media (audio / video). Default: true. (SlimerJS only) - ``maxAuthAttempts`` indicate the maximum of attempts of HTTP authentication. (SlimerJS 0.9) - ``plainTextAllContent`` true to indicate that webpage.plainText returns everything, even content of script elements, invisible elements etc.. Default: false. .. index:: Remote scripts ``remoteScripts`` ------------------------------------------------------------------------------- **Type:** ``Array`` **Default:** ``[]`` .. versionadded:: 1.0 A collection of remote script urls to include in every page loaded .. index:: Logging ``safeLogs`` ------------------------------------------------------------------------------- **Type:** ``Boolean`` **Default:** ``true`` .. versionadded:: 1.0 When this option is set to true — which is the default, any password information entered in will be obfuscated in log messages. Set safeLogs to false to disclose passwords in plain text (not recommended). .. index:: Step stack, timeout ``silentErrors`` ------------------------------------------------------------------------------- **Type:** ``Boolean`` **Default:** ``false`` When this option is enabled, caught step errors are not thrown (though related events are still emitted). Mostly used internally in a testing context. .. index:: timeout ``stepTimeout`` ------------------------------------------------------------------------------- **Type:** ``Number`` **Default:** ``null`` Max step timeout in milliseconds; when set, every defined step function will have to execute before this timeout value has been reached. You can define the onStepTimeout() callback to catch such a case. By default, the script will die() with an error message. .. index:: timeout ``timeout`` ------------------------------------------------------------------------------- **Type:** ``Number`` **Default:** ``null`` Max timeout in milliseconds .. index:: verbose ``verbose`` ------------------------------------------------------------------------------- **Type:** ``Boolean`` **Default:** ``false`` Realtime output of log messages .. index:: viewport ``viewportSize`` ------------------------------------------------------------------------------- **Type:** ``Object`` **Default:** ``null`` Viewport size, eg. ``{width: 800, height: 600}`` .. note:: PhantomJS ships with a default viewport of 400x300, and CasperJS won't override it by default. .. index:: timeout ``retryTimeout`` ------------------------------------------------------------------------------- **Type:** ``Number`` **Default:** ``100`` Default delay between attempts, for ``wait*`` family functions. ``waitTimeout`` ------------------------------------------------------------------------------- **Type:** ``Number`` **Default:** ``5000`` Default wait timeout, for ``wait*`` family functions. ``Casper`` prototype ++++++++++++++++++++ ``back()`` ------------------------------------------------------------------------------- **Signature:** ``back()`` Moves back a step in browser's history:: casper.start('http://foo.bar/1') casper.thenOpen('http://foo.bar/2'); casper.thenOpen('http://foo.bar/3'); casper.back(); casper.run(function() { console.log(this.getCurrentUrl()); // 'http://foo.bar/2' }); Also have a look at `forward()`_. .. _casper_base64encode: .. index:: Base64 ``base64encode()`` ------------------------------------------------------------------------------- **Signature:** ``base64encode(String url [, String method, Object data])`` Encodes a resource using the base64 algorithm synchronously using client-side XMLHttpRequest. .. note:: We cannot use ``window.btoa()`` because it fails miserably in the version of WebKit shipping with PhantomJS. Example: retrieving google logo image encoded in base64:: var base64logo = null; casper.start('http://www.google.fr/', function() { base64logo = this.base64encode('http://www.google.fr/images/srpr/logo3w.png'); }); casper.run(function() { this.echo(base64logo).exit(); }); You can also perform an HTTP POST request to retrieve the contents to encode:: var base64contents = null; casper.start('http://domain.tld/download.html', function() { base64contents = this.base64encode('http://domain.tld/', 'POST', { param1: 'foo', param2: 'bar' }); }); casper.run(function() { this.echo(base64contents).exit(); }); .. index:: bypass, Step stack ``bypass()`` ------------------------------------------------------------------------------- **Signature:** ``bypass(Numbr nb)`` .. versionadded:: 1.1 Bypasses a given number of defined navigation steps:: casper.start(); casper.then(function() { // This step will be executed }); casper.then(function() { this.bypass(2); }); casper.then(function() { // This test won't be executed }); casper.then(function() { // Nor this one }); casper.run(); .. _casper_click: .. index:: click ``click()`` ------------------------------------------------------------------------------- **Signature:** ``click(String selector, [Number|String X, Number|String Y])`` Performs a click on the element matching the provided :doc:`selector expression <../selectors>`. The method tries two strategies sequentially: 1. trying to trigger a MouseEvent in Javascript 2. using native QtWebKit event if the previous attempt failed Example:: casper.start('http://google.fr/'); casper.thenEvaluate(function(term) { document.querySelector('input[name="q"]').setAttribute('value', term); document.querySelector('form[name="f"]').submit(); }, 'CasperJS'); casper.then(function() { // Click on 1st result link this.click('h3.r a'); }); casper.then(function() { // Click on 1st result link this.click('h3.r a',10,10); }); casper.then(function() { // Click on 1st result link this.click('h3.r a',"50%","50%"); }); casper.then(function() { console.log('clicked ok, new location is ' + this.getCurrentUrl()); }); casper.run(); .. index:: click ``clickLabel()`` ------------------------------------------------------------------------------- **Signature:** ``clickLabel(String label[, String tag])`` .. versionadded:: 0.6.1 Clicks on the first DOM element found containing ``label`` text. Optionaly ensures that the element node name is ``tag``:: // My link is beautiful casper.then(function() { this.clickLabel('My link is beautiful', 'a'); }); // casper.then(function() { this.clickLabel('But my button is sexier', 'button'); }); .. index:: screenshot ``capture()`` ------------------------------------------------------------------------------- **Signature:** ``capture(String targetFilepath, [Object clipRect, Object imgOptions])`` Proxy method for PhantomJS' ``WebPage#render``. Adds a ``clipRect`` parameter for automatically setting page ``clipRect`` setting and reverts it back once done:: casper.start('http://www.google.fr/', function() { this.capture('google.png', { top: 100, left: 100, width: 500, height: 400 }); }); casper.run(); .. versionadded:: 1.1 The ``imgOptions`` object allows to specify two options: - ``format`` to set the image format manually, avoiding relying on the filename - ``quality`` to set the image quality, from 1 to 100 Example:: casper.start('http://foo', function() { this.capture('foo', undefined, { format: 'jpg', quality: 75 }); }); .. index:: screenshot, Base64 ``captureBase64()`` ------------------------------------------------------------------------------- **Signature:** ``captureBase64(String format[, Mixed area])`` .. versionadded:: 0.6.5 Computes the `Base64 `_ representation of a binary image capture of the current page, or an area within the page, in a given format. Supported image formats are ``bmp``, ``jpg``, ``jpeg``, ``png``, ``ppm``, ``tiff``, ``xbm`` and ``xpm``. The ``area`` argument can be either of the following types: - ``String``: area is a CSS3 selector string, eg. ``div#plop form[name="form"] input[type="submit"]`` - ``clipRect``: area is a clipRect object, eg. ``{"top":0,"left":0,"width":320,"height":200}`` - ``Object``: area is a :doc:`selector object <../selectors>`, eg. an XPath selector Example:: casper.start('http://google.com', function() { // selector capture console.log(this.captureBase64('png', '#lga')); // clipRect capture console.log(this.captureBase64('png', { top: 0, left: 0, width: 320, height: 200 })); // whole page capture console.log(this.captureBase64('png')); }); casper.run(); .. _casper_captureselector: .. index:: screenshot ``captureSelector()`` ------------------------------------------------------------------------------- **Signature:** ``captureSelector(String targetFile, String selector [, Object imgOptions])`` Captures the page area containing the provided selector and saves it to ``targetFile``:: casper.start('http://www.weather.com/', function() { this.captureSelector('weather.png', '#LookingAhead'); }); casper.run(); .. versionadded:: 1.1 The ``imgOptions`` object allows to specify two options: - ``format`` to set the image format manually, avoiding relying on the target filename - ``quality`` to set the image quality, from 1 to 100 ``clear()`` ------------------------------------------------------------------------------- **Signature:** ``clear()`` .. versionadded:: 0.6.5 Clears the current page execution environment context. Useful to avoid having previously loaded DOM contents being still active. Think of it as a way to stop javascript execution within the remote DOM environment:: casper.start('http://www.google.fr/', function() { this.clear(); // javascript execution in this page has been stopped }); casper.then(function() { // ... }); casper.run(); .. _casper_clearcache: .. index:: Memory ``clearCache()`` ------------------------------------------------------------------------------- **Signature:** ``clearCache()`` .. versionadded:: 1.1.5 Replace current page by a new page object, with newPage() and clear the memory cache, with clearMemoryCache(). Example:: casper.start('http://www.google.fr/', function() { this.clearCache(); // cleared the memory cache and replaced page object with newPage(). }); casper.then(function() { // ... }); casper.run(); .. _casper_clearmemorycache: .. index:: Memory ``clearMemoryCache()`` ------------------------------------------------------------------------------- **Signature:** ``clearMemoryCache()`` .. versionadded:: 1.1.5 Use the engine page.clearMemoryCache() to clear the memory cache. Example:: casper.start('http://www.google.fr/', function() { this.clearMemoryCache(); // cleared the memory cache. }); casper.then(function() { // ... }); casper.run(); .. index:: Debugging ``debugHTML()`` ------------------------------------------------------------------------------- **Signature:** ``debugHTML([String selector, Boolean outer])`` Outputs the results of `getHTML()`_ directly to the console. It takes the same arguments as ``getHTML()``. .. index:: Debugging ``debugPage()`` ------------------------------------------------------------------------------- **Signature:** ``debugPage()`` Logs the textual contents of the current page directly to the standard output, for debugging purpose:: casper.start('http://www.google.fr/', function() { this.debugPage(); }); casper.run(); ``die()`` ------------------------------------------------------------------------------- **Signature:** ``die(String message[, int status])`` Exits phantom with a logged error message and an optional exit status code:: casper.start('http://www.google.fr/', function() { this.die("Fail.", 1); }); casper.run(); .. _casper_download: .. index:: download ``download()`` ------------------------------------------------------------------------------- **Signature:** ``download(String url, String target[, String method, Object data])`` Saves a remote resource onto the filesystem. You can optionally set the HTTP method using the ``method`` argument, and pass request arguments through the ``data`` object (see `base64encode()`_):: casper.start('http://www.google.fr/', function() { var url = 'http://www.google.fr/intl/fr/about/corporate/company/'; this.download(url, 'google_company.html'); }); casper.run(function() { this.echo('Done.').exit(); }); .. note:: If you have some troubles downloading files, try to :ref:`disable web security `. Also, this is meant more for retrieving static resources. If you want to get the JavaScript-rendered HTML DOM use getHTML() instead. ``each()`` ------------------------------------------------------------------------------- **Signature:** ``each(Array array, Function fn)`` Iterates over provided array items and execute a callback:: var links = [ 'http://google.com/', 'http://yahoo.com/', 'http://bing.com/' ]; casper.start().each(links, function(self, link) { self.thenOpen(link, function() { this.echo(this.getTitle()); }); }); casper.run(); .. hint:: Have a look at the `googlematch.js `_ sample script for a concrete use case. ``eachThen()`` ------------------------------------------------------------------------------- **Signature:** ``eachThen(Array array, Function then)`` .. versionadded:: 1.1 Iterates over provided array items and adds a step to the stack with current data attached to it:: casper.start().eachThen([1, 2, 3], function(response) { this.echo(response.data); }).run(); Here's an example for opening an array of urls:: var casper = require('casper').create(); var urls = ['http://google.com/', 'http://yahoo.com/']; casper.start().eachThen(urls, function(response) { this.thenOpen(response.data, function(response) { console.log('Opened', response.url); }); }); casper.run(); .. note:: Current item will be stored in the ``response.data`` property. .. _casper_echo: .. index:: echo, Printing ``echo()`` ------------------------------------------------------------------------------- **Signature:** ``echo(String message[, String style])`` Prints something to stdout, optionally with some fancy color (see the :ref:`colorizer module ` for more information):: casper.start('http://www.google.fr/', function() { this.echo('Page title is: ' + this.evaluate(function() { return document.title; }), 'INFO'); // Will be printed in green on the console }); casper.run(); .. index:: evaluate, DOM .. _casper_evaluate: ``evaluate()`` ------------------------------------------------------------------------------- **Signature:** ``evaluate(Function fn[, arg1[, arg2[, …]]])`` Basically `PhantomJS' WebPage#evaluate `_ equivalent. Evaluates an expression **in the current page DOM context**:: casper.evaluate(function(username, password) { document.querySelector('#username').value = username; document.querySelector('#password').value = password; document.querySelector('#submit').click(); }, 'sheldon.cooper', 'b4z1ng4'); .. note:: For filling and submitting forms, rather use the `fill()`_ method. .. warning:: The pre-1.0 way of passing arguments using an object has been kept for BC purpose, though it may `not work in some case `_; so you're encouraged to use the method described above. .. topic:: Understanding ``evaluate()`` The concept behind this method is probably the most difficult to understand when discovering CasperJS. As a reminder, think of the ``evaluate()`` method as a *gate* between the CasperJS environment and the one of the page you have opened; everytime you pass a closure to ``evaluate()``, you're entering the page and execute code as if you were using the browser console. Here's a quickly drafted diagram trying to basically explain the separation of concerns: .. figure:: ../_static/images/evaluate-diagram.png :align: center ``evaluateOrDie()`` ------------------------------------------------------------------------------- **Signature:** ``evaluateOrDie(Function fn[, String message, int status])`` Evaluates an expression within the current page DOM and ``die()`` if it returns anything but ``true``:: casper.start('http://foo.bar/home', function() { this.evaluateOrDie(function() { return /logged in/.match(document.title); }, 'not authenticated'); }); casper.run(); .. index:: exit ``exit()`` ------------------------------------------------------------------------------- **Signature:** ``exit([int status])`` Exits PhantomJS with an optional exit status code. Note: You can not rely on the fact that your script will be turned off immediately, because this method works asynchronously. It means that your script may continue to be executed after the call of this method. More info `here `_. .. index:: DOM ``exists()`` ------------------------------------------------------------------------------- **Signature:** ``exists(String selector)`` Checks if any element within remote DOM matches the provided :doc:`selector <../selectors>`:: casper.start('http://foo.bar/home', function() { if (this.exists('#my_super_id')) { this.echo('found #my_super_id', 'INFO'); } else { this.echo('#my_super_id not found', 'ERROR'); } }); casper.run(); .. _casper_fetchtext: ``fetchText()`` ------------------------------------------------------------------------------- **Signature:** ``fetchText(String selector)`` Retrieves text contents matching a given :doc:`selector expression <../selectors>`. If you provide one matching more than one element, their textual contents will be concatenated:: casper.start('http://google.com/search?q=foo', function() { this.echo(this.fetchText('h3')); }).run(); ``forward()`` ------------------------------------------------------------------------------- **Signature:** ``forward()`` Moves a step forward in browser's history:: casper.start('http://foo.bar/1') casper.thenOpen('http://foo.bar/2'); casper.thenOpen('http://foo.bar/3'); casper.back(); // http://foo.bar/2 casper.back(); // http://foo.bar/1 casper.forward(); // http://foo.bar/2 casper.run(); Also have a look at `back()`_. .. _casper_log: .. index:: Logging ``log()`` ------------------------------------------------------------------------------- **Signature:** ``log(String message[, String level, String space])`` Logs a message with an optional level in an optional space. Available levels are ``debug``, ``info``, ``warning`` and ``error``. A space is a kind of namespace you can set for filtering your logs. By default, Casper logs messages in two distinct spaces: ``phantom`` and ``remote``, to distinguish what happens in the PhantomJS environment from the remote one:: casper.start('http://www.google.fr/', function() { this.log("I'm logging an error", "error"); }); casper.run(); .. _casper_fill: .. index:: Form ``fill()`` ------------------------------------------------------------------------------- **Signature:** ``fill(String selector, Object values[, Boolean submit])`` Fills the fields of a form with given values and optionally submits it. Fields are referenced by their ``name`` attribute. .. versionchanged:: 1.1 To use :doc:`CSS3 or XPath selectors <../selectors>` instead, check the `fillSelectors()`_ and `fillXPath()`_ methods. Example with this sample html form: .. code-block :: html
Mr Mrs Receive a copy
A script to fill and submit this form:: casper.start('http://some.tld/contact.form', function() { this.fill('form#contact-form', { 'subject': 'I am watching you', 'content': 'So be careful.', 'civility': 'Mr', 'name': 'Chuck Norris', 'email': 'chuck@norris.com', 'cc': true, 'attachment': '/Users/chuck/roundhousekick.doc' }, true); }); casper.then(function() { this.evaluateOrDie(function() { return /message sent/.test(document.body.innerText); }, 'sending message failed'); }); casper.run(function() { this.echo('message sent').exit(); }); The ``fill()`` method supports single selects in the same way as text input. For multiple selects, supply an array of values to match against: .. code-block :: html
A script to select multiple options for category in this form: .. code-block :: javascript casper.then(function() { this.fill('form#contact-form', { 'categories': ['0', '1'] // Friends and Family }); }); .. warning:: 1. The ``fill()`` method currently can't fill **file fields using XPath selectors**; PhantomJS natively only allows the use of CSS3 selectors in its ``uploadFile()`` method, hence this limitation. 2. Please Don't use CasperJS nor PhantomJS to send spam, or I'll be calling the Chuck. More seriously, please just don't. ``fillSelectors()`` ------------------------------------------------------------------------------- **Signature:** ``fillSelectors(String selector, Object values[, Boolean submit])`` .. versionadded:: 1.1 Fills form fields with given values and optionally submits it. Fields are referenced by ``CSS3`` selectors:: casper.start('http://some.tld/contact.form', function() { this.fillSelectors('form#contact-form', { 'input[name="subject"]': 'I am watching you', 'input[name="content"]': 'So be careful.', 'input[name="civility"]': 'Mr', 'input[name="name"]': 'Chuck Norris', 'input[name="email"]': 'chuck@norris.com', 'input[name="cc"]': true, 'input[name="attachment"]': '/Users/chuck/roundhousekick.doc' }, true); }); ``fillLabels()`` ------------------------------------------------------------------------------- **Signature:** ``fillLabels(String selector, Object values[, Boolean submit])`` .. versionadded:: 1.1 Fills a form with provided field values using associated label text Fields are referenced by label content values:: casper.start('http://some.tld/contact.form', function() { this.fillLabels('form#contact-form', { Email: 'chuck@norris.com', Password: 'chuck', Content: 'Am watching thou', Check: true, No: true, Topic: 'bar', Multitopic: ['bar', 'car'], File: fpath, "1": true, "3": true, Strange: "very" }, true); }); ``fillXPath()`` ------------------------------------------------------------------------------- **Signature:** ``fillXPath(String selector, Object values[, Boolean submit])`` .. versionadded:: 1.1 Fills form fields with given values and optionally submits it. While the ``form`` element is always referenced by a CSS3 selector, fields are referenced by ``XPath`` selectors:: casper.start('http://some.tld/contact.form', function() { this.fillXPath('form#contact-form', { '//input[@name="subject"]': 'I am watching you', '//input[@name="content"]': 'So be careful.', '//input[@name="civility"]': 'Mr', '//input[@name="name"]': 'Chuck Norris', '//input[@name="email"]': 'chuck@norris.com', '//input[@name="cc"]': true, }, true); }); .. warning:: The ``fillXPath()`` method currently can't fill **file fields using XPath selectors**; PhantomJS natively only allows the use of CSS3 selectors in its ``uploadFile()`` method, hence this limitation. .. index:: URL ``getCurrentUrl()`` ------------------------------------------------------------------------------- **Signature:** ``getCurrentUrl()`` Retrieves current page URL. Note that the url will be url-decoded:: casper.start('http://www.google.fr/', function() { this.echo(this.getCurrentUrl()); // "http://www.google.fr/" }); casper.run(); .. index:: DOM ``getElementAttribute()`` ------------------------------------------------------------------------------- **Signature:** ``getElementAttribute(String selector, String attribute)`` .. versionadded:: 1.0 Retrieves the value of an attribute on the first element matching the provided :doc:`selector <../selectors>`:: var casper = require('casper').create(); casper.start('http://www.google.fr/', function() { require('utils').dump(this.getElementAttribute('div[title="Google"]', 'title')); // "Google" }); casper.run(); .. index:: DOM ``getElementsAttribute()`` ------------------------------------------------------------------------------- **Signature:** ``getElementsAttribute(String selector, String attribute)`` .. versionadded:: 1.1 Retrieves the values of an attribute on each element matching the provided :doc:`selector <../selectors>`:: var casper = require('casper').create(); casper.start('http://www.google.fr/', function() { require('utils').dump(this.getElementsAttribute('div[title="Google"]', 'title')); // "['Google']" }); casper.run(); .. index:: DOM ``getElementBounds()`` ------------------------------------------------------------------------------- **Signature:** ``getElementBounds(String selector, Boolean page)`` Retrieves boundaries for a DOM element matching the provided :doc:`selector <../selectors>`. If you have frames or/and iframes, set 'true' to the page parameter. It returns an Object with four keys: ``top``, ``left``, ``width`` and ``height``, or ``null`` if the selector doesn't exist:: var casper = require('casper').create(); casper.start('http://www.google.fr/', function() { require('utils').dump(this.getElementBounds('div[title="Google"]')); }); casper.run(); This will output something like:: { "height": 95, "left": 352, "top": 16, "width": 275 } .. index:: DOM ``getElementsBounds()`` ------------------------------------------------------------------------------- **Signature:** ``getElementsBounds(String selector)`` .. versionadded:: 1.0 Retrieves a list of boundaries for all DOM elements matching the provided :doc:`selector <../selectors>`. It returns an array of objects with four keys: ``top``, ``left``, ``width`` and ``height`` (see `getElementBounds()`_). .. _casper_getelementinfo: .. index:: DOM ``getElementInfo()`` ------------------------------------------------------------------------------- **Signature:** ``getElementInfo(String selector)`` .. versionadded:: 1.0 Retrieves information about the first element matching the provided :doc:`selector <../selectors>`:: casper.start('http://google.fr/', function() { require('utils').dump(this.getElementInfo('#hplogo')); }); Gives something like:: { "attributes": { "align": "left", "dir": "ltr", "id": "hplogo", "onload": "window.lol&&lol()", "style": "height:110px;width:276px;background:url(/images/srpr/logo1w.png) no-repeat", "title": "Google" }, "height": 110, "html": "
France
", "nodeName": "div", "tag": "
France
", "text": "France\n", "visible": true, "width": 276, "x": 62, "y": 76 } .. note:: This method **does not** return a DOM element, only a simple object representation of it; this is because the casper environment has no direct access to the scraped page one. .. index:: DOM ``getElementsInfo()`` ------------------------------------------------------------------------------- **Signature:** ``getElementsInfo(String selector)`` .. versionadded:: 1.1 Retrieves information about all elements matching the provided :doc:`selector <../selectors>`:: casper.start('http://google.fr/', function() { require('utils').dump(this.getElementsInfo('#hplogo')); }); Gives something like:: [ { "attributes": { "align": "left", "dir": "ltr", "id": "hplogo", "onload": "window.lol&&lol()", "style": "height:110px;width:276px;background:url(/images/srpr/logo1w.png) no-repeat", "title": "Google" }, "height": 110, "html": "
France
", "nodeName": "div", "tag": "
France
", "text": "France\n", "visible": true, "width": 276, "x": 62, "y": 76 } ] .. note:: This method **does not** return a ``NodeList``, only a simple array of object representations of matching elements; this is because the casper environment has no direct access to the scraped page one. .. index:: Form ``getFormValues()`` ------------------------------------------------------------------------------- **Signature:** ``getFormValues(String selector)`` .. versionadded:: 1.0 Retrieves a given form all of its field values:: casper.start('http://www.google.fr/', function() { this.fill('form', {q: 'plop'}, false); this.echo(this.getFormValues('form').q); // 'plop' }); casper.run(); .. index:: Globals, window ``getGlobal()`` ------------------------------------------------------------------------------- **Signature:** ``getGlobal(String name)`` Retrieves a global variable value within the remote DOM environment by its name. Basically, ``getGlobal('foo')`` will retrieve the value of ``window.foo`` from the page:: casper.start('http://www.google.fr/', function() { this.echo(this.getGlobal('innerWidth')); // 1024 }); casper.run(); .. index:: Debugging ``getHTML()`` ------------------------------------------------------------------------------- **Signature:** ``getHTML([String selector, Boolean outer])`` .. versionadded:: 1.0 Retrieves HTML code from the current page. By default, it outputs the whole page HTML contents:: casper.start('http://www.google.fr/', function() { this.echo(this.getHTML()); }); casper.run(); The ``getHTML()`` method can also dump HTML contents matching a given :doc:`selector <../selectors>`; for example with this HTML code: .. code-block:: html

Plop

You can fetch those contents using:: casper.start('http://www.site.tld/', function() { this.echo(this.getHTML('h1#foobar')); // => 'Plop' }); The ``outer`` argument allows to retrieve the outer HTML contents of the matching element:: casper.start('http://www.site.tld/', function() { this.echo(this.getHTML('h1#foobar', true)); // => '

Plop

' }); ``getPageContent()`` ------------------------------------------------------------------------------- **Signature:** ``getPageContent()`` .. versionadded:: 1.0 Retrieves current page contents, dealing with exotic other content types than HTML:: var casper = require('casper').create(); casper.start().then(function() { this.open('http://search.twitter.com/search.json?q=casperjs', { method: 'get', headers: { 'Accept': 'application/json' } }); }); casper.run(function() { require('utils').dump(JSON.parse(this.getPageContent())); this.exit(); }); .. index:: DOM ``getTitle()`` ------------------------------------------------------------------------------- **Signature:** ``getTitle()`` Retrieves current page title:: casper.start('http://www.google.fr/', function() { this.echo(this.getTitle()); // "Google" }); casper.run(); .. _casper_mouseevent: .. index:: events ``mouseEvent()`` ------------------------------------------------------------------------------- **Signature:** ``mouseEvent(String type, String selector, [Number|String X, Number|String Y])`` .. versionadded:: 0.6.9 .. versionupdate:: 1.1.0-beta6 Triggers a mouse event on the first element found matching the provided selector. Supported events are ``mouseup``, ``mousedown``, ``click``, ``dblclick``, ``mousemove``, ``mouseover``, ``mouseout`` and for phantomjs >= 1.9.8 ``mouseenter``, ``mouseleave`` and ``contextmenu``. .. warning:: The list of supported events depends on the version of the engine in use. Older engines only provide partial support. For best support use recent builds of PhantomJS or SlimerJS.":: casper.start('http://www.google.fr/', function() { this.mouseEvent('click', 'h2 a', "20%", "50%"); }); casper.run(); ``newPage()`` ------------------------------------------------------------------------------- **Signature:** ``newPage()`` .. versionadded:: 1.1 **Only available since version 1.1.0.** Creates a new WebPage instance:: casper.start('http://google.com', function() { // ... }); casper.then(function() { casper.page = casper.newPage(); casper.open('http://yahoo.com').then( function() { // .... }); }); casper.run(); .. index:: HTTP, HTTP Request, HTTP Method, HTTP Headers ``open()`` ------------------------------------------------------------------------------- **Signature:** ``open(String location, Object Settings)`` Performs an HTTP request for opening a given location. You can forge ``GET``, ``POST``, ``PUT``, ``DELETE`` and ``HEAD`` requests. Example for a standard ``GET`` request:: casper.start(); casper.open('http://www.google.com/').then(function() { this.echo('GOT it.'); }); casper.run(); Example for a ``POST`` request:: casper.start(); casper.open('http://some.testserver.com/post.php', { method: 'post', data: { 'title': 'Plop', 'body': 'Wow.' } }); casper.then(function() { this.echo('POSTED it.'); }); casper.run(); To pass nested parameters arrays:: casper.open('http://some.testserver.com/post.php', { method: 'post', data: { 'standard_param': 'foo', 'nested_param[]': [ // please note the use of square brackets! 'Something', 'Something else' ] } }); .. versionadded:: 1.0 To POST some data with utf-8 encoding:: casper.open('http://some.testserver.com/post.php', { method: 'post', headers: { 'Content-Type': 'application/json; charset=utf-8' }, encoding: 'utf8', // not enforced by default data: { 'table_flip': '(╯°□°)╯︵ ┻━┻ ', } }); .. versionadded:: 1.1 You can also set custom request headers to send when performing an outgoing request, passing the ``headers`` option:: casper.open('http://some.testserver.com/post.php', { method: 'post', data: { 'title': 'Plop', 'body': 'Wow.' }, headers: { 'Accept-Language': 'fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3' } }); ``reload()`` ------------------------------------------------------------------------------- **Signature:** ``reload([Function then])`` .. versionadded:: 1.0 Reloads current page location:: casper.start('http://google.com', function() { this.echo("loaded"); this.reload(function() { this.echo("loaded again"); }); }); casper.run(); ``repeat()`` ------------------------------------------------------------------------------- **Signature:** ``repeat(int times, Function then)`` Repeats a navigation step a given number of times:: casper.start().repeat(3, function() { this.echo("Badger"); }); casper.run(); .. _casper_resourceexists: .. index:: HTTP ``resourceExists()`` ------------------------------------------------------------------------------- **Signature:** ``resourceExists(String|Function|RegExp test)`` Checks if a resource has been loaded. You can pass either a function, a string or a ``RegExp`` instance to perform the test:: casper.start('http://www.google.com/', function() { if (this.resourceExists('logo3w.png')) { this.echo('Google logo loaded'); } else { this.echo('Google logo not loaded', 'ERROR'); } }); casper.run(); .. note:: If you want to wait for a resource to be loaded, use the `waitForResource()`_ method. .. index:: Step stack, run ``run()`` ------------------------------------------------------------------------------- **Signature:** ``run(fn onComplete[, int time])`` Runs the whole suite of steps and optionally executes a callback when they've all been done. Obviously, **calling this method is mandatory** in order to run the Casper navigation suite. Casper suite **won't run**:: casper.start('http://foo.bar/home', function() { // ... }); // hey, it's missing .run() here! Casper suite **will run**:: casper.start('http://foo.bar/home', function() { // ... }); casper.run(); ``Casper.run()`` also accepts an ``onComplete`` callback, which you can consider as a custom final step to perform when all the other steps have been executed. Just don't forget to ``exit()`` Casper if you define one!:: casper.start('http://foo.bar/home', function() { // ... }); casper.then(function() { // ... }); casper.run(function() { this.echo('So the whole suite ended.'); this.exit(); // <--- don't forget me! }); Binding a callback to ``complete.error`` will trigger when the ``onComplete`` callback fails. .. index:: Scroll ``scrollTo()`` ------------------------------------------------------------------------------- **Signature:** ``scrollTo(Number x, Number y)`` .. versionadded:: 1.1-beta3 Scrolls current document to the coordinates defined by the value of ``x`` and ``y``:: casper.start('http://foo.bar/home', function() { this.scrollTo(500, 300); }); .. note:: This operation is synchronous. .. index:: Scroll ``scrollToBottom()`` ------------------------------------------------------------------------------- **Signature:** ``scrollToBottom()`` .. versionadded:: 1.1-beta3 Scrolls current document to its bottom:: casper.start('http://foo.bar/home', function() { this.scrollToBottom(); }); .. note:: This operation is synchronous. .. index:: Form ``sendKeys()`` ------------------------------------------------------------------------------- **Signature:** ``sendKeys(Selector selector, String keys[, Object options])`` .. versionadded:: 1.0 Sends native keyboard events to the element matching the provided :doc:`selector <../selectors>`:: casper.then(function() { this.sendKeys('form.contact input#name', 'Duke'); this.sendKeys('form.contact textarea#message', "Damn, I'm looking good."); this.click('form.contact input[type="submit"]'); }); .. versionadded:: 1.1 The currently supported HTMLElements that can receive keyboard events from ``sendKeys`` are ````, ``