driver.ts 6.7 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';
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
	async click(selector: string, xoffset?: number, yoffset?: number): TPromise<void> {
J
Joao Moreno 已提交
53 54 55 56 57 58 59
		return this._click(selector, 1, xoffset, yoffset);
	}

	doubleClick(selector: string): TPromise<void> {
		return this._click(selector, 2);
	}

J
Joao Moreno 已提交
60
	private async _getElementXY(selector: string, xoffset?: number, yoffset?: number): TPromise<{ x: number; y: number; }> {
J
Joao Moreno 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
		const element = document.querySelector(selector);

		if (!element) {
			throw new Error('Element not found');
		}

		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 83 84 85 86
		return { x, y };
	}

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

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

		await TPromise.timeout(100);
J
Joao Moreno 已提交
94 95 96 97 98 99 100 101 102
	}

	async setValue(selector: string, text: string): TPromise<void> {
		const element = document.querySelector(selector);

		if (!element) {
			throw new Error('Element not found');
		}

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

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

	async getTitle(): TPromise<string> {
		return document.title;
	}

	async isActiveElement(selector: string): TPromise<boolean> {
		const element = document.querySelector(selector);
116 117

		if (element !== document.activeElement) {
118 119
			const chain = [];
			let el = document.activeElement;
120

121 122 123 124 125
			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 已提交
126 127

				el = el.parentElement;
128 129 130
			}

			throw new Error(`Active element not found. Current active element is '${chain.join(' > ')}'`);
131 132 133
		}

		return true;
J
Joao Moreno 已提交
134 135
	}

136
	async getElements(selector: string, recursive: boolean): TPromise<IElement[]> {
J
Joao Moreno 已提交
137 138 139 140 141
		const query = document.querySelectorAll(selector);
		const result: IElement[] = [];

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

		return result;
	}
J
Joao Moreno 已提交
147

J
Joao Moreno 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	async typeInEditor(selector: string, text: string): TPromise<void> {
		const element = document.querySelector(selector);

		if (!element) {
			throw new Error('Editor not found: ' + selector);
		}

		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 已提交
168 169 170 171 172 173 174
	async getTerminalBuffer(selector: string): TPromise<string[]> {
		const element = document.querySelector(selector);

		if (!element) {
			throw new Error('Terminal not found: ' + selector);
		}

J
Joao Moreno 已提交
175 176 177 178 179
		const xterm = (element as any).xterm;

		if (!xterm) {
			throw new Error('Xterm not found: ' + selector);
		}
J
Joao Moreno 已提交
180 181 182

		const lines: string[] = [];

J
Joao Moreno 已提交
183 184
		for (let i = 0; i < xterm.buffer.lines.length; i++) {
			lines.push(xterm.buffer.translateBufferLineToString(i, true));
J
Joao Moreno 已提交
185 186 187
		}

		return lines;
J
Joao Moreno 已提交
188
	}
189

J
Joao Moreno 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	async writeInTerminal(selector: string, text: string): TPromise<void> {
		const element = document.querySelector(selector);

		if (!element) {
			throw new Error('Element not found');
		}

		const xterm = (element as any).xterm;

		if (!xterm) {
			throw new Error('Xterm not found');
		}

		xterm.send(text);
	}

206 207 208
	async openDevTools(): TPromise<void> {
		await this.windowService.openDevTools({ mode: 'detach' });
	}
J
Joao Moreno 已提交
209 210
}

J
Joao Moreno 已提交
211 212 213 214 215 216
export async function registerWindowDriver(
	client: IPCClient,
	windowId: number,
	instantiationService: IInstantiationService
): TPromise<IDisposable> {
	const windowDriver = instantiationService.createInstance(WindowDriver);
J
Joao Moreno 已提交
217 218 219 220 221 222
	const windowDriverChannel = new WindowDriverChannel(windowDriver);
	client.registerChannel('windowDriver', windowDriverChannel);

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

223 224 225
	const options = await windowDriverRegistry.registerWindowDriver(windowId);

	if (options.verbose) {
J
Joao Moreno 已提交
226
		// windowDriver.openDevTools();
227
	}
J
Joao Moreno 已提交
228

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