-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-element.ts
More file actions
178 lines (177 loc) · 5.55 KB
/
html-element.ts
File metadata and controls
178 lines (177 loc) · 5.55 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
import { PLAYWRIGHT, WEBDRIVER } from '../constants';
import { IHTMLElement } from '../interface/controls/html-element';
import { IPageObject } from '../interface/page/page-object';
import { IConfirmation } from '../interface/wallet/confirmation';
import { PlaywrightHTMLElement } from '../playwright/html-element';
import { DappDriver } from '../session/dapp-driver';
import { toRegExp } from '../utils';
import { WebDriverHTMLElement } from '../webdriver/html-element';
/**
*
*
* @export
* @class HTMLElement
* @implements {IHTMLElement}
*/
export class HTMLElement implements IHTMLElement {
protected cssLocator: string;
private element: any;
private timeout: number;
/**
* Creates an instance of HTMLElement.
* @param {string} cssLocator
* @param {number} [timeout=20000]
* @param {*} [element=null]
* @memberof HTMLElement
*/
constructor(cssLocator: string, timeout: number = 20000, element: any = null) {
this.cssLocator = cssLocator;
this.element = element;
this.timeout = timeout;
}
/**
*
*
* @protected
* @param {keyof IHTMLElement} methodName
* @param {Array<any>} [args=[]]
* @return {*} {Promise<any>}
* @memberof HTMLElement
*/
protected async callIfMethodExists(methodName: keyof IHTMLElement, args: Array<any> = []): Promise<any> {
let htmlElement: PlaywrightHTMLElement | WebDriverHTMLElement;
if (DappDriver.Instance.Framework === PLAYWRIGHT) {
htmlElement = new PlaywrightHTMLElement(this.cssLocator, this.timeout, this.element);
} else if (DappDriver.Instance.Framework === WEBDRIVER) {
htmlElement = new WebDriverHTMLElement(this.cssLocator, this.timeout, this.element);
}
return await (htmlElement[methodName] as Function)(...args);
}
/**
*
* Schedules a command to click on this element
* @return {*} {Promise<void>}
* @memberof HTMLElement
*/
async click(): Promise<void> {
return await this.callIfMethodExists('click');
}
/**
*
* Schedules a command to click on this element and return a new page object
* @template TPage
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof HTMLElement
*/
async clickRedirectsTo<TPage>(page: new () => TPage): Promise<TPage> {
return await this.callIfMethodExists('clickRedirectsTo', [page]);
}
/**
*
* Schedules a command to click on this element and switch the focus of all future commands to a given window
* @template TPage
* @param {new () => TPage} page
* @return {*} {Promise<TPage>}
* @memberof HTMLElement
*/
async clickAndSwitchToWindow<TPage extends IConfirmation | IPageObject>(page: new () => TPage): Promise<TPage> {
return await this.callIfMethodExists('clickAndSwitchToWindow', [page]);
}
/**
*
* Schedules a command to click on this element and wait for the given amount of time
* @param {number} [duration=1000]
* @return {*} {Promise<void>}
* @memberof HTMLElement
*/
async clickAndWait(duration: number = 1000): Promise<void> {
return await this.callIfMethodExists('clickAndWait', [duration]);
}
/**
*
* Schedules a command to query for the value of the given attribute of the element
* @param {string} attribute
* @return {*} {(Promise<string | null>)}
* @memberof HTMLElement
*/
async getAttribute(attribute: string): Promise<string | null> {
return await this.callIfMethodExists('getAttribute', [attribute]);
}
/**
*
* Schedules a command to query for the value of the given css property of the element
* @param {string} property
* @return {*} {(Promise<string | null>)}
* @memberof HTMLElement
*/
async getCssValue(property: string): Promise<string | null> {
return await this.callIfMethodExists('getCssValue', [property]);
}
/**
*
* Schedules a command to query for the visible innerText of the element
* @return {*} {Promise<string>}
* @memberof HTMLElement
*/
async getText(): Promise<string> {
return await this.callIfMethodExists('getText');
}
/**
*
* Schedules a command to hover over this element
* @return {*} {Promise<void>}
* @memberof HTMLElement
*/
async hover(): Promise<void> {
return await this.callIfMethodExists('hover');
}
/**
*
* Schedules a command to query whether this element is currently displayed
* @return {*} {Promise<boolean>}
* @memberof HTMLElement
*/
async isDisplayed(): Promise<boolean> {
return await this.callIfMethodExists('isDisplayed');
}
/**
*
* Schedules a command to query whether the element is enabled
* @return {*} {Promise<boolean>}
* @memberof HTMLElement
*/
async isEnabled(): Promise<boolean> {
return await this.callIfMethodExists('isEnabled');
}
/**
*
* Schedules a command to query whether the element is visible
* @return {*} {Promise<boolean>}
* @memberof HTMLElement
*/
async isVisible(): Promise<boolean> {
return await this.callIfMethodExists('isVisible');
}
/**
*
* Schedules a command to type a sequence in the element
* @param {string} keys
* @return {*} {Promise<void>}
* @memberof HTMLElement
*/
async type(keys: string): Promise<void> {
return await this.callIfMethodExists('type', [keys]);
}
/**
*
* Schedules a command to wait for the visible innerText of the element
* @param {(string | RegExp)} [text]
* @return {*} {Promise<void>}
* @memberof HTMLElement
*/
async waitForText(text?: string | RegExp): Promise<void> {
text = text ? toRegExp(text) : new RegExp(/.+/);
return await this.callIfMethodExists('waitForText', [text]);
}
}