Commit c9007eb5 c9007eb5786ea97a07a858cd396c558f69b37793 by Nicolas Perriault

added Casper#scrollTo & Casper#scrollToBottom

1 parent ce773a0d
......@@ -1453,6 +1453,40 @@ Casper suite **will run**::
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()``
......
......@@ -1600,6 +1600,30 @@ Casper.prototype.sendKeys = function(selector, keys, options) {
};
/**
* Scrolls current document to x, y coordinates.
*
* @param {Number} x X position
* @param {Number} y Y position
* @return {Casper}
*/
Casper.prototype.scrollTo = function(x, y) {
"use strict";
this.callUtils("scrollTo", x, y);
return this;
};
/**
* Scrolls current document up to its bottom.
*
* @return {Casper}
*/
Casper.prototype.scrollToBottom = function scrollToBottom() {
"use strict";
this.callUtils("scrollToBottom");
return this;
};
/**
* Sets current page content.
*
* @param String content Desired page content
......
......@@ -724,6 +724,23 @@
};
/**
* Scrolls current document to x, y coordinates.
*
* @param {Number} x X position
* @param {Number} y Y position
*/
this.scrollTo = function scrollTo(x, y) {
window.scrollTo(parseInt(x || 0, 10), parseInt(y || 0, 10));
};
/**
* Scrolls current document up to its bottom.
*/
this.scrollToBottom = function scrollToBottom() {
this.scrollTo(0, this.getDocumentHeight());
},
/**
* Performs an AJAX request.
*
* @param String url Url.
......
/*global casper*/
/*jshint strict:false*/
casper.test.begin('Casper.scrollTo()', 2, function(test) {
casper.start().then(function() {
this.setContent('<div style="width:2000px;height:2000px">large div is large</div>');
this.scrollTo(1000, 1000);
test.assertEquals(this.getGlobal("scrollX"), 1000, "scrollTo() scrolls to X position");
test.assertEquals(this.getGlobal("scrollY"), 1000, "scrollTo() scrolls to Y position");
});
casper.run(function() {
test.done();
});
});
casper.test.begin('Casper.scrollToBottom()', 1, function(test) {
casper.start().then(function() {
this.setContent('<div style="height:2000px">long div is long</div>');
this.scrollToBottom();
test.assertEval(function() {
/*global __utils__*/
return __utils__.getDocumentHeight() - window.innerHeight === window.scrollY;
}, "scrollToBottom() scrolls to max Y by default");
});
casper.run(function() {
test.done();
});
});