TestStack / TestStack.Seleno

Seleno helps you write automated UI tests in the right way by implementing Page Objects and Page Components and by reading from and writing to web pages using strongly typed view models.
http://teststack.github.com/TestStack.Seleno/
MIT License
180 stars 60 forks source link

How to do a scroll down (go to the top; go to the centre) on the page? #213

Closed sirj77 closed 9 years ago

sirj77 commented 9 years ago

Hi, just a simple question: How to do a scroll down to the bottom of the page properly? (this question is also for the go to the top/center of the page). My tests are needed to click on some element, but this element is located of the bottom of the page.

I tried to do like:

public void ScrollDownToTheBottomOfThePage()
{
 Browser.ExecuteJavaScript<??>("window.scrollTo(0, document.body.scrollHeight - 150)");
}

But I don't understand what I should put into ExecuteJavaScript<??> that my action would happen correctly.

robdmoore commented 9 years ago

Can you use Find.Element and then .Click?

Pretty sure web driver automatically scrolls when you do that...

sirj77 commented 9 years ago

Yes, you are right, but what if I want to see the actions of clicking in a browser?

robdmoore commented 9 years ago

It should be visible to you while it happens?

sirj77 commented 9 years ago

Just for the test purpose, it shouldn't be visible for me, actually :)

robdmoore commented 9 years ago

Right. So you want the Visible property of the IWebElement returned from Find.Element to be true?

robdmoore commented 9 years ago

Try this (without the click and changed for C#): http://stackoverflow.com/a/12042268

sirj77 commented 9 years ago

robdmoore, thank you

robdmoore commented 9 years ago

Did that work for you? Feel free to post the final C# code so other people that find this know how to do it

sirj77 commented 9 years ago

Well, I just wanted to observe the scroll down action in Browser while the tests are running. I've tried that piece of code that you sent me, but it didn't work, got some error, I tried it in a bit simple way (without scrolling to the particular element on the page, just scrolled down to the bottom of the page) and it works for me:

        public void ScrollDownToTheBottomOfThePage()
        {
            var js = String.Format("window.scrollTo(0, document.body.scrollHeight - 150)");
            Execute.Script(js);
        }