driver.ts 2.9 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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';
import { IDisposable } from 'vs/base/common/lifecycle';
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 15

class WindowDriver implements IWindowDriver {

J
Joao Moreno 已提交
16
	constructor() { }
J
Joao Moreno 已提交
17

J
Joao Moreno 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	click(selector: string, xoffset?: number, yoffset?: number): TPromise<void> {
		throw new Error('Method not implemented.');
	}

	doubleClick(selector: string): TPromise<void> {
		throw new Error('Method not implemented.');
	}

	move(selector: string): TPromise<void> {
		throw new Error('Method not implemented.');
	}

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

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

J
Joao Moreno 已提交
37 38 39 40 41
		const inputElement = element as HTMLInputElement;
		inputElement.value = text;

		const event = new Event('input', { bubbles: true, cancelable: true });
		inputElement.dispatchEvent(event);
J
Joao Moreno 已提交
42 43 44 45 46 47 48 49 50 51 52
	}

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

	async isActiveElement(selector: string): TPromise<boolean> {
		const element = document.querySelector(selector);
		return element === document.activeElement;
	}

J
Joao Moreno 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	async getElements(selector: string): TPromise<IElement[]> {
		const query = document.querySelectorAll(selector);
		const result: IElement[] = [];

		for (let i = 0; i < query.length; i++) {
			const element = query.item(i);

			result.push({
				tagName: element.tagName,
				className: element.className,
				textContent: element.textContent || ''
			});
		}

		return result;
	}
J
Joao Moreno 已提交
69 70 71 72

	selectorExecute<P>(selector: string, script: (elements: HTMLElement[], ...args: any[]) => P, ...args: any[]): TPromise<P> {
		return TPromise.wrapError(new Error('not implemented'));
	}
J
Joao Moreno 已提交
73 74
}

J
Joao Moreno 已提交
75 76 77 78 79 80
export async function registerWindowDriver(
	client: IPCClient,
	windowId: number,
	instantiationService: IInstantiationService
): TPromise<IDisposable> {
	const windowDriver = instantiationService.createInstance(WindowDriver);
J
Joao Moreno 已提交
81 82 83 84 85 86 87 88 89 90
	const windowDriverChannel = new WindowDriverChannel(windowDriver);
	client.registerChannel('windowDriver', windowDriverChannel);

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

	await windowDriverRegistry.registerWindowDriver(windowId);

	return client;
}