Verify the page scroll position after returning to previous page

Is it possible to verify the scroll position on a page? If I am on center of the catalog screen where I select a product to go to its description screen. If I go back, I need to verify that the page is scrolled to the same position where the product was selected rather than the page staying at the top of the screen.

You possibly can use the element’s X and Y locations but I don’t know how much leeway you should give yourself to state the element is in “the same place”. I believe there are now location settings in the WebUI list of statements, like getting the top position of an element, etc. or getElementLeftPosition()

Or maybe:
import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement

def driver = DriverFactory.getWebDriver();

buttonItem = driver.findElement(By.xpath('id("myPage")/button'));

myLastXPos = buttonItem.getLocation().getX()
myLastYPos = buttonItem.getLocation().getY()

//pop out & do your whatever

"back and now compare locations"
newItem = driver.findElement(By.xpath('id("myPage")/button'));
WebUI.verifyMatch(newItem.isDisplayed().toString(), "true", false)
WebUI.verifyEqual(newItem.getLocation().getX(), myLastXPos )
WebUI.verifyEqual(newItem.getLocation().getY(), myLastYPos)

WebUI.verifyLessThan((newItem.getLocation().getY(), myLastYPos)
WebUI.verifyGreaterThan((newItem.getLocation().getY(), myLastYPos)

Thanks, so its not about the element being at the same place but more about the position of the scroll bar (that it does not stay at (0,0) after going back to previous screen).

Do you know if its possible to verify that current location is not (0,0). With that I can verify that the scroll is not on top of the screen.

How about trying WebUI.getViewportTopPosition() to see what it returns?

https://docs.katalon.com/javadoc/com/kms/katalon/core/webui/keyword/WebUiBuiltInKeywords.html

1 Like

I don’t think your Katalon script can know the position of the scroll bar. Even your javascript will not be able to.

I think this would enough to see if your current viewport is at the top or not:

if (WebUI.getViewportTopPosition() == 0) {
    WebUI.comment("I'm on the top of the world!")
} else {
    WebUI.comment("sigh")
}