Commit 9c546d59 9c546d59117bd35dbe3f94b127d5cee951f44d6b by Nicolas Perriault

Merge pull request #725 from n1k0/casper-scroll-methods

Scroll helpers
2 parents ce773a0d c9007eb5
......@@ -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();
});
});