Professional Documents
Culture Documents
Next Blog
Create Blog
Aut hored
T estNG - Beginner's
Guide
Blog Archive
2013 (2)
2012 (4)
2011 (13)
October (3)
September (2)
August (2)
Native android app
automation
Nativedriver vs
Ro...
Scrolling on pages
using Selenium
July (6)
2010 (13)
Use Gist
Android (4)
Ant (3)
Automation (4)
Java (2)
The above code uses the JavaScript method "scrollBy" to scroll on the page. The for
loop runs for 60 sec and calls the scrollBy function every second. This makes the
selenium to scroll on the page.
If you have a test where you need to scroll on the page and check whether an element is
loaded dynamically or not you can put a isElementPresent like function after the
"driver.executeScript" to check visibility of your element.
Following is a test method written in webdriver that you can use to test the above
function on the Facebook page:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
?
public class FacebookScrollWebDriver {
RemoteWebDriver driver;
@Before
public void startup(){
DesiredCapabilities cap=new DesiredCapabilities();
cap.setJavascriptEnabled(true);
cap.setBrowserName("firefox");
try {
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@Test
public void testScroll() throws InterruptedException{
driver.get("http://www.facebook.com");
driver.findElement(By.id("email")).click();
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("facebook id");
driver.findElement(By.id("pass")).clear();
driver.findElement(By.id("pass")).sendKeys("facebook pswd");
driver.findElement(By.xpath("//input[@type='submit']")).click();
Thread.sleep(5000);
driver.switchTo().defaultContent();
//Following is the code that scrolls through the page
Sign In
Junit (2)
Locator Strategy (1)
Maven (2)
Selenium (11)
Selenium Grid (5)
Selenium Reports (2)
Selenium-2 (7)
TestNG (10)
Subscribe To
Posts
Comments
Follow by Email
Email address...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
}
@After
public void stop(){
driver.quit();
}
public boolean isElementPresent(RemoteWebDriver driver, By by){
try{
driver.findElement(by);
return true;
}catch(NoSuchElementException e){
return false;
}
}
}
Submit
About Me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Selenium selenium;
@Before
public void startup(){
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.facebook.com
selenium.start();
}
@Test
public void testScroll() throws InterruptedException{
selenium.open("/");
selenium.type("id=email", "facebook id");
selenium.type("id=pass", "facebook pswd");
selenium.click("//input[@type='submit']");
Thread.sleep(5000);
//Following is the code that scrolls through the page
for (int second = 0;; second++) {
if(second >=60){
break;
}
selenium.getEval("window.scrollBy(0,200)");
}
Thread.sleep(3000);
}
@After
public void stop(){
selenium.stop();
}
}
15 comments:
Arun May 24, 2012 at 3:21 AM
hi
thanks for the post
i want to scroll the list box in a webpage
ex:http://www.obout.com/ListBox/aspnet_ondemand_virtual_scroll.aspx
how to scroll the Asp.nte Listbox scroll bar?
Reply
Replies
Varun Menon
@Arun - Its a good problem to solve. But sorry I havent worked on such kind of thing.
But by looking at it you can go try following approaches.
- Use actions class implementation of selenium. Select an element and then use the
keyboard down button to load the whole list.
- Hover your mouse on the list box and then call the scroll option.
Reply
Thanks @Aneta
Reply
Scroll may be required when the element is loaded on scroll down as in the case of
facebook and many other apps.
0 & 200 is the x and y coordinates. Also the second argument of executeScript method
contains extra arguments that have to be given to the script. Please check the following
links:
http://www.w3schools.com/jsref/met_win_scrollby.asp
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/JavascriptE
xecutor.html#executeScript(java.lang.String, java.lang.Object...)
Reply
Comment as:
Publish
Google Account
Preview
Newer Post
Home
Older Post
All rights reserved by http://blog.varunin.com and author of the blog until explicitly mentioned. Simple template. Powered by Blogger.