forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionSequence.cs
139 lines (121 loc) · 5.16 KB
/
ActionSequence.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// <copyright file="ActionSequence.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace OpenQA.Selenium.Interactions
{
/// <summary>
/// Represents a sequence of actions to be performed in the target browser.
/// </summary>
public class ActionSequence
{
private List<Interaction> interactions = new List<Interaction>();
/// <summary>
/// Initializes a new instance of the <see cref="ActionSequence"/> class.
/// </summary>
/// <param name="device">The input device that executes this sequence of actions.</param>
public ActionSequence(InputDevice device)
: this(device, 0)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ActionSequence"/> class.
/// </summary>
/// <param name="device">The input device that executes this sequence of actions.</param>
/// <param name="initialSize">the initial size of the sequence.</param>
public ActionSequence(InputDevice device, int initialSize)
{
if (device == null)
{
throw new ArgumentNullException(nameof(device), "Input device cannot be null.");
}
this.InputDevice = device;
for (int i = 0; i < initialSize; i++)
{
this.AddAction(new PauseInteraction(device, TimeSpan.Zero));
}
}
/// <summary>
/// Gets the count of actions in the sequence.
/// </summary>
public int Count
{
get { return this.interactions.Count; }
}
/// <summary>
/// Gets the input device for this Action sequence.
/// </summary>
[Obsolete("This property has been renamed to InputDevice and will be removed in a future version")]
[CLSCompliant(false)]
public InputDevice inputDevice => InputDevice;
/// <summary>
/// Gets the input device for this Action sequence.
/// </summary>
public InputDevice InputDevice { get; }
/// <summary>
/// Adds an action to the sequence.
/// </summary>
/// <param name="interactionToAdd">The action to add to the sequence.</param>
/// <returns>A self-reference to this sequence of actions.</returns>
public ActionSequence AddAction(Interaction interactionToAdd)
{
if (interactionToAdd == null)
{
throw new ArgumentNullException(nameof(interactionToAdd), "Interaction to add to sequence must not be null");
}
if (!interactionToAdd.IsValidFor(this.InputDevice.DeviceKind))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Interaction {0} is invalid for device type {1}.", interactionToAdd.GetType(), this.InputDevice.DeviceKind), nameof(interactionToAdd));
}
this.interactions.Add(interactionToAdd);
return this;
}
/// <summary>
/// Converts this action sequence into an object suitable for serializing across the wire.
/// </summary>
/// <returns>A <see cref="Dictionary{TKey, TValue}"/> containing the actions in this sequence.</returns>
public Dictionary<string, object> ToDictionary()
{
Dictionary<string, object> toReturn = this.InputDevice.ToDictionary();
List<object> encodedActions = new List<object>();
foreach (Interaction action in this.interactions)
{
encodedActions.Add(action.ToDictionary());
}
toReturn["actions"] = encodedActions;
return toReturn;
}
/// <summary>
/// Returns a string that represents the current <see cref="ActionSequence"/>.
/// </summary>
/// <returns>A string that represents the current <see cref="ActionSequence"/>.</returns>
public override string ToString()
{
StringBuilder builder = new StringBuilder("Action sequence - ").Append(this.InputDevice.ToString());
foreach (Interaction action in this.interactions)
{
builder.AppendLine();
builder.AppendFormat(" {0}", action.ToString());
}
return builder.ToString();
}
}
}