ScrollPattern是用来操作控件的滚动条,目前支持ScrollPattern的控件有ListBox,listView,GridView,TreeView.
ScrollPattern主要方法
1. Scroll 水平和垂直滚动内容区域的可见区域滚动, Scroll有两个参数,其类型为ScrollAmount枚举类型。
2. ScrollHorizontal 按指定的 ScrollAmount 水平滚动内容区域的当前可见区域滚动。
3. ScrollVertical 按指定的 ScrollAmount 垂直滚动内容区域的当前可见区域滚动。
ScrollPattern属性
1. VerticallyScrollable 属性用于判定是否可以垂直滚动。
2. HorizontallyScrollable 属性用于判定是否可以水平滚动。
3. HorizontalScrollPercent 获取当前水平滚动条的位置。
4. VerticalScrollPercent 获取当前垂直滚动条的位置。
下面我们通过一个实例来演示自动化测试中如何使用ScrollPattern来测试GridView中滚动条的方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Text;
3
using System.Diagnostics;
4
using System.Threading;
5
using System.Windows.Automation;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace UIATest
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
class Program
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
static void Main(string[] args)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
Process process = Process.Start(@"F:\CSharpDotNet\AutomationTest\ATP\WpfApp\bin\Debug\WpfApp.exe");
14
int processId = process.Id;
15
Thread.Sleep(1000);
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ScrollPattern#region ScrollPattern
18
AutomationElement element = FindElementById(processId, "listview1");
19
AutomationElementCollection elementCollection=element.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.DataItem));
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
ScrollPattern scrollPattern = GetScrollPattern(element);
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
if (scrollPattern.Current.VerticallyScrollable)
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
while (elementCollection[22].Current.IsOffscreen)
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
scrollPattern.ScrollVertical(ScrollAmount.LargeIncrement);
28
}
29
}
30
#endregion
31
32
}
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
35
/// Get the automation elemention of current form.
36
/// </summary>
37
/// <param name="processId">Process Id</param>
38
/// <returns>Target element</returns>
39
public static AutomationElement FindWindowByProcessId(int processId)
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
41
AutomationElement targetWindow = null;
42
int count = 0;
43
try
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
Process p = Process.GetProcessById(processId);
46
targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
47
return targetWindow;
48
}
49
catch (Exception ex)
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
count++;
52
StringBuilder sb = new StringBuilder();
53
string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
54
if (count > 5)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
throw new InvalidProgramException(message, ex);
57
}
58
else
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
return FindWindowByProcessId(processId);
61
}
62
}
63
}
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
67
/// Get the automation element by automation Id.
68
/// </summary>
69
/// <param name="windowName">Window name</param>
70
/// <param name="automationId">Control automation Id</param>
71
/// <returns>Automatin element searched by automation Id</returns>
72
public static AutomationElement FindElementById(int processId, string automationId)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
74
AutomationElement aeForm = FindWindowByProcessId(processId);
75
AutomationElement tarFindElement = aeForm.FindFirst(TreeScope.Descendants,
76
new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
77
return tarFindElement;
78
}
79![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
80![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
GetScrollPattern helper#region GetScrollPattern helper
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
82
/// Get ScrollPattern
83
/// </summary>
84
/// <param name="element">AutomationElement instance</param>
85
/// <returns>ScrollPattern instance</returns>
86
public static ScrollPattern GetScrollPattern(AutomationElement element)
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
88
object currentPattern;
89
if (!element.TryGetCurrentPattern(ScrollPattern.Pattern, out currentPattern))
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
throw new Exception(string.Format("Element with AutomationId '{0}' and Name '{1}' does not support the ScrollPattern.",
92
element.Current.AutomationId, element.Current.Name));
93
}
94
return currentPattern as ScrollPattern;
95
}
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97
#endregion
98
}
99
}
100![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XAML源码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
<Window x:Class="WpfApp.GridView"
2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
Title="GridView" Height="280" Width="467">
5
<Grid>
6
<!--将鼠标放在方框的边缘点击就会产生相应的分割线生成Grid.RowDefinitions-->
7
<Grid.RowDefinitions>
8
<!--Auto,实际作用就是取实际控件所需的最小值;值为*或N*,实际作用就是取尽可能大的值;数字,绝对尺寸-->
9
<RowDefinition Height="*" />
10
<RowDefinition Height="auto" MinHeight="95" />
11
<RowDefinition Height="22" />
12
</Grid.RowDefinitions>
13
<ListView Name="listview1" MinWidth="280" Grid.RowSpan="2" MouseMove="listview1_MouseMove">
14
<ListView.View>
15
<GridView x:Name="gridView1">
16
<GridViewColumn Header="EmployeeID" DisplayMemberBinding="{Binding Path=EmployeeID}"></GridViewColumn>
17
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding Path=FirstName}"></GridViewColumn>
18
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding Path=LastName}"></GridViewColumn>
19
<GridViewColumn Header="Address" DisplayMemberBinding="{Binding Path=Address}"></GridViewColumn>
20
</GridView>
21
</ListView.View>
22
</ListView>
23
<!--Grid.Row="1"用来设置WrapPanel及Button应该在父容器的什么位置-->
24
</Grid>
25![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
</Window>
后台CS源码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Windows;
6
using System.Windows.Controls;
7
using System.Windows.Data;
8
using System.Windows.Documents;
9
using System.Windows.Input;
10
using System.Windows.Media;
11
using System.Windows.Media.Imaging;
12
using System.Windows.Shapes;
13
using System.Data.SqlClient;
14
using System.Data;
15![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
namespace WpfApp
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
19
/// Interaction logic for GridView.xaml
20
/// </summary>
21
public partial class GridView : Window
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
public GridView()
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
InitializeComponent();
26
getData();
27
}
28
SqlDataAdapter sda;
29
DataTable dt;
30
void getData()
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
//init sqlconnection
33
SqlConnectionStringBuilder connbuilder = new SqlConnectionStringBuilder();
34
connbuilder.DataSource = ".";//本地服务器
35
connbuilder.IntegratedSecurity = true;//Windows集成验证
36
connbuilder.InitialCatalog = "TestDB";//数据库为Northwind
37
SqlConnection conn = new SqlConnection(connbuilder.ConnectionString);
38
sda = new SqlDataAdapter("select EmployeeID,FirstName,LastName,Address from Employees ", conn);
39
SqlCommandBuilder commbuilder = new SqlCommandBuilder(sda);
40
//sda.UpdateCommand = commbuilder.GetUpdateCommand();
41
dt = new DataTable();
42
//sda.AcceptChangesDuringUpdate = true;
43
sda.Fill(dt);
44
listview1.ItemsSource = dt.DefaultView;
45
}
46
}
47
}
48![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
49![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
数据库及数据库表源码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
USE [master]
2
GO
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//****** Object: Database TestDB Script Date: 10/17/2009 16:08:09 ******/
4
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'TestDB')
5
DROP DATABASE [TestDB]
6
CREATE DATABASE TestDB ON PRIMARY
7
( NAME = N'TestDB', FILENAME = N'C:\TestDB.mdf' , SIZE = 2688KB , MAXSIZE = UNLIMITED, FILEGROWTH = 80KB )
8
LOG ON
9
( NAME = N'TestDB_log', FILENAME = N'C:\TestDB.ldf' , SIZE = 1024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
10
GO
11![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
use TestDB
13![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
CREATE TABLE [dbo].[Employees](
15
[EmployeeID] [int] IDENTITY(1,1) primary key NOT NULL,
16
[LastName] [varchar](20) NOT NULL,
17
[FirstName] [varchar](10) NOT NULL,
18
[Address] [varchar](60) NULL
19
)
20![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
GO
22
truncate table employees
23
declare @i int
24
set @i = 1
25
while @i<25
26
begin
27![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
insert into Employees
29
(
30
LastName,
31
FirstName,
32
[Address]
33
)
34
values
35
('Kaden'+cast(@i as varchar), 'Kang'+cast(@i as varchar), 'Nanjing, Jiangsu, China'+cast(@i as varchar))
36
set @i=@i+1
37
end
38![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
本文简单介绍了ScrollPattern以及使用ScrollPattern来操作垂直水平滚动条。