How to scroll
Sahi cannot record scroll bar movement.But it is possible to simulate scrolling from a Sahi script.
The first thing to do is to identify the scroll bar inside a container element, using the controller.
- Most likely, the scroll bar belongs to the container element itself, so it will be identified as the container itself. For example, a div.
- Or the scrollbar can be a separate element in itself. For example, an image or another div.
To scroll vertically, you can use the following.
_call(_div("identifier").scrollTop=100); // 100 is in pixels.
OR
_call(_div("identifier").scrollTo(0,100));
To scroll horizontally, you can use the following.
_call(_div("identifier").scrollLeft=200); // 200 is in pixels.
OR
_call(_div("identifier").scrollTo(200,0));
info
NOTE: We are essentially using JavaScript logic with _call().
If the scrollbar is for the webpage, you can use the following code.
_call(document.body.scrollTop=800); // Scroll down.
_wait(2000); // Wait 2 seconds
_call(document.body.scrollTop=100); // Scroll back up.
_call(window.scrollTo(0,800)); // Scroll down.
_wait(2000); // Wait 2 seconds
_call(window.scrollTo(0,100)); // Scroll back up.
For using scrollTop, use
document.body.scrollTop
. For scrollTo(), use window.scrollTo
.
info
NOTE that the above scripts will work on mobile devices too.
Scrollbar as a separate element
You can use _dragDropXY() API to drag the scroll bar.
Let's say you have identified the scrollbar as _image("scroll");
To scroll vertically, you can use the following.
_dragDropXY(_image("scroll"),0,300,true) // Will drag the item vertically by 300 pixels. The last parameter should be true.
To scroll horizontally, you can use the following.
_dragDropXY(_image("scroll"),200,0,true) // Will drag the item horizontally by 200 pixels. The last parameter should be true.
info
NOTE that the above scripts will work on mobile devices too.
Check movement of scrollbar
question
How can I check the movement of the vertical scroll bar i.e whether it is moving from bottom to top, when I click on a button?
answer
Do the following to verify that the scroll bar goes to top after you click on the specific button, say Submit.
var $currentPos=window.document.body.scrollTop; // Gives current position of Scroll bar
_click(_button("Submit"));
var $changedPos=window.document.body.scrollTop;
if ($changedPos < $currentPos) {
_log("Scroll bar has moved up");
}
If you want to check if scroll bar is right at the top, you can use below code.
if (window.document.body.scrollTop== 0) {
_log("Scroll bar is at the Top");
}