Page 1 of 1

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

Posted: Fri Jul 16, 2010 12:51 pm
by Support Team
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:
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

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

Posted: Mon Jul 19, 2010 4:40 pm
by arun_volam
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.

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

Posted: Tue Jul 20, 2010 10:23 am
by Support Team
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

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

Posted: Tue Jul 20, 2010 12:03 pm
by Support Team
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

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

Posted: Tue Feb 26, 2013 2:00 pm
by bvandewe
I normally do not dig up old threads, but here goes...

I have trouble getting this code to work. I checked in the user guide how to set up Code Modules to get the class working, but I still can't use it in my test scripts. It's specified that you can drag the code module to the test suite to get it included, but I'm unable to do so with this code.

Besides that, the code seems to be "limited" in a way that restricts its use for me. Let me explain.

The project I'm working on is a rather expansive Flex client with several different "screens" and sub-screens. Due to lack of screen estate, several of the "screens" contain scroll bars and elements that are outside the screen (meaning Ranorex can find them and sees them as visible, but can't reach them as they are positioned outside the screen's boundaries). Often I have several elements on one screen that are outside the boundary, so I would have to call on this scrolling extensively.

I figured out that I probably would have to do so with parameters, but wonder if there is no easier way to achieve this by having actions happen directly on repository items in the main record modules. For instance with the Invoke Action option?

And yeah... I'm new to C# and Ranorex, which probably explains some of my inabilities here.

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

Posted: Fri Mar 01, 2013 1:32 pm
by Support Team
Hi,

In case of elements which are outside of the visible area you could try to use EnsureVisible.
You can either set the "Use Ensure Visible" property of the specific repository item to true or you could add a "Invoke Action" and select "EnsureVisible" before you perform your "normal" action.
In order to add the Invoke Action just drag the specific repository item to your action table and select "Invoke Action" -> "Ensure Visible".
Did you consider that the methods are static methods?
You have to call the methods from your Recordings or UserCodeModules but you cannot add the self created class to the test suite as it doesn't inherit from the "ITestModule" interface.

Regards,
Markus

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

Posted: Wed Jan 15, 2014 12:23 pm
by tman786
Hi Support,

I'm trying this code as i am having the same problem with FlexObjects.

After placing in a class, Visual Studio is reporting issues with the type of 'Point', 'Rectangle' and 'Size' types.

I have the using System.drawing namespace as you have advised.

Please assist.

Thanks

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

Posted: Wed Jan 29, 2014 12:55 pm
by tman786
code for scrolling to object did not work for me however the following setting change did:

Configuration.Current.Adapter.DefaultUseEnsureVisible = false;

Regards,

Tman786