-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-element.ts
More file actions
91 lines (75 loc) · 2.86 KB
/
html-element.ts
File metadata and controls
91 lines (75 loc) · 2.86 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
import { Locator } from 'playwright-core';
import { IHTMLElement } from '../interface/controls/html-element';
import { IPageObject } from '../interface/page/page-object';
import { IConfirmation } from '../interface/wallet/confirmation';
import { PageObject } from '../page';
import { DappDriver } from '../session/dapp-driver';
import { Frame, Page } from '../types';
export class PlaywrightHTMLElement implements IHTMLElement {
protected page: Page;
protected frame: Frame;
protected webElement: Locator;
private element: Locator;
protected timeout: number;
constructor(cssLocator: string, timeout: number = 20000, element: any = null) {
this.page = DappDriver.Instance.Page;
this.frame = DappDriver.Instance.Frame;
this.element = element;
this.timeout = timeout;
this.webElement = this.getWebElement(cssLocator);
}
getWebElement(cssLocator: string): Locator {
let locator: Locator;
if (this.element) {
locator = this.element;
} else {
locator = !this.frame ? this.page.locator(cssLocator) : this.frame.locator(cssLocator);
}
return locator;
}
async click(): Promise<void> {
await this.webElement.click({ timeout: this.timeout });
}
async clickRedirectsTo<TPage>(page: new () => TPage): Promise<TPage> {
await this.webElement.click({ timeout: this.timeout });
return await DappDriver.getPage<TPage>(page);
}
async clickAndWait(duration: number): Promise<void> {
await this.click();
await DappDriver.sleep(duration);
}
async clickAndSwitchToWindow<TPage extends IConfirmation | IPageObject>(page: new () => TPage): Promise<TPage> {
await this.click();
return await new PageObject().waitAndSwitchToWindow(page);
}
async getAttribute(attribute: string): Promise<string | null> {
return await this.webElement.getAttribute(attribute, { timeout: this.timeout });
}
async getCssValue(property: string): Promise<string | null> {
return await this.webElement.evaluate(
(element, property) => window.getComputedStyle(element).getPropertyValue(property),
property
);
}
async getText(): Promise<string> {
return await this.webElement.innerText({ timeout: this.timeout });
}
async hover(): Promise<void> {
return await this.webElement.hover({ timeout: this.timeout });
}
async isDisplayed(): Promise<boolean> {
return (await this.webElement.count()) > 0;
}
async isEnabled(): Promise<boolean> {
return await this.webElement.isEnabled({ timeout: this.timeout });
}
async isVisible(): Promise<boolean> {
return await this.webElement.isVisible({ timeout: this.timeout });
}
async type(keys: string): Promise<void> {
await this.webElement.pressSequentially(keys, { timeout: this.timeout });
}
async waitForText(text: RegExp): Promise<void> {
await this.webElement.filter({ hasText: text }).waitFor({ timeout: this.timeout });
}
}