driver all the way to the windows

This commit is contained in:
Joao Moreno
2018-04-06 18:07:29 +02:00
parent f246e6fab8
commit 8421745dee
16 changed files with 358 additions and 115 deletions

View File

@@ -0,0 +1,128 @@
/*---------------------------------------------------------------------------------------------
* 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 { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IChannel } from 'vs/base/parts/ipc/common/ipc';
export const ID = 'driverService';
export const IDriver = createDecorator<IDriver>(ID);
//*START
export interface IElement {
tagName: string;
className: string;
textContent: string;
}
export interface IDriver {
_serviceBrand: any;
getWindowIds(): TPromise<number[]>;
getElements(windowId: number, selector: string): TPromise<IElement[]>;
}
//*END
export interface IDriverChannel extends IChannel {
call(command: 'getWindowIds'): TPromise<number[]>;
call(command: 'getElements', arg: [number, string]): TPromise<IElement[]>;
call(command: string, arg: any): TPromise<any>;
}
export class DriverChannel implements IDriverChannel {
constructor(private driver: IDriver) { }
call(command: string, arg?: any): TPromise<any> {
switch (command) {
case 'getWindowIds': return this.driver.getWindowIds();
case 'getElements': return this.driver.getElements(arg[0], arg[1]);
}
return undefined;
}
}
export class DriverChannelClient implements IDriver {
_serviceBrand: any;
constructor(private channel: IDriverChannel) { }
getWindowIds(): TPromise<number[]> {
return this.channel.call('getWindowIds');
}
getElements(windowId: number, selector: string): TPromise<IElement[]> {
return this.channel.call('getElements', [windowId, selector]);
}
}
export interface IWindowDriverRegistry {
registerWindowDriver(windowId: number): TPromise<void>;
}
export interface IWindowDriverRegistryChannel extends IChannel {
call(command: 'registerWindowDriver', arg: number): TPromise<void>;
call(command: string, arg: any): TPromise<any>;
}
export class WindowDriverRegistryChannel implements IWindowDriverRegistryChannel {
constructor(private registry: IWindowDriverRegistry) { }
call(command: string, arg?: any): TPromise<any> {
switch (command) {
case 'registerWindowDriver': return this.registry.registerWindowDriver(arg);
}
return undefined;
}
}
export class WindowDriverRegistryChannelClient implements IWindowDriverRegistry {
_serviceBrand: any;
constructor(private channel: IWindowDriverRegistryChannel) { }
registerWindowDriver(windowId: number): TPromise<void> {
return this.channel.call('registerWindowDriver', windowId);
}
}
export interface IWindowDriver {
getElements(selector: string): TPromise<IElement[]>;
}
export interface IWindowDriverChannel extends IChannel {
call(command: 'getElements', arg: string): TPromise<IElement[]>;
call(command: string, arg: any): TPromise<any>;
}
export class WindowDriverChannel implements IWindowDriverChannel {
constructor(private driver: IWindowDriver) { }
call(command: string, arg?: any): TPromise<any> {
switch (command) {
case 'getElements': return this.driver.getElements(arg);
}
return undefined;
}
}
export class WindowDriverChannelClient implements IWindowDriver {
_serviceBrand: any;
constructor(private channel: IWindowDriverChannel) { }
getElements(selector: string): TPromise<IElement[]> {
return this.channel.call('getElements', selector);
}
}

View File

@@ -0,0 +1,44 @@
/*---------------------------------------------------------------------------------------------
* 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';
class WindowDriver implements IWindowDriver {
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;
}
}
export async function registerWindowDriver(client: IPCClient, windowId: number): TPromise<IDisposable> {
const windowDriver = new WindowDriver();
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;
}

View File

@@ -0,0 +1,71 @@
/*---------------------------------------------------------------------------------------------
* 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 { IDriver, DriverChannel, IElement, IWindowDriverChannel, WindowDriverChannelClient, IWindowDriverRegistry, WindowDriverRegistryChannel } from 'vs/platform/driver/common/driver';
import { IWindowsMainService } from 'vs/platform/windows/electron-main/windows';
import { serve as serveNet } from 'vs/base/parts/ipc/node/ipc.net';
import { combinedDisposable, IDisposable } from 'vs/base/common/lifecycle';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IPCServer, IClientRouter } from 'vs/base/parts/ipc/common/ipc';
class WindowRouter implements IClientRouter {
constructor(private windowId: number) { }
route(command: string, arg: any): string {
return `window:${this.windowId}`;
}
}
export class Driver implements IDriver, IWindowDriverRegistry {
_serviceBrand: any;
private registeredWindowIds = new Set<number>();
constructor(
private windowServer: IPCServer,
@IWindowsMainService private windowsService: IWindowsMainService
) { }
registerWindowDriver(windowId: number): TPromise<void> {
this.registeredWindowIds.add(windowId);
return TPromise.as(null);
}
async getWindowIds(): TPromise<number[]> {
return this.windowsService.getWindows()
.map(w => w.id)
.filter(id => this.registeredWindowIds.has(id));
}
getElements(windowId: number, selector: string): TPromise<IElement[], any> {
const router = new WindowRouter(windowId);
const windowDriverChannel = this.windowServer.getChannel<IWindowDriverChannel>('windowDriver', router);
const windowDriver = new WindowDriverChannelClient(windowDriverChannel);
return windowDriver.getElements(selector);
}
}
export async function serve(
windowServer: IPCServer,
handle: string,
instantiationService: IInstantiationService
): TPromise<IDisposable> {
const driver = instantiationService.createInstance(Driver, windowServer);
const windowDriverRegistryChannel = new WindowDriverRegistryChannel(driver);
windowServer.registerChannel('windowDriverRegistry', windowDriverRegistryChannel);
const server = await serveNet(handle);
const channel = new DriverChannel(driver);
server.registerChannel('driver', channel);
return combinedDisposable([server, windowServer]);
}

View File

@@ -0,0 +1,17 @@
/*---------------------------------------------------------------------------------------------
* 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 { IDriver, DriverChannelClient } from 'vs/platform/driver/common/driver';
import { connect as connectNet, Client } from 'vs/base/parts/ipc/node/ipc.net';
export async function connect(handle: string): TPromise<{ client: Client, driver: IDriver }> {
const client = await connectNet(handle, 'driverClient');
const channel = client.getChannel('driver');
const driver = new DriverChannelClient(channel);
return { client, driver };
}