-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREPL.cs
434 lines (378 loc) · 19.2 KB
/
REPL.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
using Newtonsoft.Json;
using ScriptCs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FluentAutomation.REPL
{
public partial class REPL : Form
{
private UserSettings settings = null;
private StatusViewModel viewModel = null;
private ScriptServices scriptServices = null;
private Repl repl = null;
private SettingsManager settingsManager = null;
private List<string> commands = new List<string>();
private int commandIndex = -1;
private string fluentGuid = string.Empty;
private bool hasOpenBrowser = false;
public REPL()
{
InitializeComponent();
this.settings = new UserSettings();
this.viewModel = new StatusViewModel();
this.browserSelection.Items.Clear();
var browserValues = Enum.GetValues(typeof(FluentAutomation.SeleniumWebDriver.Browser)).Cast<FluentAutomation.SeleniumWebDriver.Browser>().ToList();
browserValues.Sort((x, y) => x.ToString().CompareTo(y.ToString()));
foreach (var browserValue in browserValues)
{
this.browserSelection.Items.Add(browserValue.ToString());
}
this.InitRepl();
this.browserSelection.SetItemChecked(1, true);
}
private void REPL_Load(object sender, EventArgs e)
{
this.consoleTextBox.Text = this.CreateLine().ToString();
this.consoleTextBox.Select(this.consoleTextBox.Text.Length, 0);
}
private void REPL_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
if (this.viewModel.Url != null)
this.repl.Execute(this.fluentGuid + ".Dispose();");
}
private void InitRepl()
{
var servicesBuilder = new ReplScriptServicesBuilder(new ScriptConsole(), new Common.Logging.Simple.NoOpLogger()).Repl(true);
servicesBuilder.ScriptName("F14N.REPL");
this.scriptServices = servicesBuilder.Build();
this.scriptServices.Executor.Initialize(new string[] { }, this.scriptServices.ScriptPackResolver.GetPacks());
this.scriptServices.Executor.AddReferenceAndImportNamespaces(new[] { typeof(FluentAutomation.FluentTest), typeof(FluentAutomation.SeleniumWebDriver) });
this.repl = this.scriptServices.Executor as Repl;
this.settingsManager = new SettingsManager((scriptString) =>
{
return this.repl.Execute(scriptString);
});
}
private void SetReplBrowser()
{
if (this.browserSelection.CheckedItems.Count == 1)
{
var browserValue = this.browserSelection.CheckedItems[0].ToString();
this.SetReplBrowser(browserValue);
}
}
private void SetReplBrowser(string browser)
{
this.SetReplBrowser((FluentAutomation.SeleniumWebDriver.Browser)Enum.Parse(typeof(FluentAutomation.SeleniumWebDriver.Browser), browser));
}
private void SetReplBrowser(FluentAutomation.SeleniumWebDriver.Browser browser)
{
this.viewModel.Browser = browser;
this.fluentGuid = "__a" + Guid.NewGuid().ToString().Replace("-", "");
var result = this.repl.Execute("FluentAutomation.SeleniumWebDriver.Bootstrap(FluentAutomation.SeleniumWebDriver.Browser." + browser.ToString() + "); var " + this.fluentGuid + " = new FluentAutomation.FluentTest(); var I = " + this.fluentGuid + ".I; var Container = " + this.fluentGuid + ".Session.Container;");
if (result.CompileExceptionInfo != null)
this.consoleTextBox.Text += "\n" + result.CompileExceptionInfo.SourceException.ToString() + "\n";
else if (result.ExecuteExceptionInfo != null)
consoleTextBox.Text += "\n" + result.ExecuteExceptionInfo.SourceException.ToString() + "\n";
}
private void UpdateCommand(RichTextBox textBox, string command)
{
var lines = textBox.Lines;
if (lines.Length > 0)
{
lines[lines.Length - 1] = this.CreateLine().ToString() + command;
textBox.Lines = lines;
textBox.Select(textBox.Text.Length, 0);
}
}
private void consoleTextBox_KeyDown(object sender, KeyEventArgs e)
{
var textBox = (RichTextBox)sender;
var cursorCharacterIndex = textBox.SelectionStart;
if (e.Control && e.KeyCode == Keys.V)
{
textBox.Text += Clipboard.GetText(TextDataFormat.Text);
textBox.Select(textBox.Text.Length, 0);
e.SuppressKeyPress = true;
return;
}
else if (e.Control && e.KeyCode == Keys.C)
{
return;
}
else if (e.Control)
{
return;
}
else if (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown)
{
e.SuppressKeyPress = true;
return;
}
if (textBox.GetLineFromCharIndex(cursorCharacterIndex) != textBox.GetLineFromCharIndex(textBox.Text.Length - 1))
{
textBox.Select(textBox.Text.Length, 0);
return;
}
var preceedingCharacters = "";
if (cursorCharacterIndex >= 2)
preceedingCharacters = textBox.Text.Substring(cursorCharacterIndex - 2, 2);
if (e.KeyCode == Keys.Back)
{
if (preceedingCharacters == this.settings.StatusLineSeparator && textBox.SelectionLength == 0)
e.SuppressKeyPress = true;
}
else if (e.KeyCode == Keys.Up)
{
e.SuppressKeyPress = true;
if (this.commandIndex >= 0 && this.commandIndex < this.commands.Count)
{
this.UpdateCommand(textBox, this.commands[this.commandIndex]);
if (this.commandIndex > 0) this.commandIndex--;
}
}
else if (e.KeyCode == Keys.Down)
{
e.SuppressKeyPress = true;
if (this.commandIndex >= 0 && this.commandIndex < this.commands.Count)
{
if (this.commandIndex < this.commands.Count - 1)
{
this.commandIndex++;
this.UpdateCommand(textBox, this.commands[this.commandIndex]);
}
else
{
this.UpdateCommand(textBox, "");
}
}
}
else if (e.KeyCode == Keys.Home)
{
e.SuppressKeyPress = true;
var sepPosition = textBox.Text.LastIndexOf(this.settings.StatusLineSeparator);
if (e.Modifiers == Keys.Shift)
textBox.Select(sepPosition + this.settings.StatusLineSeparator.Length, textBox.SelectionStart);
else
textBox.Select(sepPosition + this.settings.StatusLineSeparator.Length, 0);
}
else if (e.KeyCode == Keys.End)
{
e.SuppressKeyPress = true;
if (e.Modifiers == Keys.Shift)
textBox.Select(textBox.SelectionStart, textBox.Text.Length - textBox.SelectionStart);
else
textBox.Select(textBox.Text.Length, 0);
}
else if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
textBox.Select(textBox.Text.Length, 0);
var commandLine = textBox.Lines.Last();
var sepPosition = textBox.Text.LastIndexOf(this.settings.StatusLineSeparator);
var command = textBox.Text.Substring(sepPosition + this.settings.StatusLineSeparator.Length, textBox.Text.Length - sepPosition - this.settings.StatusLineSeparator.Length);
this.commands.Add(command);
// We wait until the last second to init the REPL/browser
var skipExecute = false;
if (command.IndexOf("I.Open") != -1)
{
this.viewModel.Url = null; // invalidate viewModel URL so we can re-request it after loading
if (this.viewModel.Browser == null)
this.SetReplBrowser();
this.hasOpenBrowser = true;
}
else if ((command.IndexOf("I.") != -1 || command.IndexOf("I") == 0) && this.viewModel.Browser == null)
{
textBox.Text += "\nCall I.Open with a URI before issuing browser commands.\n";
skipExecute = true;
}
else if (command.IndexOf("I.Press") != -1)
{
textBox.Text += "\nI.Press commands are disabled in the REPL.\n";
skipExecute = true;
}
else if (command.IndexOf("FluentAutomation.Settings") != -1)
{
textBox.Text += "\nChange settings from the settings panel, not via commands.\n";
skipExecute = true;
}
else if (command == "cls" || command == "clear")
{
textBox.Text = this.CreateLine().ToString();
textBox.Select(textBox.Text.Length, 0);
return;
}
else if (command == "exit" || command == "close")
{
Application.Exit();
return;
}
else if (command == "help" || command == "?")
{
var helpText =
@"" + Environment.NewLine +
" ----------------------------------------------------------------------------- " + Environment.NewLine +
" FluentAutomation REPL Help " + Environment.NewLine +
" ----------------------------------------------------- | --------------------- " + Environment.NewLine +
" Actions: | Other Commands: " + Environment.NewLine +
" ----------------------------------------------------- | --------------------- " + Environment.NewLine +
" I.Open(url); | cls / clear " + Environment.NewLine +
" I.Click(selector); I.Click(x, y); | exit " + Environment.NewLine +
" I.DoubleClick(selector) / I.DoubleClick(x, y) | help " + Environment.NewLine +
" I.RightClick(selector) / I.RightClick(x, y) | " + Environment.NewLine +
" I.Hover(selector) / I.Hover(x, y) | " + Environment.NewLine +
" I.Focus(selector) | " + Environment.NewLine +
" I.Select(value).From(selector) | " + Environment.NewLine +
" I.Drag(selector).To(selector) / I.Drag(x, y).To(x, y) | " + Environment.NewLine +
" I.Wait(seconds) / I.Wait(TimeSpan) | " + Environment.NewLine +
" I.WaitUntil(() => expressionSucceeds) | " + Environment.NewLine +
" I.Find(selector) | " + Environment.NewLine +
" I.Type [disabled] | " + Environment.NewLine +
" I.Press [disabled] | " + Environment.NewLine +
" ----------------------------------------------------- | " + Environment.NewLine +
" Expects: | " + Environment.NewLine +
" ----------------------------------------------------- | " + Environment.NewLine +
" I.Expect.Text(text).In(selector) | " + Environment.NewLine +
" I.Expect.Value(value).In(selector) | " + Environment.NewLine +
" I.Expect.Exists(selector) | " + Environment.NewLine +
" I.Expect.Count(value).Of(selector) | " + Environment.NewLine +
" I.Expect.Class(value).On(selector) | " + Environment.NewLine +
" I.Expect.Url(url) / I.Expect.Url((url) => expression) | " + Environment.NewLine +
" I.Expect.True(() => expression) | " + Environment.NewLine +
" I.Expect.False(() => expression) | " + Environment.NewLine +
" I.Expect.Throws(() => expression) | " + Environment.NewLine +
" ----------------------------------------------------- | --------------------- " + Environment.NewLine + Environment.NewLine;
textBox.Text += helpText;
textBox.Select(textBox.Text.Length, 0);
this.UpdateCommand(textBox, "");
return;
}
if (!skipExecute)
{
var scriptResult = this.repl.Execute(command);
if (scriptResult.CompileExceptionInfo != null)
textBox.Text += "\n" + scriptResult.CompileExceptionInfo.SourceException.ToString() + "\n";
else if (scriptResult.ExecuteExceptionInfo != null)
textBox.Text += "\n" + scriptResult.ExecuteExceptionInfo.SourceException.ToString() + "\n";
else if (this.viewModel.Url == null)
if (this.hasOpenBrowser)
{
this.viewModel.Url = this.GetPageLocation();
// Edge case protection when changing browsers, first nav fails sometimes. Retry once.
if (this.viewModel.Url.DnsSafeHost == "")
{
this.repl.Execute(command);
this.viewModel.Url = this.GetPageLocation();
}
}
if (scriptResult.ReturnValue != null)
textBox.Text += "\n" + JsonConvert.SerializeObject(scriptResult.ReturnValue) + "\n";
}
textBox.Text += "\n" + this.CreateLine().ToString();
textBox.Select(textBox.Text.Length, 0);
this.Activate();
this.commandIndex = this.commands.Count - 1;
}
else
{
this.commandIndex = this.commands.Count - 1;
}
}
private Uri GetPageLocation()
{
var scriptResult = this.repl.Execute("Container.Resolve<FluentAutomation.Interfaces.ICommandProvider>().Url");
if (scriptResult.CompileExceptionInfo != null || scriptResult.ExecuteExceptionInfo != null)
return null;
return new Uri(scriptResult.ReturnValue.ToString());
}
private StatusLine CreateLine()
{
return new StatusLine(this.settings, this.viewModel);
}
private void openSettings_Click(object sender, EventArgs e)
{
if (this.settingsPanel.Visible)
this.settingsPanel.Hide();
else
this.settingsPanel.Show();
}
private void browserSelection_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
foreach (int index in browserSelection.CheckedIndices)
{
browserSelection.SetItemChecked(index, false);
}
if (this.viewModel.Url != null)
this.hasOpenBrowser = false;
var uiThreadContext = TaskScheduler.FromCurrentSynchronizationContext();
Task.Run(() =>
{
if (this.viewModel.Url != null)
this.repl.Execute(this.fluentGuid + ".Dispose();");
this.InitRepl();
this.SetReplBrowser(browserSelection.Items[e.Index].ToString());
Task.Factory.StartNew(() =>
{
this.UpdateCommand(this.consoleTextBox, "");
}, System.Threading.CancellationToken.None, TaskCreationOptions.None, uiThreadContext);
});
}
}
private void minimizeOnStart_CheckedChanged(object sender, EventArgs e)
{
this.settingsManager.MinimizeAllWindowsOnTestStart = minimizeOnStart.Checked;
}
private void windowWidth_TextChanged(object sender, EventArgs e)
{
var windowWidthText = this.windowWidth.Text;
if (windowWidthText == "")
this.settingsManager.WindowWidth = null;
else
this.settingsManager.WindowWidth = int.Parse(windowWidthText);
}
private void windowHeight_TextChanged(object sender, EventArgs e)
{
var windowHeightText = this.windowHeight.Text;
if (windowHeightText == "")
this.settingsManager.WindowHeight = null;
else
this.settingsManager.WindowHeight = int.Parse(windowHeightText);
}
private void defaultWait_TextChanged(object sender, EventArgs e)
{
this.settingsManager.DefaultWaitTimeout = TimeSpan.FromMilliseconds(double.Parse(this.defaultWait.Text));
}
private void defaultWaitUntil_TextChanged(object sender, EventArgs e)
{
this.settingsManager.DefaultWaitUntilTimeout = TimeSpan.FromMilliseconds(double.Parse(this.defaultWaitUntil.Text));
}
private void timeBetweenAttempts_TextChanged(object sender, EventArgs e)
{
this.settingsManager.DefaultWaitUntilThreadSleep = TimeSpan.FromMilliseconds(double.Parse(this.timeBetweenAttempts.Text));
}
private void waitOnAllCommands_CheckedChanged(object sender, EventArgs e)
{
this.settingsManager.WaitOnAllCommands = waitOnAllCommands.Checked;
}
private void waitOnAllExpects_CheckedChanged(object sender, EventArgs e)
{
this.settingsManager.WaitOnAllExpects = waitOnAllExpects.Checked;
}
private void windowWidth_Validating(object sender, CancelEventArgs e)
{
if (!Regex.Match(this.windowWidth.Text, @"^\d+$").Success)
e.Cancel = true;
}
}
}