0

I used an existing script in my project to scroll a page. The scroll does work without any issue. After the review of my scripts, it was commented that the scroll script I used is of fragile movement. I want to understand how the script was analyzed to be fragile. I'm using java and Cucumber framework. Can someone help to understand the issue

JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("document.getElementById('ContentScrollView').scrollTop= 450");
  • What does "fragile movement" mean? – acejazz Feb 6 '18 at 9:11
  • @acejazz It means that the script written for scroll is fragile. Not implying to the standards of scripting. I have taken another solution for the mentioned piece of code. I wanted to know how you evaluate the given script for scrolling as fragile. This can help me in future too. – K.K Feb 6 '18 at 9:33
  • Why don't you ask the commenter some advice? We could come up with some solution he/she will comment anyway, so I suggest you to talk with him and learn what are his/her points of view. – acejazz Feb 6 '18 at 9:39
  • 1
    The scroll works different with different screen size of the system on which the test will be running. This is the reason what I got from the commentator. Wanted to see if there would be different reasons for the mentioned scenario – K.K Feb 6 '18 at 9:44
  • 1
    What are you trying to achieve with that scroll, exactly? – acejazz Feb 6 '18 at 9:48
2

Not all screens are the same size , your scroll method would be less fragile if it examined the situation first.

For example scrolling 450 pixels on screen that is 1920 pixels wide is a tiny movement, but on a screen 640 pixels wide it's huge.

Read the screen size to ensure you are trying to scroll be the right amount, then calculate the amount to scroll relative to that size.

You should also be aware of rotation and that this can cause the screen dimensions to flip in the middle of your scrolling. You should have a test case for this.

The safest way to scroll is to do so in small amounts and then check the scroll happened and the no errors happened before scrolling further. It's also possible for your elements to disappear when scrolling them (especially in a browser) or fail to load before you use them, if you use a compiled language for your tests this will need robust handling to avoid spurious test fails.

  • Thanks for the explanation. I changed the scrolling script from the pixel based movement to the element based movement JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("arguments[0].scrollIntoView(true);",SingleClick); – K.K Feb 6 '18 at 11:26
  • you should be able to see the source of that function somewhere and be able to see if checks the size , it might well do that already. – Amias Feb 6 '18 at 15:34
0

If you have to scroll to the bottom of the page and do some other action, you can use the following JavaScript:

window.scrollTo(0,document.body.scrollHeight);

inside of the executeScript function and then you can proceed with the other actions.

If you want to scroll where that element is you can use the following JavaScript:

document.getElementById("divFirst").scrollIntoView();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.