Get cross browser scrollheight through Selenium

I am working on a project that uses Selenium and I would like to try and get a complete cross browser and cross platform. IE8 is stubborn as always, is there anyone with an idea how to solve this?

Problem: When scrolling down the page, for example. 500px and you keep doing this until the bottom of the page, the final scroll will be less than 500px. I need to know how much was in this final scroll.

Two ways to solve: 1) Find the offset that has been scrolled every time (works everywhere except IE8) 2) Find the total height of the webpage

I know the jQuery height () function does this, but I cannot use this function from Selenium RC. If you know a way to call jQuery functions through Selenium or any other solution please let me know!

Cheers, Henry

+2


a source to share


1 answer


I found a solution to my problem. When you run tests with Selenium, it launches two windows: 1) A Selenium window that executes all commands 2) The browser window in which the website is tested.

When trying to get information about window 2 using JavaScript functions, you need to do the following: selenium.browserbot.getCurrentWindow ()



To get the full height of the browser window through the browser via selenium you need the following script:

function getPageHeight(){
    $scrOfY = 0;
    $test = $this->getEval("typeof(selenium.browserbot.getCurrentWindow().pageYOffset)");
    if(strcmp($test,"number") == 0) {
        //Netscape compliant
        $scrOfY = (int)$this->getEval("selenium.browserbot.getCurrentWindow().pageYOffset;");
        //scrOfX = window.pageXOffset;
    } else if( (bool)$this->getEval("selenium.browserbot.getCurrentWindow().document.body != null") && (bool)$this->getEval("selenium.browserbot.getCurrentWindow().document.body.scrollTop != null")) {
        //DOM compliant
        $scrOfY = (int)$this->getEval("selenium.browserbot.getCurrentWindow().document.body.scrollTop;");
        //scrOfX = document.body.scrollLeft;
    } else if( (bool)$this->getEval("selenium.browserbot.getCurrentWindow().document.documentElement != null") && (bool)$this->getEval("selenium.browserbot.getCurrentWindow().document.documentElement.scrollTop != null")) {
        //IE6 standards compliant mode
        $scrOfY = (int)$this->getEval("selenium.browserbot.getCurrentWindow().document.documentElement.scrollTop;");
        //scrOfX = document.documentElement.scrollLeft;
    }
    if(!$scrOfY || $scrOfY <= 0)
        $scrOfY = $this->getEval("selenium.browserbot.getCurrentWindow().document.body.offsetHeight");

    return $scrOfY;
}

      

+2


a source







All Articles