提交 a7675860 编写于 作者: J Joao

smoke test: fix bad code

上级 30e81176
......@@ -70,42 +70,43 @@ export class Extensions {
}
private getExtensionIndex(name: string, extensionListSelector: string): Promise<number> {
return new Promise(async (res, rej) => {
const html = await this.spectron.waitFor(this.spectron.client.getHTML, extensionListSelector);
let extensionIndex: number = 0;
let extension: boolean;
let tags: string[] = [];
let parser = new htmlparser.Parser({
onopentag: function (name, attribs) {
if (name === 'div' && attribs.class === 'extension') {
extensionIndex++;
extension = true;
return this.spectron.waitFor(this.spectron.client.getHTML, extensionListSelector).then(html => {
return new Promise<number>((res, rej) => {
let extensionIndex: number = 0;
let extension: boolean;
let tags: string[] = [];
let parser = new htmlparser.Parser({
onopentag: function (name, attribs) {
if (name === 'div' && attribs.class === 'extension') {
extensionIndex++;
extension = true;
}
if (extension) {
tags.push(name);
}
},
ontext: function (text) {
if (extension && text === name) {
parser.end();
}
},
onclosetag: function (name) {
if (extension) {
tags.pop();
}
if (extension && tags.length === 0) {
extension = false;
}
},
onend: function () {
if (extensionIndex === 0) {
return rej(`${name} extension was not found.`);
}
return res(extensionIndex);
}
if (extension) {
tags.push(name);
}
},
ontext: function (text) {
if (extension && text === name) {
parser.end();
}
},
onclosetag: function (name) {
if (extension) {
tags.pop();
}
if (extension && tags.length === 0) {
extension = false;
}
},
onend: function () {
if (extensionIndex === 0) {
return rej(`${name} extension was not found.`);
}
return res(extensionIndex);
}
});
parser.write(html);
});
parser.write(html);
});
}
}
\ No newline at end of file
......@@ -131,36 +131,37 @@ export class Git {
}
private getFirstChangeIndex(changeClass: string, selector: string): Promise<number> {
return new Promise(async (res, rej) => {
const html = await this.spectron.waitFor(this.spectron.client.getHTML, selector);
let lineIndex: number = 0;
let changeFound: boolean;
let tags: string[] = [];
let parser = new htmlparser.Parser({
onopentag: function (name: string, attribs: any) {
tags.push(name);
if (name === 'div' && !attribs.class) {
lineIndex++;
} else if (name === 'div' && attribs.class === changeClass) {
changeFound = true;
parser.end();
return this.spectron.waitFor(this.spectron.client.getHTML, selector).then(html => {
return new Promise<number>((res, rej) => {
let lineIndex: number = 0;
let changeFound: boolean;
let tags: string[] = [];
let parser = new htmlparser.Parser({
onopentag: function (name: string, attribs: any) {
tags.push(name);
if (name === 'div' && !attribs.class) {
lineIndex++;
} else if (name === 'div' && attribs.class === changeClass) {
changeFound = true;
parser.end();
}
},
onclosetag: function (name) {
// Terminate once last tag is closed
tags.pop();
if (!changeFound && tags.length === 0) {
parser.end();
}
},
onend: function () {
if (!changeFound) {
return rej(`No changes in the diff found.`);
}
return res(lineIndex);
}
},
onclosetag: function (name) {
// Terminate once last tag is closed
tags.pop();
if (!changeFound && tags.length === 0) {
parser.end();
}
},
onend: function () {
if (!changeFound) {
return rej(`No changes in the diff found.`);
}
return res(lineIndex);
}
});
parser.write(html);
});
parser.write(html);
});
}
}
\ No newline at end of file
......@@ -130,55 +130,57 @@ export class JavaScript {
}
private getLineIndexOfFirst(string: string, selector: string): Promise<number> {
return new Promise(async (res, rej) => {
const html = await this.spectron.waitFor(this.spectron.client.getHTML, selector);
let lineIndex: number = 0;
let stringFound: boolean;
let parser = new htmlparser.Parser({
onopentag: function (name: string, attribs: any) {
if (name === 'div' && attribs.class === 'view-line') {
lineIndex++;
return this.spectron.waitFor(this.spectron.client.getHTML, selector).then(html => {
return new Promise<number>((res, rej) => {
let lineIndex: number = 0;
let stringFound: boolean;
let parser = new htmlparser.Parser({
onopentag: function (name: string, attribs: any) {
if (name === 'div' && attribs.class === 'view-line') {
lineIndex++;
}
},
ontext: function (text) {
if (!stringFound && text === string) {
stringFound = true;
parser.end();
}
},
onend: function () {
if (!stringFound) {
return rej(`No ${string} in editor found.`);
}
return res(lineIndex);
}
},
ontext: function (text) {
if (!stringFound && text === string) {
stringFound = true;
parser.end();
}
},
onend: function () {
if (!stringFound) {
return rej(`No ${string} in editor found.`);
}
return res(lineIndex);
}
});
parser.write(html);
});
parser.write(html);
});
}
private getLineIndexOfFirstFoldableElement(selector: string): Promise<number> {
return new Promise(async (res, rej) => {
const html = await this.spectron.waitFor(this.spectron.client.getHTML, selector);
let lineIndex: number = 0;
let foldFound: boolean;
let parser = new htmlparser.Parser({
onopentag: function (name: string, attribs: any) {
if (name === 'div' && !attribs.class) {
lineIndex++;
} else if (name === 'div' && attribs.class.indexOf('cldr folding') !== -1) {
foldFound = true;
parser.end();
}
},
onend: function () {
if (!foldFound) {
return rej(`No foldable elements found.`);
return this.spectron.waitFor(this.spectron.client.getHTML, selector).then(html => {
return new Promise<number>((res, rej) => {
let lineIndex: number = 0;
let foldFound: boolean;
let parser = new htmlparser.Parser({
onopentag: function (name: string, attribs: any) {
if (name === 'div' && !attribs.class) {
lineIndex++;
} else if (name === 'div' && attribs.class.indexOf('cldr folding') !== -1) {
foldFound = true;
parser.end();
}
},
onend: function () {
if (!foldFound) {
return rej(`No foldable elements found.`);
}
return res(lineIndex);
}
return res(lineIndex);
}
});
parser.write(html);
});
parser.write(html);
});
}
}
\ No newline at end of file
......@@ -21,16 +21,8 @@ export class Screenshot {
}
public async capture(): Promise<any> {
return new Promise(async (res, rej) => {
const image: Electron.NativeImage = await this.spectron.app.browserWindow.capturePage();
fs.writeFile(`${this.testPath}/${this.index}.png`, image, (err) => {
if (err) {
rej(err);
}
this.index++;
res();
});
});
const image = await this.spectron.app.browserWindow.capturePage();
await new Promise((c, e) => fs.writeFile(`${this.testPath}/${this.index++}.png`, image, err => err ? e(err) : c()));
}
private createFolder(name: string): void {
......
......@@ -119,24 +119,16 @@ function runTests(): void {
});
}
function cleanOrClone(repo: string, dir: string): Promise<any> {
async function cleanOrClone(repo: string, dir: string): Promise<any> {
console.log('Cleaning or cloning test project repository...');
return new Promise(async (res, rej) => {
if (!folderExists(dir)) {
await gitClone(repo, dir);
res();
} else {
git.cwd(dir);
git.fetch(async err => {
if (err) {
rej(err);
}
await gitResetAndClean();
res();
});
}
});
if (!folderExists(dir)) {
await gitClone(repo, dir);
} else {
git.cwd(dir);
await new Promise((c, e) => git.fetch(err => err ? e(err) : c()));
await gitResetAndClean();
}
}
function gitClone(repo: string, dir: string): Promise<any> {
......@@ -148,22 +140,10 @@ function gitClone(repo: string, dir: string): Promise<any> {
});
}
function gitResetAndClean(): Promise<any> {
return new Promise((res, rej) => {
git.reset(['FETCH_HEAD', '--hard'], err => {
if (err) {
rej(err);
}
git.clean('f', ['-d'], err => {
if (err) {
rej(err);
}
console.log('Test project was successfully reset to initial state.');
res();
});
});
});
async function gitResetAndClean(): Promise<any> {
await new Promise((c, e) => git.reset(['FETCH_HEAD', '--hard'], err => err ? e(err) : c()));
await new Promise((c, e) => git.clean('f', ['-d'], err => err ? e(err) : c()));
console.log('Test project was successfully reset to initial state.');
}
function execute(cmd: string, dir: string): Promise<any> {
......
......@@ -6,9 +6,8 @@
const MochaTest = require('mocha');
const mochaTest = new MochaTest({
timeout: 360000,
retries: 2,
slow: 50000,
timeout: 60000,
slow: 10000,
useColors: true
});
mochaTest.addFile(require('path').join(process.cwd(), 'out/test.js'));
......
......@@ -68,13 +68,9 @@ export class SpectronApplication {
}
public async start(): Promise<any> {
try {
await this.spectron.start();
await this.focusOnWindow(1); // focuses on main renderer window
return this.checkWindowReady();
} catch (err) {
throw err;
}
await this.spectron.start();
await this.focusOnWindow(1); // focuses on main renderer window
await this.checkWindowReady();
}
public async stop(): Promise<any> {
......@@ -95,8 +91,8 @@ export class SpectronApplication {
return this.client.windowByIndex(index);
}
private checkWindowReady(): Promise<any> {
return this.waitFor(this.spectron.client.getHTML, '[id="workbench.main.container"]');
private async checkWindowReady(): Promise<any> {
await this.waitFor(this.spectron.client.getHTML, '[id="workbench.main.container"]');
}
private retrieveKeybindings() {
......@@ -112,30 +108,27 @@ export class SpectronApplication {
});
}
private callClientAPI(func: (...args: any[]) => Promise<any>, args: any): Promise<any> {
private async callClientAPI(func: (...args: any[]) => Promise<any>, args: any): Promise<any> {
let trial = 1;
return new Promise(async (res, rej) => {
while (true) {
if (trial > this.pollTrials) {
rej(`Could not retrieve the element in ${this.testRetry * this.pollTrials * this.pollTimeout} seconds.`);
break;
}
let result;
try {
result = await func.call(this.client, args, false);
} catch (e) { }
if (result && result !== '') {
await this.screenshot.capture();
res(result);
break;
}
await this.wait();
trial++;
while (true) {
if (trial > this.pollTrials) {
throw new Error(`Could not retrieve the element in ${this.testRetry * this.pollTrials * this.pollTimeout} seconds.`);
}
});
let result;
try {
result = await func.call(this.client, args, false);
} catch (e) { }
if (result && result !== '') {
await this.screenshot.capture();
return result;
}
await this.wait();
trial++;
}
}
/**
......
......@@ -3,8 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { testDataLoss } from './tests/data-loss';
import { testDataMigration } from './tests/data-migration';
import { testDataLoss } from './tests/data-loss';
import { testExplorer } from './tests/explorer';
import { testConfigViews } from './tests/configuration-views';
import { testSearch } from './tests/search';
......
......@@ -22,7 +22,7 @@ export function testDataLoss() {
dl = new DataLoss(app);
await common.removeDirectory(USER_DIR);
return await app.start();
await app.start();
});
afterEach(async function () {
return await app.stop();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册