<commit_before>.. _selectors:

.. index:: selector, DOM, HTML

=========
Selectors
=========

CasperJS makes an heavy use of selectors in order to work with the `DOM <http://www.w3.org/TR/dom/>`_, and can transparently use either `CSS3 <http://www.w3.org/TR/selectors/>`_ or `XPath <http://www.w3.org/TR/xpath/>`_ expressions.

All the examples below are based on this HTML code:

.. code-block:: html

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>My page</title>
    </head>
    <body>
        <h1 class="page-title">Hello</h1>
        <ul>
            <li>one</li>
            <li>two</li>
            <li>three</li>
        </ul>
        <footer><p>©2012 myself</p></footer>
    </body>
    </html>

.. index:: CSS, CSS3

CSS3
----

By default, CasperJS accepts `CSS3 selector strings <http://www.w3.org/TR/selectors/#selectors>`_ to check for elements within the DOM.

To check if the ``<h1 class="page-title">`` element exists in the example page, you can use::

    var casper = require('casper').create();

    casper.start('http://domain.tld/page.html', function() {
        if (this.exists('h1.page-title')) {
            this.echo('the heading exists');
        }
    });

    casper.run();

Or if you're using the :doc:`testing framework <testing>`::

    casper.test.begin('The heading exists', 1, function suite(test) {
        casper.start('http://domain.tld/page.html', function() {
            this.assertExists('h1.page-title');
        }).run(function() {
            test.done();
        });
    });

Some other convenient testing methods are relying on selectors::

    casper.test.begin('Page content tests', 3, function suite(test) {
        casper.start('http://domain.tld/page.html', function() {
            this.assertExists('h1.page-title');
            this.assertSelectorHasText('h1.page-title', 'Hello');
            this.assertVisible('footer');
        }).run(function() {
            test.done();
        });
    });

.. index:: XPath

XPath
-----<commit_msg>refs #545 - Copy update in docs. Updated the selector doc page to use 'test' instead of 'this' since tester has been made a module.<commit_after>.. _selectors:

.. index:: selector, DOM, HTML

=========
Selectors
=========

CasperJS makes an heavy use of selectors in order to work with the `DOM <http://www.w3.org/TR/dom/>`_, and can transparently use either `CSS3 <http://www.w3.org/TR/selectors/>`_ or `XPath <http://www.w3.org/TR/xpath/>`_ expressions.

All the examples below are based on this HTML code:

.. code-block:: html

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>My page</title>
    </head>
    <body>
        <h1 class="page-title">Hello</h1>
        <ul>
            <li>one</li>
            <li>two</li>
            <li>three</li>
        </ul>
        <footer><p>©2012 myself</p></footer>
    </body>
    </html>

.. index:: CSS, CSS3

CSS3
----

By default, CasperJS accepts `CSS3 selector strings <http://www.w3.org/TR/selectors/#selectors>`_ to check for elements within the DOM.

To check if the ``<h1 class="page-title">`` element exists in the example page, you can use::

    var casper = require('casper').create();

    casper.start('http://domain.tld/page.html', function() {
        if (this.exists('h1.page-title')) {
            this.echo('the heading exists');
        }
    });

    casper.run();

Or if you're using the :doc:`testing framework <testing>`::

    casper.test.begin('The heading exists', 1, function suite(test) {
        casper.start('http://domain.tld/page.html', function() {
            test.assertExists('h1.page-title');
        }).run(function() {
            test.done();
        });
    });

Some other convenient testing methods are relying on selectors::

    casper.test.begin('Page content tests', 3, function suite(test) {
        casper.start('http://domain.tld/page.html', function() {
            test.assertExists('h1.page-title');
            test.assertSelectorHasText('h1.page-title', 'Hello');
            test.assertVisible('footer');
        }).run(function() {
            test.done();
        });
    });

.. index:: XPath

XPath
-----
{'fraction_non_alphanumeric': 0.17478510028653296, 'fraction_numerical': 0.011222540592168099, 'mean_word_length': 2.4392446633825946, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3579121', 'n_tokens_mistral': 1360, 'n_tokens_neox': 1314, 'n_words': 335}