-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
Session.cs
134 lines (121 loc) · 4.92 KB
/
Session.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
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using FlaUI.UIA3;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FlaUI.WebDriver
{
public class Session : IDisposable
{
public Session(Application? app)
{
App = app;
SessionId = Guid.NewGuid().ToString();
Automation = new UIA3Automation();
InputState = new InputState();
TimeoutsConfiguration = new TimeoutsConfiguration();
if (app != null)
{
// We have to capture the initial window handle to be able to keep it stable
CurrentWindowWithHandle = GetOrAddKnownWindow(app.GetMainWindow(Automation, PageLoadTimeout));
}
}
public string SessionId { get; }
public UIA3Automation Automation { get; }
public Application? App { get; }
public InputState InputState { get; }
private Dictionary<string, KnownElement> KnownElementsByElementReference { get; } = new Dictionary<string, KnownElement>();
private Dictionary<string, KnownWindow> KnownWindowsByWindowHandle { get; } = new Dictionary<string, KnownWindow>();
public TimeSpan ImplicitWaitTimeout => TimeSpan.FromMilliseconds(TimeoutsConfiguration.ImplicitWaitTimeoutMs);
public TimeSpan PageLoadTimeout => TimeSpan.FromMilliseconds(TimeoutsConfiguration.PageLoadTimeoutMs);
public TimeSpan? ScriptTimeout => TimeoutsConfiguration.ScriptTimeoutMs.HasValue ? TimeSpan.FromMilliseconds(TimeoutsConfiguration.ScriptTimeoutMs.Value) : null;
public TimeoutsConfiguration TimeoutsConfiguration { get; set; }
private KnownWindow? CurrentWindowWithHandle { get; set; }
public Window CurrentWindow
{
get
{
if (App == null || CurrentWindowWithHandle == null)
{
throw WebDriverResponseException.UnsupportedOperation("This operation is not supported for Root app");
}
if (CurrentWindowWithHandle.Window.IsMainWindow)
{
// When expanding menus, calling `GetMainWindow` again is necessary to be able to find the expanded menu items
// This seems to be a bug (it isn't solved by using `CacheRequest.ForceNoCache()`)
return App.GetMainWindow(Automation);
}
return CurrentWindowWithHandle.Window;
}
set
{
CurrentWindowWithHandle = GetOrAddKnownWindow(value);
}
}
public string CurrentWindowHandle
{
get
{
if (App == null || CurrentWindowWithHandle == null)
{
throw WebDriverResponseException.UnsupportedOperation("This operation is not supported for Root app");
}
return CurrentWindowWithHandle.WindowHandle;
}
}
public KnownElement GetOrAddKnownElement(AutomationElement element)
{
var result = KnownElementsByElementReference.Values.FirstOrDefault(knownElement => knownElement.Element.Equals(element));
if (result == null)
{
result = new KnownElement(element);
KnownElementsByElementReference.Add(result.ElementReference, result);
}
return result;
}
public AutomationElement? FindKnownElementById(string elementId)
{
if (!KnownElementsByElementReference.TryGetValue(elementId, out var knownElement))
{
return null;
}
return knownElement.Element;
}
public KnownWindow GetOrAddKnownWindow(Window window)
{
var result = KnownWindowsByWindowHandle.Values.FirstOrDefault(knownElement => knownElement.Window.Equals(window));
if (result == null)
{
result = new KnownWindow(window);
KnownWindowsByWindowHandle.Add(result.WindowHandle, result);
}
return result;
}
public Window? FindKnownWindowByWindowHandle(string windowHandle)
{
if (!KnownWindowsByWindowHandle.TryGetValue(windowHandle, out var knownWindow))
{
return null;
}
return knownWindow.Window;
}
public void RemoveKnownWindow(Window window)
{
var item = KnownWindowsByWindowHandle.Values.FirstOrDefault(knownElement => knownElement.Window.Equals(window));
if (item != null)
{
KnownWindowsByWindowHandle.Remove(item.WindowHandle);
}
}
public void Dispose()
{
if (App != null && !App.HasExited)
{
App.Close();
}
Automation.Dispose();
App?.Dispose();
}
}
}