How to scroll to a flex item if EnsureVisible is not working
Posted: Fri Jul 16, 2010 12: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:
Peter
Ranorex Support Team
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:
RxScrollFlexUtil.ScrollToElement(VisibleContainer, YourItem, IncrementButton, DecrementButton);Here is the code for the class
using System; using System.Drawing; using Ranorex; using Ranorex.Core; namespace Ranorex.Testing { public class RxScrollFlexUtil { static readonly TimeSpan timeout = new TimeSpan(0, 1, 0); /// <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