-
Notifications
You must be signed in to change notification settings - Fork 11
/
Form.ts
140 lines (119 loc) · 3.75 KB
/
Form.ts
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
import * as playwright from "playwright";
import { EnsureXrmGetter } from "./Global";
import { XrmUiTest } from "./XrmUITest";
/**
* Scheme to describe a form reference
* Either by name or by id
*/
export interface FormIdentifier {
/**
* The name of the form
*/
byName?: string;
/**
* The id of the form
*/
byId?: string;
}
/**
* Scheme to describe items of form selector
*/
export interface FormSelectorItem {
/**
* Id of the form
*/
id?: string;
/**
* Label of the form
*/
label?: string;
}
/**
* Module for interacting with D365 Forms
*/
export class Form {
private _page: playwright.Page;
constructor(private xrmUiTest: XrmUiTest) {
this._page = xrmUiTest.page;
this.xrmUiTest = xrmUiTest;
}
/**
* Sets all attributes to submit mode none. This is useful if you don't want to save and just change the page. No prompt for unsaved data will open.
*
* @returns Promise which resolves once all attribute submit modes are set
* @deprecated Please use noSubmit
*/
reset = async () => {
return this.noSubmit();
}
/**
* Sets all attributes to submit mode none. This is useful if you don't want to save and just change the page. No prompt for unsaved data will open.
*
* @returns Promise which resolves once all attribute submit modes are set.
*/
noSubmit = async () => {
await EnsureXrmGetter(this._page);
return this._page.evaluate(() => {
const xrm = window.oss_FindXrm();
xrm.Page.getAttribute().forEach(a => a.setSubmitMode("never"));
});
}
/**
* Gets data about the currently selected form
*
* @returns Object containing id and label of current form
*/
getCurrentFormId = async (): Promise<FormSelectorItem> => {
await EnsureXrmGetter(this._page);
return this._page.evaluate(() => {
const xrm = window.oss_FindXrm();
const form = xrm.Page.ui.formSelector.getCurrentItem();
return {
id: form.getId(),
label: form.getLabel()
};
});
}
/**
* Gets a list of all forms that are currently available in the form selector
*
* @returns Array of objects with id and label
*/
getAvailableForms = async (): Promise<Array<FormSelectorItem>> => {
await EnsureXrmGetter(this._page);
return this._page.evaluate(() => {
const xrm = window.oss_FindXrm();
return xrm.Page.ui.formSelector.items.get().map(i => ({ id: i.getId(), label: i.getLabel()}));
});
}
/**
* Switches to the specified form
*
* @param identifier Defines which form to switch to
* @returns Promise which resolves once the selected form is loaded
*/
switch = async (identifier: FormIdentifier) => {
await EnsureXrmGetter(this._page);
if (identifier.byId) {
return Promise.all([
this._page.evaluate((i) => {
const xrm = window.oss_FindXrm();
(xrm.Page.ui.formSelector.items.get(i) as any).navigate();
}, identifier.byId),
this.xrmUiTest.waitForIdleness()
]);
}
else if (identifier.byName) {
return Promise.all([
this._page.evaluate((i) => {
const xrm = window.oss_FindXrm();
(xrm.Page.ui.formSelector.items as any).getByFilter((f: any) => f._label === i).navigate();
}, identifier.byName),
this.xrmUiTest.waitForIdleness()
]);
}
else {
throw new Error("Choose to search by id or name");
}
}
}