-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathEntitySteps.cs
More file actions
608 lines (552 loc) · 24.6 KB
/
EntitySteps.cs
File metadata and controls
608 lines (552 loc) · 24.6 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
namespace Capgemini.PowerApps.SpecFlowBindings.Steps
{
using System;
using System.Globalization;
using System.Linq;
using Capgemini.PowerApps.SpecFlowBindings.Extensions;
using FluentAssertions;
using Microsoft.Dynamics365.UIAutomation.Api.UCI;
using Microsoft.Dynamics365.UIAutomation.Api.UCI.DTO;
using Microsoft.Dynamics365.UIAutomation.Browser;
using OpenQA.Selenium;
using TechTalk.SpecFlow;
/// <summary>
/// Step bindings related to forms.
/// </summary>
[Binding]
public class EntitySteps : PowerAppsStepDefiner
{
/// <summary>
/// Selects a tab on the form.
/// </summary>
/// <param name="tabName">The name of the tab.</param>
[When(@"I select the '(.*)' tab")]
public static void ISelectTab(string tabName)
{
Driver.WaitUntilVisible(By.CssSelector($"li[title=\"{tabName}\"]"));
XrmApp.Entity.SelectTab(tabName);
}
/// <summary>
/// Asserts whether a tab is currently visible.
/// </summary>
/// <param name="canOrCannot">Whether the tab should be visible.</param>
/// <param name="tabName">The name of the tab.</param>
[Then(@"I (can|cannot) see the '(.*)' tab")]
public static void ICanSeeTab(string canOrCannot, string tabName)
{
canOrCannot = canOrCannot ?? throw new ArgumentNullException(nameof(canOrCannot));
tabName = tabName ?? throw new ArgumentNullException(nameof(tabName));
bool shouldBeVisible = canOrCannot.Equals("can", StringComparison.InvariantCultureIgnoreCase);
Driver.IsVisible(By.CssSelector($"li[title=\"{tabName}\"]"))
.Should()
.Be(
shouldBeVisible,
because: $"The tab '{tabName}' {(shouldBeVisible ? "should" : "should not")} be visible.");
}
/// <summary>
/// Sets the value for the field.
/// </summary>
/// <param name="fieldValue">The field value.</param>
/// <param name="fieldName">The field name.</param>
/// <param name="fieldType">The field type.</param>
/// <param name="fieldLocation">Whether the field is in the header.</param>
[When(@"I enter '(.*)' into the '(.*)' (text|optionset|multioptionset|boolean|numeric|currency|datetime|lookup) (field|header field) on the form")]
public static void WhenIEnterInTheField(string fieldValue, string fieldName, string fieldType, string fieldLocation)
{
if (fieldLocation == "field")
{
SetFieldValue(fieldName, fieldValue.ReplaceTemplatedText(), fieldType);
}
else
{
SetHeaderFieldValue(fieldName, fieldValue.ReplaceTemplatedText(), fieldType);
}
// Click to lose focus - So that business rules and other form events can occur
Driver.FindElement(By.XPath("html")).Click();
Driver.WaitForTransaction();
}
/// <summary>
/// Sets the values of the fields in the table on the form.
/// </summary>
/// <param name="fields">The fields to set.</param>
[When(@"I enter the following into the form")]
public static void WhenIEnterTheFollowingIntoTheForm(Table fields)
{
fields = fields ?? throw new ArgumentNullException(nameof(fields));
foreach (TableRow row in fields.Rows)
{
WhenIEnterInTheField(row["Value"], row["Field"], row["Type"], row["Location"]);
}
}
/// <summary>
/// Clears the value for the field.
/// </summary>
/// <param name="field">The field name.</param>
[When(@"I clear the '(.*)' (?:currency|numeric|text|boolean) field")]
public static void WhenIClearTheField(string field)
{
XrmApp.Entity.ClearValue(field);
}
/// <summary>
/// Clears the value for the option set field.
/// </summary>
/// <param name="field">The field.</param>
[When(@"I clear the '(.*)' optionset field")]
public static void WhenIClearTheOptionSetField(OptionSet field)
{
XrmApp.Entity.ClearValue(field);
}
/// <summary>
/// Clears the value for the multi-select option set field.
/// </summary>
/// <param name="field">The field.</param>
[When(@"I clear the '(.*)' multioptionset field")]
public static void WhenIClearTheOptionSetField(MultiValueOptionSet field)
{
XrmApp.Entity.ClearValue(field);
}
/// <summary>
/// Clears the value for the boolean field.
/// </summary>
/// <param name="field">The field.</param>
[When(@"I clear the '(.*)' datetime field")]
public static void WhenIClearTheDateTimeField(DateTimeControl field)
{
XrmApp.Entity.ClearValue(field);
}
/// <summary>
/// Clears the value for the lookup field.
/// </summary>
/// <param name="field">The field.</param>
[When(@"I clear the '(.*)' lookup field")]
public static void WhenIClearTheLookupField(LookupItem field)
{
XrmApp.Entity.ClearValue(field);
}
/// <summary>
/// Deletes the record.
/// </summary>
[When(@"I delete the record")]
public static void WhenIDeleteTheRecord()
{
XrmApp.Entity.Delete();
}
/// <summary>
/// Opens the record set navigator.
/// </summary>
[When(@"I open the record set navigator")]
public static void WhenIOpenTheRecordSetNavigator()
{
XrmApp.Entity.OpenRecordSetNavigator();
}
/// <summary>
/// Closes the record set navigator.
/// </summary>
[When(@"I close the record set navigator")]
public static void WhenICloseTheRecordSetNavigator()
{
XrmApp.Entity.CloseRecordSetNavigator();
}
/// <summary>
/// Select a Form.
/// </summary>
/// <param name="formName">The name of the form.</param>
[When(@"I select '(.*)' form")]
public static void WhenISelectForm(string formName)
{
XrmApp.Entity.SelectForm(formName);
}
/// <summary>
/// Select a lookup on the form.
/// </summary>
/// <param name="fieldName">The name of the lookup.</param>
[When(@"I select '(.*)' lookup")]
public static void WhenISelectLookup(string fieldName)
{
XrmApp.Entity.SelectLookup(new LookupItem { Name = fieldName });
}
/// <summary>
/// Saves the record.
/// </summary>
[When(@"I save the record")]
public static void WhenISaveTheRecord()
{
try
{
XrmApp.Entity.Save();
}
catch (InvalidOperationException)
{
// Ignore business process errors on save to allow assertions against these if required.
}
}
/// <summary>
/// Assigns the record to a user or team.
/// </summary>
/// <param name="userOrTeamName">The name of the user or team.</param>
[When(@"I assign the record to a (?:user|team) named '(.*)'")]
public static void WhenIAssignTheRecordToANamed(string userOrTeamName)
{
XrmApp.Entity.Assign(userOrTeamName);
}
/// <summary>
/// Switches business process flow.
/// </summary>
/// <param name="process">The name of the process.</param>
[When(@"I switch process to the '(.*)' process")]
public static void WhenISwitchProcessToTheProcess(string process)
{
XrmApp.Entity.SwitchProcess(process);
}
/// <summary>
/// Asserts that a value is shown in a text, currency or numeric field.
/// </summary>
/// <param name="expectedValue">The expected value.</param>
/// <param name="field">The field name.</param>
/// <param name="fieldLocation">Where the field is located.</param>
[Then("I can see a value of '(.*)' in the '(.*)' (?:currency|numeric|text) (field|header field)")]
public static void ThenICanSeeAValueOfInTheField(string expectedValue, string field, string fieldLocation)
{
string actualValue = fieldLocation == "field" ? XrmApp.Entity.GetValue(field) : XrmApp.Entity.GetHeaderValue(field);
actualValue.Should().Be(expectedValue);
}
/// <summary>
/// Asserts that a value is shown in an option set field.
/// </summary>
/// <param name="expectedValue">The expected value.</param>
/// <param name="field">The field name.</param>
/// <param name="fieldLocation">Where the field is located.</param>
[Then("I can see a value of '(.*)' in the '(.*)' optionset (field|header field)")]
public static void ThenICanSeeAValueOfInTheOptionSetField(string expectedValue, OptionSet field, string fieldLocation)
{
string actualValue = fieldLocation == "field" ? XrmApp.Entity.GetValue(field) : XrmApp.Entity.GetHeaderValue(field);
actualValue.Should().Be(expectedValue);
}
/// <summary>
/// Asserts that a value is shown in a multi-select option set field.
/// </summary>
/// <param name="expectedValue">The expected value.</param>
/// <param name="field">The field name.</param>
[Then("I can see a value of '(.*)' in the '(.*)' multioptionset field")]
public static void ThenICanSeeAValueOfInTheOptionSetField(string[] expectedValue, MultiValueOptionSet field)
{
XrmApp.Entity.GetValue(field).Values.Should().Equal(expectedValue);
}
/// <summary>
/// Asserts that a value is shown in a lookup field.
/// </summary>
/// <param name="expectedValue">The expected value.</param>
/// <param name="field">The field name.</param>
/// <param name="fieldLocation">Where the field is located.</param>
[Then("I can see a value of '(.*)' in the '(.*)' lookup (field|header field)")]
public static void ThenICanSeeAValueOfInTheLookupField(string expectedValue, LookupItem field, string fieldLocation)
{
string actualValue = fieldLocation == "field" ? XrmApp.Entity.GetValue(field) : XrmApp.Entity.GetHeaderValue(field);
actualValue.Should().Be(expectedValue);
}
/// <summary>
/// Asserts that a field is mandatory or optional.
/// </summary>
/// <param name="fieldName">The name of the field.</param>
/// <param name="requirementLevel">Whether the field should be mandatory or optional.</param>
[Then(@"the '(.*)' field is (mandatory|optional)")]
public static void ThenTheFieldIsMandatory(string fieldName, string requirementLevel)
{
if (!Driver.TryFindElement(By.CssSelector($"div[data-id=\"{fieldName}-FieldSectionItemContainer\"]"), out var fieldContainer))
{
throw new InvalidOperationException($"The {fieldName} field is not visible on the form.");
}
fieldContainer.GetAttribute<int>("data-fieldrequirement").Should().Be(requirementLevel == "mandatory" ? 2 : 0, because: $"the field should be {requirementLevel}");
}
/// <summary>
/// Asserts that a set of fields are mandatory or optional.
/// </summary>
/// <param name="requirementLevel">Whether the field should be mandatory or optional.</param>
/// <param name="table">The names of the fields.</param>
[Then("the following fields are (mandatory|optional)")]
public static void ThenTheFieldsAreMandatory(string requirementLevel, Table table)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table));
}
var fields = table.Rows.Select(x => x.Values.First());
foreach (var field in fields)
{
ThenTheFieldIsMandatory(field, requirementLevel);
}
}
/// <summary>
/// Asserts that a value is shown in a boolean field.
/// </summary>
/// <param name="expectedValue">The expected value.</param>
/// <param name="field">The field name.</param>
/// <param name="fieldLocation">Where the field is located.</param>
[Then("I can see a value of '(true|false)' in the '(.*)' boolean (field|header field)")]
public static void ThenICanSeeAValueOfInTheBooleanField(bool expectedValue, BooleanItem field, string fieldLocation)
{
var actualValue = fieldLocation == "field" ? XrmApp.Entity.GetValue(field) : XrmApp.Entity.GetHeaderValue(field);
actualValue.Should().Be(expectedValue);
}
/// <summary>
/// Asserts that a value is shown in a datetime field.
/// </summary>
/// <param name="expectedValue">The expected value.</param>
/// <param name="field">The field name.</param>
/// <param name="fieldLocation">Where the field is located.</param>
[Then(@"I can see a value of '(.*)' in the '(.*)' datetime (field|header field)")]
public static void ThenICanSeeAValueOfInTheDateTimeField(DateTime? expectedValue, DateTimeControl field, string fieldLocation)
{
var actualValue = fieldLocation == "field" ? XrmApp.Entity.GetValue(field) : XrmApp.Entity.GetHeaderValue(field);
actualValue.Should().Be(expectedValue);
}
/// <summary>
/// Asserts that a business process error is shown with a given message.
/// </summary>
/// <param name="expectedMessage">The expected message.</param>
[Then(@"I can see a business process error stating '(.*)'")]
public static void ThenICanSeeABusinessProcessErrorStating(string expectedMessage)
{
XrmApp.Entity.GetBusinessProcessError().Should().Be(expectedMessage);
}
/// <summary>
/// Asserts that the provided form for the provided entity is shown.
/// </summary>
/// <param name="formName">The name of the form.</param>
/// <param name="entityName">The name of the entity.</param>
[Then(@"I am presented with a '(.*)' form for the '(.*)' entity")]
public static void ThenIAmPresentedWithANewFormForTheEntity(string formName, string entityName)
{
XrmApp.Entity.GetFormName().Should().Be(formName);
XrmApp.Entity.GetEntityName().Should().Be(entityName);
}
/// <summary>
/// Asserts that an info form notification can be seen with the given message.
/// </summary>
/// <param name="message">The message of the notification.</param>
[Then(@"I can see an info form notification stating '(.*)'")]
public static void ThenICanSeeAnInfoFormNotificationStating(string message)
{
XrmApp.Entity.GetFormNotifications().Should().Contain(formNotification => formNotification.Type == FormNotificationType.Information && formNotification.Message == message);
}
/// <summary>
/// Asserts that a warning form notification can be seen with the given message.
/// </summary>
/// <param name="message">The message of the notification.</param>
[Then(@"I can see a warning form notification stating '(.*)'")]
public static void ThenICanSeeAWarningFormNotificationStating(string message)
{
XrmApp.Entity.GetFormNotifications().Should().Contain(formNotification => formNotification.Type == FormNotificationType.Warning && formNotification.Message == message);
}
/// <summary>
/// Asserts that an error form notification can be seen with the given message.
/// </summary>
/// <param name="message">The message of the notification.</param>
[Then(@"I can see an error form notification stating '(.*)'")]
public static void ThenICanSeeAnErrorFormNotificationStating(string message)
{
XrmApp.Entity.GetFormNotifications().Should().Contain(formNotification => formNotification.Type == FormNotificationType.Error && formNotification.Message == message);
}
/// <summary>
/// Asserts that the given value is in the header title.
/// </summary>
/// <param name="message">The header title.</param>
[Then(@"I can see a value of '(.*)' as the header title")]
public static void ThenICanSeeAsTheHeaderTitle(string message)
{
XrmApp.Entity.GetHeaderTitle().Should().Contain(message);
}
/// <summary>
/// Asserts that a field is editable on the form.
/// </summary>
/// <param name="fieldName">The name of the field.</param>
[Then(@"I can edit the '(.*)' field")]
public static void ThenICanEditTheField(string fieldName)
{
var field = XrmApp.Entity.GetField(fieldName);
field.IsVisible.Should().BeTrue(because: "the field must be visible to be editable");
field.IsReadOnly(Driver).Should().BeFalse(because: "the field should be editable");
}
/// <summary>
/// Asserts that the given values are available in an option set.
/// </summary>
/// <param name="fieldName">The name of the option set field.</param>
/// <param name="expectedOptionsTable">The options.</param>
[Then(@"I can see the following options in the '(.*)' option set field")]
public static void ThenICanSeeTheFollowingOptionsInTheOptionSetField(string fieldName, Table expectedOptionsTable)
{
if (expectedOptionsTable is null)
{
throw new ArgumentNullException(nameof(expectedOptionsTable));
}
if (!Driver.TryFindElement(By.CssSelector($"select[data-id*=\"{fieldName}.fieldControl-option-set-select\"]"), out var optionSet))
{
throw new InvalidOperationException($"Unable to find option set field {fieldName}.");
}
var expectedOptions = expectedOptionsTable.Rows.Select(r => r[0]);
foreach (var option in optionSet.FindElements(By.CssSelector("option")).Where(e => e.GetAttribute("value") != "-1"))
{
expectedOptions.Should().Contain(option.Text, because: "the options be in the list of expected options");
}
}
/// <summary>
/// Asserts that the provided fields are editable.
/// </summary>
/// <param name="table">A table containing the fields to assert against.</param>
[Then(@"I can edit the following fields")]
public static void ThenICanEditTheFollowingFields(Table table)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table));
}
var fields = table.Rows.Select((row) => row.Values.First());
foreach (var field in fields)
{
ThenICanEditTheField(field);
}
}
/// <summary>
/// Asserts that a field is read-only.
/// </summary>
/// <param name="fieldName">The name of the field.</param>
[Then(@"I can not edit the '(.*)' field")]
public static void ThenICanNotEditTheField(string fieldName)
{
XrmApp.Entity.GetField(fieldName).IsReadOnly(Driver).Should().BeTrue(because: "the field should not be editable");
}
/// <summary>
/// Asserts that the provided fields are not editable.
/// </summary>
/// <param name="table">A table containing the fields to assert against.</param>
[Then(@"I can not edit the following fields")]
public static void ThenICanNotEditTheFollowingFields(Table table)
{
if (table is null)
{
throw new ArgumentNullException(nameof(table));
}
foreach (var field in table.Rows.Select((row) => row.Values.First()))
{
ThenICanNotEditTheField(field);
}
}
/// <summary>
/// Asserts that a field is not visible.
/// </summary>
/// <param name="fieldName">The name of the field.</param>
[Then(@"I can see the '(.*)' field")]
public static void ThenICanSeeTheField(string fieldName)
{
XrmApp.Entity.IsFieldVisible(Driver, fieldName).Should().BeTrue(because: "the field should be visible");
}
/// <summary>
/// Asserts that a field is not visible.
/// </summary>
/// <param name="fieldName">The name of the field.</param>
[Then(@"I can not see the '(.*)' field")]
public static void ThenICanNotSeeTheField(string fieldName)
{
XrmApp.Entity.IsFieldVisible(Driver, fieldName).Should().BeFalse(because: "the field should not be visible");
}
/// <summary>
/// Asserts that a record is active or inactive.
/// </summary>
/// <param name="status">The status.</param>
[Then("the status of the record is (active|inactive)")]
public static void ThenTheStatusOfTheRecordIs(string status)
{
XrmApp.Entity.GetFooterStatusValue().Should().BeEquivalentTo(status);
}
private static void SetFieldValue(string fieldName, string fieldValue, string fieldType)
{
switch (fieldType)
{
case "multioptionset":
XrmApp.Entity.SetValue(
new MultiValueOptionSet()
{
Name = fieldName,
Values = fieldValue
.Split(',')
.Select(v => v.Trim())
.ToArray(),
},
true);
break;
case "optionset":
XrmApp.Entity.SetValue(new OptionSet()
{
Name = fieldName,
Value = fieldValue,
});
break;
case "boolean":
XrmApp.Entity.SetValue(new BooleanItem()
{
Name = fieldName,
Value = bool.Parse(fieldValue),
});
break;
case "datetime":
XrmApp.Entity.SetValue(new DateTimeControl(fieldName)
{
Value = DateTime.Parse(fieldValue, CultureInfo.CurrentCulture),
});
break;
case "lookup":
XrmApp.Entity.SetValue(new LookupItem()
{
Name = fieldName,
Value = fieldValue,
});
break;
case "currency":
case "numeric":
case "text":
default:
XrmApp.Entity.SetValue(fieldName, fieldValue);
break;
}
}
private static void SetHeaderFieldValue(string fieldName, string fieldValue, string fieldType)
{
switch (fieldType)
{
case "optionset":
XrmApp.Entity.SetHeaderValue(new OptionSet()
{
Name = fieldName,
Value = fieldValue,
});
break;
case "boolean":
XrmApp.Entity.SetHeaderValue(new BooleanItem()
{
Name = fieldName,
Value = bool.Parse(fieldValue),
});
break;
case "datetime":
XrmApp.Entity.SetHeaderValue(new DateTimeControl(fieldName)
{
Value = DateTime.Parse(fieldValue, CultureInfo.CurrentCulture),
});
break;
case "lookup":
XrmApp.Entity.SetHeaderValue(new LookupItem()
{
Name = fieldName,
Value = fieldValue,
});
break;
case "currency":
case "numeric":
case "text":
default:
XrmApp.Entity.SetHeaderValue(fieldName, fieldValue);
break;
}
}
}
}