-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathPreviewWidget.ts
142 lines (109 loc) · 5.28 KB
/
PreviewWidget.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
141
142
/*********************************************************************
* Copyright (c) 2019 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
import { injectable, inject } from 'inversify';
import { CLASSES } from '../../../inversify.types';
import { DriverHelper } from '../../../utils/DriverHelper';
import { By, error, Key } from 'selenium-webdriver';
import { TestConstants } from '../../../TestConstants';
import { Ide } from './Ide';
import { Logger } from '../../../utils/Logger';
import { TimeoutConstants } from '../../../TimeoutConstants';
/**
* @deprecated Preview widget is no longer a valid page fragment.
*/
@injectable()
export class PreviewWidget {
private static readonly WIDGET_LOCATOR: By = By.css('div.theia-mini-browser');
private static readonly WIDGET_IFRAME_LOCATOR: By = By.css('div.theia-mini-browser iframe');
private static readonly WIDGET_URL_LOCATOR: By = By.css('div.theia-mini-browser input');
private static readonly WIDGET_REFRESH_BUTTON_LOCATOR: By = By.css('div.theia-mini-browser-refresh.theia-mini-browser-button');
constructor(@inject(CLASSES.DriverHelper) private readonly driverHelper: DriverHelper,
@inject(CLASSES.Ide) private readonly ide: Ide) { }
async waitUrl(expectedUrl: string, timeout: number = TimeoutConstants.TS_WAIT_URL_TIMEOUT) {
Logger.debug(`PreviewWidget.waitUrl ${expectedUrl}`);
await this.driverHelper.waitAttributeValue(PreviewWidget.WIDGET_URL_LOCATOR, 'value', expectedUrl, timeout);
}
async getUrl(timeout: number = TimeoutConstants.TS_WAIT_URL_TIMEOUT): Promise<string> {
Logger.debug(`PreviewWidget.getUrl`);
return await this.driverHelper.waitAndGetElementAttribute(PreviewWidget.WIDGET_URL_LOCATOR, 'value', timeout);
}
async typeUrl(url: string, timeout: number = TimeoutConstants.TS_SELENIUM_CLICK_ON_VISIBLE_ITEM) {
Logger.debug(`PreviewWidget.typeUrl ${url}`);
await this.driverHelper.type(PreviewWidget.WIDGET_URL_LOCATOR, url, timeout);
}
async clearUrl() {
Logger.debug('PreviewWidget.clearUrl');
await this.typeUrl(Key.chord(Key.CONTROL, 'a', Key.DELETE));
await this.waitUrl('');
}
async typeAndApplyUrl(url: string, timeout: number = TimeoutConstants.TS_SELENIUM_CLICK_ON_VISIBLE_ITEM) {
Logger.debug(`PreviewWidget.typeAndApplyUrl ${url}`);
await this.clearUrl();
await this.typeUrl(Key.chord(url, Key.ENTER), timeout);
}
async waitApplicationOpened(expectedUrl: string, timeout: number) {
Logger.debug(`PreviewWidget.waitApplicationOpened ${expectedUrl}`);
await this.driverHelper.getDriver().wait(async () => {
try {
await this.waitUrl(expectedUrl, timeout / 5);
return true;
} catch (err) {
if (!(err instanceof error.TimeoutError)) {
throw err;
}
await this.typeAndApplyUrl(expectedUrl, timeout);
}
}, timeout);
}
/**
* @deprecated Method deprecated. Iframe is not available anymore
*/
async waitAndSwitchToWidgetFrame() {
Logger.debug('PreviewWidget.waitAndSwitchToWidgetFrame');
await this.driverHelper.waitAndSwitchToFrame(PreviewWidget.WIDGET_IFRAME_LOCATOR, TimeoutConstants.TS_SELENIUM_PREVIEW_WIDGET_DEFAULT_TIMEOUT);
}
async waitPreviewWidgetAbsence() {
Logger.debug('PreviewWidget.waitPreviewWidgetAbsence');
await this.driverHelper.waitDisappearance(PreviewWidget.WIDGET_LOCATOR);
}
async waitContentAvailable(contentLocator: By,
timeout: number,
polling: number = TestConstants.TS_SELENIUM_DEFAULT_POLLING * 5) {
Logger.debug(`PreviewWidget.waitContentAvailable ${contentLocator}`);
await this.waitAndSwitchToWidgetFrame();
await this.driverHelper.getDriver().wait(async () => {
const isApplicationTitleVisible: boolean = await this.driverHelper.isVisible(contentLocator);
if (isApplicationTitleVisible) {
return true;
}
await this.driverHelper.wait(polling);
}, timeout);
}
async waitVisibility(element: By, timeout: number) {
Logger.debug(`PreviewWidget.waitVisibility ${element}`);
await this.driverHelper.waitVisibility(element, timeout);
}
async waitAndClick(element: By, timeout: number = TimeoutConstants.TS_SELENIUM_PREVIEW_WIDGET_DEFAULT_TIMEOUT) {
Logger.debug(`PreviewWidget.waitAndClick ${element}`);
await this.driverHelper.waitAndClick(element, timeout);
}
async refreshPage() {
Logger.debug('PreviewWidget.refreshPage');
await this.driverHelper.waitAndClick(PreviewWidget.WIDGET_REFRESH_BUTTON_LOCATOR);
}
/**
* @deprecated Method deprecated. Iframe is not available.
*/
async switchBackToIdeFrame() {
Logger.debug('PreviewWidget.switchBackToIdeFrame');
await this.driverHelper.getDriver().switchTo().defaultContent();
await this.ide.waitAndSwitchToIdeFrame();
}
}