Skip to content

Commit 86fe750

Browse files
committed
slim down driver API
1 parent 6651dd5 commit 86fe750

2 files changed

Lines changed: 46 additions & 31 deletions

File tree

test/smoke/src/areas/search/search.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class Search extends Viewlet {
6060
}
6161

6262
async removeFileMatch(index: number): Promise<void> {
63-
await this.api.waitAndMoveToObject(`${VIEWLET} .results .monaco-tree-rows>:nth-child(${index}) .filematch`);
63+
await this.api.waitAndMove(`${VIEWLET} .results .monaco-tree-rows>:nth-child(${index}) .filematch`);
6464
const file = await this.api.waitForTextContent(`${VIEWLET} .results .monaco-tree-rows>:nth-child(${index}) .filematch a.label-name`);
6565
await this.api.waitAndClick(`${VIEWLET} .results .monaco-tree-rows>:nth-child(${index}) .filematch .action-label.icon.action-remove`);
6666
await this.api.waitForTextContent(`${VIEWLET} .results .monaco-tree-rows>:nth-child(${index}) .filematch a.label-name`, void 0, result => result !== file);
@@ -77,7 +77,7 @@ export class Search extends Viewlet {
7777
}
7878

7979
async replaceFileMatch(index: number): Promise<void> {
80-
await this.api.waitAndMoveToObject(`${VIEWLET} .results .monaco-tree-rows>:nth-child(${index}) .filematch`);
80+
await this.api.waitAndMove(`${VIEWLET} .results .monaco-tree-rows>:nth-child(${index}) .filematch`);
8181
await this.api.waitAndClick(`${VIEWLET} .results .monaco-tree-rows>:nth-child(${index}) .filematch .action-label.icon.action-replace-all`);
8282
}
8383

test/smoke/src/spectron/client.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { SpectronClient } from 'spectron';
7-
import { RawResult, Element } from 'webdriverio';
87
import { ScreenCapturer } from '../helpers/screenshot';
98

109
export interface APIElement {
@@ -13,30 +12,66 @@ export interface APIElement {
1312
textContent: string;
1413
}
1514

15+
export interface Driver {
16+
keys(keys: string[]): Promise<void>;
17+
getElements(selector: string): Promise<APIElement[]>;
18+
}
19+
20+
export class SpectronDriver implements Driver {
21+
22+
constructor(private spectronClient: SpectronClient) { }
23+
24+
keys(keys: string[]): Promise<void> {
25+
this.spectronClient.keys(keys);
26+
return Promise.resolve();
27+
}
28+
29+
async getElements(selector: string): Promise<APIElement[]> {
30+
const result = await (this.spectronClient.execute(selector => {
31+
const query = document.querySelectorAll(selector);
32+
const result: APIElement[] = [];
33+
34+
for (let i = 0; i < query.length; i++) {
35+
const element: HTMLElement = query.item(i);
36+
37+
result.push({
38+
tagName: element.tagName,
39+
className: element.className,
40+
textContent: element.textContent || ''
41+
});
42+
}
43+
44+
return result;
45+
}, selector) as any as Promise<{ value: APIElement[]; }>);
46+
47+
return result.value;
48+
}
49+
}
50+
1651
export class API {
1752

1853
// waitFor calls should not take more than 200 * 100 = 20 seconds to complete, excluding
1954
// the time it takes for the actual retry call to complete
2055
private retryCount: number;
2156
private readonly retryDuration = 100; // in milliseconds
57+
private driver: Driver;
2258

2359
constructor(
2460
private spectronClient: SpectronClient,
2561
private screenCapturer: ScreenCapturer,
2662
waitTime: number
2763
) {
64+
this.driver = new SpectronDriver(spectronClient);
2865
this.retryCount = (waitTime * 1000) / this.retryDuration;
2966
}
3067

3168
keys(keys: string[]): Promise<void> {
32-
this.spectronClient.keys(keys);
33-
return Promise.resolve();
69+
return this.driver.keys(keys);
3470
}
3571

3672
async waitForTextContent(selector: string, textContent?: string, accept?: (result: string) => boolean): Promise<string> {
3773
accept = accept ? accept : (result => textContent !== void 0 ? textContent === result : !!result);
38-
const fn = async () => await this.spectronClient.selectorExecute(selector, div => Array.isArray(div) ? div[0].textContent : div.textContent);
39-
return this.waitFor(fn, s => accept!(typeof s === 'string' ? s : ''), `getTextContent with selector ${selector}`);
74+
return this.waitFor(() => this.driver.getElements(selector).then(els => els[0].textContent), s => accept!(typeof s === 'string' ? s : ''), `getTextContent with selector ${selector}`);
4075
}
4176

4277
async waitAndClick(selector: string, xoffset?: number, yoffset?: number): Promise<any> {
@@ -47,7 +82,7 @@ export class API {
4782
return this.waitFor(() => this.spectronClient.doubleClick(selector), void 0, `doubleClick with selector ${selector}`);
4883
}
4984

50-
async waitAndMoveToObject(selector: string): Promise<any> {
85+
async waitAndMove(selector: string): Promise<any> {
5186
return this.waitFor(() => this.spectronClient.moveToObject(selector), void 0, `move to object with selector ${selector}`);
5287
}
5388

@@ -65,31 +100,11 @@ export class API {
65100
}
66101

67102
async waitForElements(selector: string, accept: (result: APIElement[]) => boolean = result => result.length > 0): Promise<APIElement[]> {
68-
const _fn: any = () => {
69-
return this.spectronClient.execute(selector => {
70-
const query = document.querySelectorAll(selector);
71-
const result: APIElement[] = [];
72-
73-
for (let i = 0; i < query.length; i++) {
74-
const element: HTMLElement = query.item(i);
75-
76-
result.push({
77-
tagName: element.tagName,
78-
className: element.className,
79-
textContent: element.textContent || ''
80-
});
81-
}
82-
83-
return result;
84-
}, selector)
85-
.then(result => result.value);
86-
};
87-
88-
return this.waitFor(_fn, accept, `elements with selector ${selector}`) as Promise<any>;
103+
return this.waitFor(() => this.driver.getElements(selector), accept, `elements with selector ${selector}`) as Promise<any>;
89104
}
90105

91-
async waitForElement(selector: string, accept: (result: Element | undefined) => boolean = result => !!result): Promise<void> {
92-
return this.waitFor<RawResult<Element>>(() => this.spectronClient.element(selector), result => accept(result ? result.value : void 0), `element with selector ${selector}`) as Promise<any>;
106+
async waitForElement(selector: string, accept: (result: APIElement | undefined) => boolean = result => !!result): Promise<void> {
107+
return this.waitFor(() => this.driver.getElements(selector).then(els => els[0]), accept, `element with selector ${selector}`) as Promise<any>;
93108
}
94109

95110
async waitForActiveElement(selector: string): Promise<any> {

0 commit comments

Comments
 (0)