Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions src/TestStack.White/Factory/TableRowFactory.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
using System;
using System.Collections.Generic;
using System.Windows.Automation;
using TestStack.White.AutomationElementSearch;
using TestStack.White.Configuration;
using TestStack.White.UIItems.Actions;
using TestStack.White.UIItems.TableItems;

namespace TestStack.White.Factory
{
public class TableRowFactory
{
private readonly AutomationElementFinder automationElementFinder;
private static readonly Predicate<AutomationElement> RowPredicate;
private static int result;
private readonly AutomationElementFinder automationElementFinder;

static TableRowFactory()
public TableRowFactory(AutomationElementFinder automationElementFinder)
{
RowPredicate =
element =>
element.Current.Name.Split(' ').Length == 2 &&
// row header containes no Numbers
(int.TryParse(element.Current.Name.Split(' ')[0], out result) ||
int.TryParse(element.Current.Name.Split(' ')[1], out result));
this.automationElementFinder = automationElementFinder;
}

public TableRowFactory(AutomationElementFinder automationElementFinder)
public virtual int NumberOfRows
{
this.automationElementFinder = automationElementFinder;
get { return GetRowElements().Count; }
}

public virtual TableRows CreateRows(IActionListener actionListener, TableHeader tableHeader)
{
List<AutomationElement> rowElements = GetRowElements();
var rowElements = GetRowElements();
return new TableRows(rowElements, actionListener, tableHeader, new TableCellFactory(automationElementFinder.AutomationElement, actionListener));
}

private List<AutomationElement> GetRowElements()
{
// this will find only first level children of out element - rows
List<AutomationElement> descendants = automationElementFinder.Children(AutomationSearchCondition.ByControlType(ControlType.Custom));
var automationElements = new List<AutomationElement>(descendants.FindAll(RowPredicate));
var descendants = automationElementFinder.Children(AutomationSearchCondition.ByControlType(ControlType.Custom));
var automationElements = new List<AutomationElement>(descendants.FindAll(IsRow));
return automationElements;
}

public virtual int NumberOfRows
private static bool IsRow(AutomationElement element)
{
get { return GetRowElements().Count; }
var parts = element.Current.Name.Split(' ');

if (parts.Length < 2)
{
return false;
}

int result;

for (var i = parts.Length - 1; i >= 0; i--)
{
if (int.TryParse(parts[i], out result))
{
return true;
}
}

return false;
}
}
}
}