-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-element.ts
More file actions
119 lines (102 loc) · 3.65 KB
/
html-element.ts
File metadata and controls
119 lines (102 loc) · 3.65 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
import { WebDriver, WebElement, WebElementPromise, error, until } from 'selenium-webdriver';
import { IHTMLElement } from '../interface/controls/html-element';
import { IPageObject } from '../interface/page/page-object';
import { IConfirmation } from '../interface/wallet/confirmation';
import { logWarning } from '../log';
import { PageObject } from '../page';
import { DappDriver } from '../session/dapp-driver';
export class WebDriverHTMLElement implements IHTMLElement {
private cssLocator: string;
protected driver: WebDriver;
protected webElement: WebElement;
private element: WebElementPromise;
private timeout: number;
constructor(cssLocator: string, timeout: number = 20000, element: WebElementPromise = null) {
this.cssLocator = cssLocator;
this.driver = DappDriver.Instance.Driver as WebDriver;
this.element = element;
this.timeout = timeout;
this.webElement = null;
}
async search(): Promise<void> {
await this.driver.manage().setTimeouts({ implicit: this.timeout });
if (this.cssLocator.startsWith('xpath=')) {
this.webElement = !(await this.element)
? await this.driver.findElement({ xpath: this.cssLocator.substring(6, this.cssLocator.length) })
: await this.element;
} else {
this.webElement = !(await this.element)
? await this.driver.findElement({ css: this.cssLocator })
: await this.element;
}
await this.driver.manage().setTimeouts({ implicit: 20000 });
}
async hardClick(): Promise<void> {
await this.search();
await this.driver.wait(until.elementIsVisible(this.webElement), 10000);
await this.driver.wait(until.elementIsEnabled(this.webElement), 10000);
await this.webElement.click();
}
async click(): Promise<void> {
try {
await this.hardClick();
} catch (err) {
if (err instanceof error.StaleElementReferenceError) {
logWarning(err.name + ': ' + this.cssLocator);
await this.hardClick();
}
}
}
async clickRedirectsTo<TPage>(page: new () => TPage): Promise<TPage> {
await this.click();
return await DappDriver.getPage<TPage>(page);
}
async clickAndWait(duration: number): Promise<void> {
await this.click();
return 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> {
await this.search();
return await this.webElement.getAttribute(attribute);
}
async getCssValue(property: string): Promise<string | null> {
await this.search();
return await this.webElement.getCssValue(property);
}
async getText(): Promise<string> {
await this.search();
return await this.webElement.getText();
}
async hover(): Promise<void> {
await this.search();
return await this.driver.actions({ async: true }).move({ origin: this.webElement }).perform();
}
async isDisplayed(): Promise<boolean> {
try {
await this.search();
return true;
} catch (ex) {
return false;
}
}
async isEnabled(): Promise<boolean> {
await this.search();
return await this.webElement.isEnabled();
}
async isVisible(): Promise<boolean> {
await this.search();
return await this.webElement.isDisplayed();
}
async type(keys: string): Promise<void> {
await this.search();
await this.webElement.sendKeys(keys);
}
async waitForText(text: RegExp): Promise<void> {
await this.search();
await this.driver.wait(until.elementTextMatches(this.webElement, text), this.timeout);
}
}