I can't handle it() - expect() couple along with exceljs's readfile() function. With this code i get the following error : "UnhandledPromiseRejectionWarning: Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out"
I tried removing the first it() and just surrounding the expect()s with the it()s but then "hello2" doesn't show up, which means excelsjs's readFile() isn't even read in that case.
This clearly has something to do with promises and how you should handle them but i just can't manage to find a solution. Thanks in advance for any help.
import { AppPage } from './app.po';
import { browser, logging, element, by } from 'protractor'
import { Workbook, Row, Cell, Worksheet } from 'exceljs';
describe('E2E Tests', () => {
const log4js = require('log4js');
log4js.configure({
appenders: { igwan: { type: 'file', filename: 'cheese.log' } },
categories: { default: { appenders: ['igwan'], level: 'error' } }
});
const logger = log4js.getLogger('IGWAN');
logger.level = 'trace';
//console.log = (msg) => logger.trace(msg);
let page = new AppPage();
let wb: Workbook = new Workbook();
let sheet: Worksheet;
let rowObject: Row;
let cellObject: Cell;
let deviceName: string;
let dataContainer: any[] = [];
let dataContainer3: any[];
let row: any[] = []
browser.manage().timeouts().implicitlyWait(30000);
page.navigateToPark();
it('whatever man', () => {
wb.xlsx.readFile('e2e/src/testsE2E.xlsx').then(() => {
sheet = wb.getWorksheet("Eqpt");
for (let k = 1; k <= sheet.rowCount; k++) {
dataContainer.push([]);
}
for (let l = 1; l <= sheet.rowCount; l++) {
rowObject = sheet.getRow(l);
for (let m = 1; m <= sheet.columnCount; m++) {
cellObject = rowObject.getCell(m);
dataContainer[l - 1].push(cellObject.toString());
}
}
console.log("hello2");
return dataContainer;
}).then((result) => {
dataContainer3 = result;
console.log(dataContainer3);
for (let i = 0; i < dataContainer3.length; i++) {
row = dataContainer3[i];
if (row[0].toString() === "1") {
deviceName = row[1];
element(by.id('device_input')).clear().then(() => {
element(by.id('device_input')).sendKeys(deviceName);
});
element(by.id('search_button')).click();
for (let j = 2; j <= row.length; j++) {
switch (j) {
case 2: {
//it('should fetch and display correct type data for device ' + deviceName.toString(), () => {
expect(element(by.id('type')).getText()).toEqual(dataContainer[i][j]);
//});
break;
}
case 3: {
//it('should fetch and display correct POP data for device ' + deviceName.toString(), () => {
expect(element(by.id('pop')).getText()).toEqual(dataContainer[i][j]);
//});
break;
}
case 4: {
//it('should fetch and display correct ipMngt data for device ' + deviceName.toString(), () => {
expect(element(by.id('ipMngt')).getText()).toEqual(dataContainer[i][j]);
//});
break;
}
case 5: {
//it('should fetch and display correct internalState data for device ' + deviceName.toString(), () => {
expect(element(by.id('internalState')).getText()).toEqual(dataContainer[i][j]);
//});
break;
}
case 6: {
//it('should fetch and display correct prodState data for device ' + deviceName.toString(), () => {
expect(element(by.id('prodState')).getText()).toEqual(dataContainer[i][j]);
//});
break;
}
case 7: {
//it('should fetch and display correct currentSwRelease data for device ' + deviceName.toString(), () => {
expect(element(by.id('currentSwRelease')).getText()).toEqual(dataContainer[i][j]);
//});
break;
}
case 8: {
//it('should fetch and display correct hwChassis data for device ' + deviceName.toString(), () => {
expect(element(by.id('hwChassis')).getText()).toEqual(dataContainer[i][j]);
//});
break;
}
case 9: {
//it('should fetch and display correct rfsNode data for device ' + deviceName.toString(), () => {
expect(element(by.id('rfsNode')).getText()).toEqual(dataContainer[i][j]);
//});
break;
}
case 10: {
//it('should fetch and display correct peId data for device ' + deviceName.toString(), () => {
expect(element(by.id('peId')).getText()).toEqual(dataContainer[i][j]);
//});
break;
}
}
}
} else {
console.log("Line was marked as skipped by author of .xlsx file");
}
}
});
});
//sheet = wb.getWorksheet("POP");
// TO DO
//sheet = wb.getWorksheet("Clients");
// TO DO
afterEach(async () => {
// Assert that there are no errors emitted from the browser
const logs = await browser.manage().logs().get(logging.Type.BROWSER);
expect(logs).not.toContain(jasmine.objectContaining({
level: logging.Level.SEVERE,
} as logging.Entry));
});
});
I would recommend doing the following:
Turn off the control flow of Protractor and switch to the async/await syntax. The control flow feature is deprecated see docs.
In my experience understanding what happens behind the scene with the control flow is much harder than switching to async/await syntax. This will give you complete control over how the code gets executed. You can do this by adding SELENIUM_PROMISE_MANAGER: false
to your protractor config file.
So you code snippets would have to look something like this:
import { AppPage } from './app.po';
import { browser, logging, element, by } from 'protractor';
import { Workbook, Row, Cell, Worksheet } from 'exceljs';
import * as log4js from 'log4js';
describe('E2E Tests', () => {
let wb: Workbook;
let sheet: Worksheet;
let rowObject: Row;
let cellObject: Cell;
let deviceName: string;
let dataContainer: any[];
let row: any[];
let logger;
beforeAll(async () => {
log4js.configure({
appenders: { igwan: { type: 'file', filename: 'cheese.log' } },
categories: { default: { appenders: ['igwan'], level: 'error' } }
});
logger = log4js.getLogger('IGWAN');
logger.level = 'trace';
});
beforeEach(async () => {
const page = new AppPage();
wb = new Workbook();
dataContainer = [];
row = [];
await browser.sleep(5000);
await page.navigateToPark();
});
//console.log = (msg) => logger.trace(msg);
it('whatever man', async () => {
dataContainer = await wb.xlsx.readFile('e2e/src/testsE2E.xlsx').then(() => {
sheet = wb.getWorksheet('Eqpt');
for (let k = 1; k <= sheet.rowCount; k++) {
dataContainer.push([]);
}
for (let l = 1; l <= sheet.rowCount; l++) {
rowObject = sheet.getRow(l);
for (let m = 1; m <= sheet.columnCount; m++) {
cellObject = rowObject.getCell(m);
dataContainer[l - 1].push(cellObject.toString());
}
}
console.log('hello2');
return dataContainer;
});
for (let i = 0; i < dataContainer.length; i++) {
row = dataContainer[i];
if (row[0].toString() === '1') {
deviceName = row[1];
await element(by.id('device_input')).clear();
await element(by.id('device_input')).sendKeys(deviceName);
await element(by.id('search_button')).click();
for (let j = 2; j <= row.length; j++) {
switch (j) {
case 2: {
//it('should fetch and display correct type data for device ' + deviceName.toString(), () => {
expect(await element(by.id('type')).getText()).toEqual(
dataContainer[i][j]
);
//});
break;
}
case 3: {
//it('should fetch and display correct POP data for device ' + deviceName.toString(), () => {
expect(await element(by.id('pop')).getText()).toEqual(
dataContainer[i][j]
);
//});
break;
}
case 4: {
//it('should fetch and display correct ipMngt data for device ' + deviceName.toString(), () => {
expect(await element(by.id('ipMngt')).getText()).toEqual(
dataContainer[i][j]
);
//});
break;
}
case 5: {
//it('should fetch and display correct internalState data for device ' + deviceName.toString(), () => {
expect(await element(by.id('internalState')).getText()).toEqual(
dataContainer[i][j]
);
//});
break;
}
case 6: {
//it('should fetch and display correct prodState data for device ' + deviceName.toString(), () => {
expect(await element(by.id('prodState')).getText()).toEqual(
dataContainer[i][j]
);
//});
break;
}
case 7: {
//it('should fetch and display correct currentSwRelease data for device ' + deviceName.toString(), () => {
expect(
await element(by.id('currentSwRelease')).getText()
).toEqual(dataContainer[i][j]);
//});
break;
}
case 8: {
//it('should fetch and display correct hwChassis data for device ' + deviceName.toString(), () => {
expect(await element(by.id('hwChassis')).getText()).toEqual(
dataContainer[i][j]
);
//});
break;
}
case 9: {
//it('should fetch and display correct rfsNode data for device ' + deviceName.toString(), () => {
expect(await element(by.id('rfsNode')).getText()).toEqual(
dataContainer[i][j]
);
//});
break;
}
case 10: {
//it('should fetch and display correct peId data for device ' + deviceName.toString(), () => {
expect(await element(by.id('peId')).getText()).toEqual(
dataContainer[i][j]
);
//});
break;
}
}
}
} else {
console.log('Line was marked as skipped by author of .xlsx file');
}
}
});
//sheet = wb.getWorksheet("POP");
// TO DO
//sheet = wb.getWorksheet("Clients");
// TO DO
afterEach(async () => {
// Assert that there are no errors emitted from the browser
const logs = await browser
.manage()
.logs()
.get(logging.Type.BROWSER);
expect(logs).not.toContain(
jasmine.objectContaining({
level: logging.Level.SEVERE
} as logging.Entry)
);
});
});