How to scroll to a flex item if EnsureVisible is not working : How To …

How to scroll to a flex item if EnsureVisible is not working

Best practices, code snippets for common functionality, examples, and guidelines.

How to scroll to a flex item if EnsureVisible is not working

Postby Support Team » Fri Jul 16, 2010 1:51 pm

Hi,

If you have an item which returns you visible true, although it's not in the visible area for the user, please use following class to scroll to the item. The class scrolls while the item is not in the visible container by clicking the scrollbar buttons.
Just add the class to your project and call the method ScrollToElement().
For example in your Program.cs:
ScrollFlexUtil.ScrollToElement(VisibleContainer, YourItem, IncrementButton, DecrementButton);

Here is the code for the class
public class ScrollFlexUtil
{

	/// <summary>
	/// Scrolls while the item is not in the visible container.
	/// </summary>
	/// <param name="visibleContainer">The visible container of your scrolling area</param>
	/// <param name="flexItem">The item to scroll</param>
	/// <param name="down">Button for downscrolling (decrement button)</param>
	/// <param name="up">Button for upscrolling (increment button)</param>
	public static void ScrollToElement(Container visibleContainer, Element flexItem, Ranorex.Button down, Ranorex.Button up)
	{
            System.DateTime abortTime = System.DateTime.Now + timeout;

            bool direction = CheckDirection(visibleContainer, flexItem);
            Ranorex.Button scrollButton = direction ? down : up;

            while (!ElementVisible(visibleContainer, flexItem))
            {
                if (CheckDirection(visibleContainer, flexItem) != direction)
                    throw new RanorexException("Direction to element in ScrollToElement changed without element being ever visible.");
                if (System.DateTime.Now > abortTime)
                    throw new RanorexException("Timeout reached in ScrollToElement.");
                scrollButton.Click();
            }

	}

	/// <summary>
	/// Calculates the central point of your flex item
	/// </summary>
	/// <param name="rect">Rectangle of the flex item</param>		
	private static Point CalculateCentralPoint(Rectangle rect)
	{
        return rect.Location + new Size(rect.Width / 2, rect.Height / 2);
	}

	/// <summary>
	/// Checks if the flex item is in the visible area for the user
	/// </summary>
	/// <param name="main">Container which is visible to the user</param>
	/// <param name="item">Your flex item to scroll</param>
	/// <returns>Returns true if element is in container</returns>
    private static bool ElementVisible(Container main, Element item)
	{
		return main.ScreenRectangle.Contains(CalculateCentralPoint(item.ScreenRectangle));
	}
	/// <summary>
	/// Returns the direction for scrolling to the Element
	/// </summary>
	/// <param name="main">Container which is visible to the user</param>
	/// <param name="item">Your flex item to scroll</param>
	/// <returns>True for downscrolling, False for upscrolling</returns>
	private static bool CheckDirection(Ranorex.Container main, Ranorex.Core.Element item)
	{						
		return main.ScreenRectangle.Y < item.ScreenRectangle.Y;
	}

}


Regards,
Peter
Ranorex Support Team
User avatar
Support Team
Site Admin
 
Posts: 2006
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria

Re: How to scroll to a flex item if EnsureVisible is not working

Postby arun_volam » Mon Jul 19, 2010 5:40 pm

Hi,

i want to scroll the browsers horizontal scroll bar not a flex element. can u please tell me how to get the scrollbars and use them.And regarding increment and decrement buttons are they ranorex buttons or windows.
arun_volam
 
Posts: 16
Joined: Fri Jul 09, 2010 2:40 pm

Re: How to scroll to a flex item if EnsureVisible is not working

Postby Support Team » Tue Jul 20, 2010 11:23 am

Hi,

arun_volam wrote:i want to scroll the browsers horizontal scroll bar not a flex element. can u please tell me how to get the scrollbars and use them

Sorry but it is not possible at the moment to use the scrollbars of the browser. I'm searching for another solution that you can use with the browser scroll bars. A possible solution with the current method would be to add flex scrollbars to your application.
arun_volam wrote:And regarding increment and decrement buttons are they ranorex buttons or windows.

Sorry for the ambiguity, I've changed the name. Now it should be clear.

Regards,
Peter
Ranorex Support Team
User avatar
Support Team
Site Admin
 
Posts: 2006
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria

Re: How to scroll to a flex item if EnsureVisible is not working

Postby Support Team » Tue Jul 20, 2010 1:03 pm

Hi,

Please try following method to scroll to a specific point of your web document.
public static void ScrollToElement(Ranorex.WebDocument web, Element elem, int margin)
{
	Point offset = elem.ScreenRectangle.Location - (Size)web.ScreenRectangle.Location;
	string script = String.Format("window.scrollBy('{0}','{1}')", offset.X - margin, offset.Y - margin);
	web.ExecuteScript(script);
}

Regards,
Peter
Ranorex Support Team
User avatar
Support Team
Site Admin
 
Posts: 2006
Joined: Fri Jul 07, 2006 5:30 pm
Location: Graz, Austria


Return to How To …

Who is online

Users browsing this forum: No registered users and 0 guests

cron