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

'use strict';

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

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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 已提交
35 36
	const { left, top } = getTopLeftOffset(element as HTMLElement);

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

J
Joao Moreno 已提交
48 49
class WindowDriver implements IWindowDriver {

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

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

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

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

		if (!element) {
J
Joao Moreno 已提交
66
			return TPromise.wrapError(new Error('Element not found'));
J
Joao Moreno 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80
		}

		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 已提交
81 82 83
		x = Math.round(x);
		y = Math.round(y);

J
Joao Moreno 已提交
84 85 86
		return { x, y };
	}

J
Joao Moreno 已提交
87
	private async _click(selector: string, clickCount: number, xoffset?: number, yoffset?: number): Promise<void> {
J
Joao Moreno 已提交
88
		const { x, y } = await this._getElementXY(selector, xoffset, yoffset);
J
Joao Moreno 已提交
89
		const webContents = electron.remote.getCurrentWebContents();
90

J
Joao Moreno 已提交
91
		webContents.sendInputEvent({ type: 'mouseDown', x, y, button: 'left', clickCount } as any);
92
		await TPromise.timeout(10);
J
Joao Moreno 已提交
93
		webContents.sendInputEvent({ type: 'mouseUp', x, y, button: 'left', clickCount } as any);
J
Joao Moreno 已提交
94 95

		await TPromise.timeout(100);
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) {
J
Joao Moreno 已提交
102
			return TPromise.wrapError(new Error('Element not found'));
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
			}

J
Joao Moreno 已提交
134
			return TPromise.wrapError(new Error(`Active element not found. Current active element is '${chain.join(' > ')}'`));
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) {
J
Joao Moreno 已提交
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) {
J
Joao Moreno 已提交
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) {
J
Joao Moreno 已提交
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) {
J
Joao Moreno 已提交
200
			return TPromise.wrapError(new Error('Element not found'));
J
Joao Moreno 已提交
201 202
		}

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

		if (!xterm) {
J
Joao Moreno 已提交
206
			return TPromise.wrapError(new Error('Xterm not found'));
J
Joao Moreno 已提交
207 208
		}

D
Daniel Imms 已提交
209
		xterm._core.send(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);

231 232 233
	const options = await windowDriverRegistry.registerWindowDriver(windowId);

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

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