4

As part of a test suite measuring FPS for a web application I need to perform a smooth scroll of the web page. That is, the same smoothness as when a user grabs the scroll bar and moves it with the mouse.

So far I have tried by using simulating key presses with sikuli, i.e. pressing the arrow up/down keys multiple times to scroll the whole page. I have also tried using a Javascript approach:

public void scrollSmooth(int durationOfScroll){
    long timeWhenStarting = System.currentTimeMillis() / 1000L;
while (System.currentTimeMillis() / 1000L - timeWhenStarting < durationOfScroll) {
    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,10)", "");
    }
}

Both these approaches fails to fulfill their purpose, as they both generate a step-by-step scroll, which is not suitable when I simultaneously want to measure the FPS (e.g. the smoothnes of the page when scrolling).

AxelPaxel
  • 133
  • 1
  • 2
  • 7

4 Answers4

5

The solution was a lot more simple than expected. Instead of using a time-based condition for the loop I tried the following:

public void scrollSmooth(){
    for(int i=0;i<6000;i++) {
        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,1)", "");
    }
}

This works well, with the small downside that I cannot specify the length (in time) of the scroll, only the actual pixels to be scrolled.

Agent Shoulder
  • 555
  • 6
  • 22
  • JavascriptExecutor jse = (JavascriptExecutor)driver; int ch = ((Number) jse.executeScript("return document.body.scrollHeight")).intValue(); try{ if(scroll.equals("Up to Down Scroll")){ for(int i=0;i – A.Aleem11 Mar 02 '19 at 16:25
  • @A.Aleem11- Thanks for contributing! Your code seems to return the current scroll position, which could sometimes be usable. 'scroll' is not defined though, and you seem to be passing some kind of string variable to "toggle" the scroll function, which is probably not needed for this purpose. BR – Agent Shoulder Dec 26 '19 at 22:43
  • 1
    @AgentShoulder fantastic – D.JCode Jul 03 '21 at 22:06
  • 1
    @D.JCode upvote the solution if it worked for you, thx! – Agent Shoulder Jul 15 '21 at 04:05
1

Two methods scrollDown and scrollUp,Hope it 'll help. :)

 /**
 * scrollDown() method scrolls down the page.
 *
 * @return void
 */
public void scrollDown(WebDriver driver) {
    try {
        int i=0;
        for(;i<=30;i++) {
            ((JavascriptExecutor) driver).executeScript(("window.scrollBy(0,"+i+")"), "");
        }
        for(;i>0;i--) {
            ((JavascriptExecutor) driver).executeScript(("window.scrollBy(0,"+i+")"), "");
        }
    } catch (WebDriverException wde) {
    } catch (Exception e) {
    }
}

/**
 * scrollUp() method scrolls up the page.
 *
 * @return void
 */
public void scrollUp(WebDriver driver) {
    try {
        int i=0;
        for(;i>-30;i--) {
            ((JavascriptExecutor) driver).executeScript(("window.scrollBy(0,"+i+")"), "");
        }
        for(;i<0;i++) {
            ((JavascriptExecutor) driver).executeScript(("window.scrollBy(0,"+i+")"), "");
        }
    } catch (WebDriverException wde) {
    } catch (Exception e) {
    }
}
1
html = driver.find_element(By.XPATH,'//body')
total_scroled = 0
page_height = driver.execute_script("return document.body.scrollHeight")
while total_scroled < page_height:
    html.send_keys(Keys.PAGE_DOWN)
    total_scroled += 400
    time.sleep(.5)
Souvik98
  • 11
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 14 '22 at 18:55
0

Here is a version for Selenium with Python -

from selenium import webdriver
import chromedriver_autoinstaller
from time import sleep

chromedriver_autoinstaller.install()
driver = webdriver.Chrome()

def scroll_down(driver):
    page_height = driver.execute_script("return document.body.scrollHeight")
    total_scrolled = 0
    for i in range(page_height):
        driver.execute_script(f'window.scrollBy(0,{i});')
        total_scrolled += i
        if total_scrolled >= page_height/2:
            last_no = i
            break
            
    for i in range(last_no, 0, -1):
        driver.execute_script(f'window.scrollBy(0,{i});')


def scroll_up(driver):
    page_height = driver.execute_script("return document.body.scrollHeight")
    total_scrolled = 0
    for i in range(0, -page_height, -1):
        driver.execute_script(f'window.scrollBy(0,{i});')
        total_scrolled += i
        if total_scrolled <= -page_height/2:
            last_no = i
            break
    for i in range(last_no, 0):
        driver.execute_script(f'window.scrollBy(0,{i});')

scroll_down(driver)
sleep(2)
scroll_up(driver)