driver.ts 7.3 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { TPromise } from 'vs/base/common/winjs.base';
J
Joao Moreno 已提交
7
import { IDisposable, toDisposable, combinedDisposable } from 'vs/base/common/lifecycle';
J
Joao Moreno 已提交
8 9
import { IWindowDriver, IElement, WindowDriverChannel, WindowDriverRegistryChannelClient } from 'vs/platform/driver/node/driver';
import { IPCClient } from 'vs/base/parts/ipc/node/ipc';
J
Joao Moreno 已提交
10
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
J
Joao Moreno 已提交
11 12
import { getTopLeftOffset, getClientArea } from 'vs/base/browser/dom';
import * as electron from 'electron';
13
import { IWindowService } from 'vs/platform/windows/common/windows';
D
Daniel Imms 已提交
14
import { Terminal } from 'vscode-xterm';
J
Joao Moreno 已提交
15
import { timeout } from 'vs/base/common/async';
J
Joao Moreno 已提交
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
function serializeElement(element: Element, recursive: boolean): IElement {
	const attributes = Object.create(null);

	for (let j = 0; j < element.attributes.length; j++) {
		const attr = element.attributes.item(j);
		attributes[attr.name] = attr.value;
	}

	const children = [];

	if (recursive) {
		for (let i = 0; i < element.children.length; i++) {
			children.push(serializeElement(element.children.item(i), true));
		}
	}

J
Joao Moreno 已提交
33 34
	const { left, top } = getTopLeftOffset(element as HTMLElement);

35 36 37 38 39
	return {
		tagName: element.tagName,
		className: element.className,
		textContent: element.textContent || '',
		attributes,
J
Joao Moreno 已提交
40 41 42
		children,
		left,
		top
43 44 45
	};
}

J
Joao Moreno 已提交
46 47
class WindowDriver implements IWindowDriver {

48 49 50
	constructor(
		@IWindowService private windowService: IWindowService
	) { }
J
Joao Moreno 已提交
51

J
Joao Moreno 已提交
52
	click(selector: string, xoffset?: number, yoffset?: number): TPromise<void> {
J
Joao Moreno 已提交
53
		return this._click(selector, 1, xoffset, yoffset);
J
Joao Moreno 已提交
54 55 56
	}

	doubleClick(selector: string): TPromise<void> {
J
Joao Moreno 已提交
57
		return this._click(selector, 2);
J
Joao Moreno 已提交
58 59
	}

J
Joao Moreno 已提交
60
	private _getElementXY(selector: string, xoffset?: number, yoffset?: number): TPromise<{ x: number; y: number; }> {
J
Joao Moreno 已提交
61 62 63
		const element = document.querySelector(selector);

		if (!element) {
B
Benjamin Pasero 已提交
64
			return TPromise.wrapError(new Error(`Element not found: ${selector}`));
J
Joao Moreno 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78
		}

		const { left, top } = getTopLeftOffset(element as HTMLElement);
		const { width, height } = getClientArea(element as HTMLElement);
		let x: number, y: number;

		if ((typeof xoffset === 'number') || (typeof yoffset === 'number')) {
			x = left + xoffset;
			y = top + yoffset;
		} else {
			x = left + (width / 2);
			y = top + (height / 2);
		}

J
Joao Moreno 已提交
79 80 81
		x = Math.round(x);
		y = Math.round(y);

J
Joao Moreno 已提交
82
		return TPromise.as({ x, y });
J
Joao Moreno 已提交
83 84
	}

J
Joao Moreno 已提交
85 86
	private _click(selector: string, clickCount: number, xoffset?: number, yoffset?: number): TPromise<void> {
		return this._getElementXY(selector, xoffset, yoffset).then(({ x, y }) => {
87

88
			const webContents: electron.WebContents = (electron as any).remote.getCurrentWebContents();
J
Joao Moreno 已提交
89
			webContents.sendInputEvent({ type: 'mouseDown', x, y, button: 'left', clickCount } as any);
J
Joao Moreno 已提交
90

J
Joao Moreno 已提交
91
			return TPromise.wrap(timeout(10)).then(() => {
J
Joao Moreno 已提交
92
				webContents.sendInputEvent({ type: 'mouseUp', x, y, button: 'left', clickCount } as any);
J
Joao Moreno 已提交
93
				return TPromise.wrap(timeout(100));
J
Joao Moreno 已提交
94 95
			});
		});
J
Joao Moreno 已提交
96 97
	}

J
Joao Moreno 已提交
98
	setValue(selector: string, text: string): TPromise<void> {
J
Joao Moreno 已提交
99 100 101
		const element = document.querySelector(selector);

		if (!element) {
B
Benjamin Pasero 已提交
102
			return TPromise.wrapError(new Error(`Element not found: ${selector}`));
J
Joao Moreno 已提交
103 104
		}

J
Joao Moreno 已提交
105 106 107 108 109
		const inputElement = element as HTMLInputElement;
		inputElement.value = text;

		const event = new Event('input', { bubbles: true, cancelable: true });
		inputElement.dispatchEvent(event);
J
Joao Moreno 已提交
110 111

		return TPromise.as(null);
J
Joao Moreno 已提交
112 113
	}

J
Joao Moreno 已提交
114 115
	getTitle(): TPromise<string> {
		return TPromise.as(document.title);
J
Joao Moreno 已提交
116 117
	}

J
Joao Moreno 已提交
118
	isActiveElement(selector: string): TPromise<boolean> {
J
Joao Moreno 已提交
119
		const element = document.querySelector(selector);
120 121

		if (element !== document.activeElement) {
122 123
			const chain = [];
			let el = document.activeElement;
124

125 126 127 128 129
			while (el) {
				const tagName = el.tagName;
				const id = el.id ? `#${el.id}` : '';
				const classes = el.className.split(/\s+/g).map(c => c.trim()).filter(c => !!c).map(c => `.${c}`).join('');
				chain.unshift(`${tagName}${id}${classes}`);
J
Joao Moreno 已提交
130 131

				el = el.parentElement;
132 133
			}

B
Benjamin Pasero 已提交
134
			return TPromise.wrapError(new Error(`Active element not found. Current active element is '${chain.join(' > ')}'. Looking for ${selector}`));
135 136
		}

J
Joao Moreno 已提交
137
		return TPromise.as(true);
J
Joao Moreno 已提交
138 139
	}

J
Joao Moreno 已提交
140
	getElements(selector: string, recursive: boolean): TPromise<IElement[]> {
J
Joao Moreno 已提交
141 142 143 144 145
		const query = document.querySelectorAll(selector);
		const result: IElement[] = [];

		for (let i = 0; i < query.length; i++) {
			const element = query.item(i);
146
			result.push(serializeElement(element, recursive));
J
Joao Moreno 已提交
147 148
		}

J
Joao Moreno 已提交
149
		return TPromise.as(result);
J
Joao Moreno 已提交
150
	}
J
Joao Moreno 已提交
151

J
Joao Moreno 已提交
152
	typeInEditor(selector: string, text: string): TPromise<void> {
J
Joao Moreno 已提交
153 154 155
		const element = document.querySelector(selector);

		if (!element) {
B
Benjamin Pasero 已提交
156
			return TPromise.wrapError(new Error(`Editor not found: ${selector}`));
J
Joao Moreno 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169
		}

		const textarea = element as HTMLTextAreaElement;
		const start = textarea.selectionStart;
		const newStart = start + text.length;
		const value = textarea.value;
		const newValue = value.substr(0, start) + text + value.substr(start);

		textarea.value = newValue;
		textarea.setSelectionRange(newStart, newStart);

		const event = new Event('input', { 'bubbles': true, 'cancelable': true });
		textarea.dispatchEvent(event);
J
Joao Moreno 已提交
170 171

		return TPromise.as(null);
J
Joao Moreno 已提交
172 173
	}

J
Joao Moreno 已提交
174
	getTerminalBuffer(selector: string): TPromise<string[]> {
J
Joao Moreno 已提交
175 176 177
		const element = document.querySelector(selector);

		if (!element) {
B
Benjamin Pasero 已提交
178
			return TPromise.wrapError(new Error(`Terminal not found: ${selector}`));
J
Joao Moreno 已提交
179 180
		}

D
Daniel Imms 已提交
181
		const xterm: Terminal = (element as any).xterm;
J
Joao Moreno 已提交
182 183

		if (!xterm) {
B
Benjamin Pasero 已提交
184
			return TPromise.wrapError(new Error(`Xterm not found: ${selector}`));
J
Joao Moreno 已提交
185
		}
J
Joao Moreno 已提交
186 187 188

		const lines: string[] = [];

D
Daniel Imms 已提交
189 190
		for (let i = 0; i < xterm._core.buffer.lines.length; i++) {
			lines.push(xterm._core.buffer.translateBufferLineToString(i, true));
J
Joao Moreno 已提交
191 192
		}

J
Joao Moreno 已提交
193
		return TPromise.as(lines);
J
Joao Moreno 已提交
194
	}
195

J
Joao Moreno 已提交
196
	writeInTerminal(selector: string, text: string): TPromise<void> {
J
Joao Moreno 已提交
197 198 199
		const element = document.querySelector(selector);

		if (!element) {
B
Benjamin Pasero 已提交
200
			return TPromise.wrapError(new Error(`Element not found: ${selector}`));
J
Joao Moreno 已提交
201 202
		}

D
Daniel Imms 已提交
203
		const xterm: Terminal = (element as any).xterm;
J
Joao Moreno 已提交
204 205

		if (!xterm) {
B
Benjamin Pasero 已提交
206
			return TPromise.wrapError(new Error(`Xterm not found: ${selector}`));
J
Joao Moreno 已提交
207 208
		}

D
Daniel Imms 已提交
209
		xterm._core.handler(text);
J
Joao Moreno 已提交
210 211

		return TPromise.as(null);
J
Joao Moreno 已提交
212 213
	}

J
Joao Moreno 已提交
214 215
	openDevTools(): TPromise<void> {
		return this.windowService.openDevTools({ mode: 'detach' });
216
	}
J
Joao Moreno 已提交
217 218
}

J
Joao Moreno 已提交
219 220 221 222
export async function registerWindowDriver(
	client: IPCClient,
	windowId: number,
	instantiationService: IInstantiationService
223
): Promise<IDisposable> {
J
Joao Moreno 已提交
224
	const windowDriver = instantiationService.createInstance(WindowDriver);
J
Joao Moreno 已提交
225 226 227 228 229 230
	const windowDriverChannel = new WindowDriverChannel(windowDriver);
	client.registerChannel('windowDriver', windowDriverChannel);

	const windowDriverRegistryChannel = client.getChannel('windowDriverRegistry');
	const windowDriverRegistry = new WindowDriverRegistryChannelClient(windowDriverRegistryChannel);

J
Joao Moreno 已提交
231
	await windowDriverRegistry.registerWindowDriver(windowId);
232
	// const options = await windowDriverRegistry.registerWindowDriver(windowId);
233

234 235 236
	// if (options.verbose) {
	// 	windowDriver.openDevTools();
	// }
J
Joao Moreno 已提交
237

J
Joao Moreno 已提交
238 239
	const disposable = toDisposable(() => windowDriverRegistry.reloadWindowDriver(windowId));
	return combinedDisposable([disposable, client]);
240
}