May 13, 2011

How to Scroll the page

We can scroll the page by using FOCUS command...


focus(locator)
Arguments:
Move the focus to the specified element; for example, if the element is an input field, move the cursor to that field.
Basically there is no specific command for scrolling the page down. I tried keyPress and all other options, but i felt Focus is the best for scrolling the page down. We need to select a element and set focus on that element.

Below is the example...


package reporting;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

public class Focus extends SeleneseTestCase {
public SeleniumServer aa;
@Before
public void setUp() throws Exception {
aa= new SeleniumServer();
aa.start();
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.com/");
selenium.start();
}

@Test
public void testFocus() throws Exception {
selenium.open("/");
selenium.windowMaximize();
selenium.type("q", "selenium wiki");
selenium.click("btnG");
//Waiting for an element
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (selenium.isElementPresent("link=Selenium Overview - Wiki - Liferay.com")) break; } catch (Exception e) {}
Thread.sleep(1000);
}
//Set focus on element --then page will scroll down --focus will be on that element
selenium.focus("link=Selenium Overview - Wiki - Liferay.com");
//To capture a screenshot
selenium.captureScreenshot("c:/naga/screenshot.png");
Thread.sleep(10000);
}



@After
public void tearDown() throws Exception {
selenium.stop();
aa.stop();
}
}




5 comments: