Unable to scroll ListView

Jan 27, 2011 at 11:15 AM
Edited Jan 27, 2011 at 11:17 AM

Hi,

I am automating the testing of a Silverlight application. In the application there are a number of ListViews for which I need to click on child rows. I can click on the rows fine if they are on screen, but if they are not on screen then calling the Click() on the ListViewRow instance does not work. I had a look through some of the discussions but none of the solutions suggested for a similar problem seem to work.

I figured a suitable solution would be to just scroll the ListView up or down until that ListViewRow was on screen. This is where my current problem lies. I have tried to scroll the ListView but I am having no luck. I access the scrollbars like so : listViewControl.ScrollBars, but the ScrollBars.CanScroll property is false even though the ScrollBars are present on the Silverlight page.

Is there some otherway i can scroll the ListView to get the desired ListViewRow on screen?



Thanks

Jan 31, 2011 at 1:37 PM
Edited Jan 31, 2011 at 1:38 PM

Anyone got any ideas?

I still haven't found a solution yet other than making sure that the rows I want to interact with contain data that sorts them to the top of the list.

Jan 31, 2011 at 2:09 PM

Hi

try the following:

 

get the row element from the listview and use the following code

ScrollAutomationElement(row.AutomationElement);

 

/// <summary>
/// Gets the specified pattern.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
private static AutomationPattern GetSpecifiedPattern( AutomationElement element, string value )
{
  AutomationPattern[] supportedPattern = element.GetSupportedPatterns();
  
  AutomationPattern automationValuePattern = null;
  foreach (AutomationPattern pattern in supportedPattern)
  {
	if (pattern.ProgrammaticName == value )
	{
	  automationValuePattern = pattern;
	  break;
	}
  }
  return automationValuePattern;
}

/// <summary>
/// Scroll the automation element 
/// </summary>
/// <param name="element">The element.</param>
public static void ScrollAutomationElement(AutomationElement element)
{
  AutomationPattern automationPatternFromElement = GetSpecifiedPattern(element, "ScrollItemPatternIdentifiers.Pattern");

  if (automationPatternFromElement != null)
  {
	ScrollItemPattern scrollPattern = (ScrollItemPattern)element.GetCurrentPattern(automationPatternFromElement);
	if (scrollPattern != null)
	{
	  scrollPattern.ScrollIntoView();
	}
  }
}

If this don't help:

it might be that there is  a kind of lazy loading, only the elements in view will be loaded (have a look in UIAVerify to check against this, if the expected row count differ to the row count in UIAVerify)

I use something like that

_listView is here a global object in my tests it is already set

/// <summary>
    /// Finds all rows.
    /// </summary>
    /// <returns></returns>
    public List<ListViewRow> FindAllRows()
    {
      List<ListViewRow> rows = new List<ListViewRow>();
      WalkOverRows( rows );

      return rows;
    }

    /// <summary>
    /// Walks the over rows.
    /// </summary>
    /// <param name="rows">The rows.</param>
    private void WalkOverRows( List<ListViewRow> rows )
    {
      bool added = false;
      foreach( ListViewRow row in _listView.Rows )
      {
        if (!rows.Contains(row))
        {
          rows.Add( row );
          added = true;
        }
      }

      if (added)
      {
        AutomationElementHelper.ScrollAutomationElement(_listView.Rows.Last().AutomationElement);
        Thread.Sleep(300);
        WalkOverRows( rows );
      }
    }

After that all rows can be find.

Throndorin

 

 

Jan 31, 2011 at 2:17 PM

Thanks Throndorin,

I'll give the above a try and let you know if that solves my problem!

Aug 4, 2011 at 3:45 AM
Edited Aug 4, 2011 at 3:50 AM

Unfortunately, trying to use ScrollAutomationElement did not work for me why trying to scroll to a Label within a silverlight DataGrid\ListView.

The Label does not return any supported automation patterns.

I found a related work item here:

"Scroll horizontally when items are not visible"

http://white.codeplex.com/workitem/7025