Introduce reader object to read result from HTML table

and present it as list of plain js objects. Updated page object assertion method to be simpler.
This commit is contained in:
BorysLevytskyi
2015-12-20 18:51:10 +02:00
parent f955f7d9b3
commit a01ca1c670
3 changed files with 72 additions and 57 deletions

View File

@@ -1,5 +1,6 @@
var Key = protractor.Key;
var By = protractor.By;
var resultTableReader = require('./resultTableReader');
function BitwiseCmdPage(driver, appUrl) {
this.driver = driver;
@@ -24,7 +25,6 @@ BitwiseCmdPage.prototype.goToApp = function (hashValue) {
};
BitwiseCmdPage.prototype.sendCommand = function(cmd) {
console.log('\r\nSend command: ' + cmd + "\r\n");
return this.driver.findElement(By.id('in')).then(function (el) {
return el.sendKeys(cmd + Key.ENTER);
});
@@ -69,60 +69,23 @@ function ExpressionResultObject(resultElement) {
this.resultElement = resultElement;
}
ExpressionResultObject.prototype.shouldBe = function(expectedResult) {
return this.resultElement.findElements(By.tagName('tr'))
.then(function(rows) {
var actualLength = rows.length + 0;
var expectedLength = expectedResult.length + 0;
expect(actualLength).toBe(expectedLength);
if(actualLength != expectedLength) {
//TODO: I don't know why but expect doesn't throw exception...
throw new Error("Inconsistent length");
}
var all = null, cur, expectedRow, actualRow;
for (var i = 0; i < rows.length; i++) {
expectedRow = expectedResult[i];
actualRow = rows[i];
cur = ExpressionResultObject.assertSingleRowResult(actualRow, expectedRow);
all = all == null ? cur : all.then(cur);
}
return all;
});
ExpressionResultObject.prototype.readResult = function () {
return resultTableReader.read(this.resultElement);
};
ExpressionResultObject.assertSingleRowResult = function(actualRow, expectedRow) {
ExpressionResultObject.prototype.shouldBe = function(expectedResult) {
this.readResult().then(function(actualResult) {
expect(actualResult.length).toEqual(expectedResult.length, "Unexpected result length");
var expected, actual;
for(var i=0;i<expectedResult.length; i++) {
var actual = actualResult[i],
expected = expectedResult[i];
if(expectedRow == null) {
throw new Error("Expected row is null")
}
var p = actualRow.findElement(by.css('.label')).then(function (tbLabel) {
expect(tbLabel.getText()).toBe(expectedRow.label);
}).then(function () {
return actualRow.findElement(by.css('.bin'));
}).then(function (tdBin) {
expect(tdBin.getText()).toBe(expectedRow.bin);
}).then(function () {
return actualRow.findElement(by.css('.other'));
}).then(function (tdOther) {
expect(tdOther.getText()).toBe(expectedRow.other);
});
if(expectedRow.sign != null) {
console.log('has sign!!!!!!!!!!!!!!!!!!!!!!!!');
p = p.then(function () {
return actualRow.findElement(by.css('.sign'));
}).then(function (tdSign) {
expect(tdSign.getText()).toBe(expectedRow.sign);
});
expect(actual).toEqual(jasmine.objectContaining(expected));
}
});
return p;
};
};
module.exports = {
BitwiseCmdPage : BitwiseCmdPage,

View File

@@ -0,0 +1,56 @@
var promise = protractor.promise;
var resultTableReader = {
read: function (table) {
var self = this;
return table.findElements(By.tagName('tr'))
.then(function(rows) {
var promises = [];
for (var i = 0; i < rows.length; i++) {
promises.push(self.readRow(rows[i]));
}
return promise.all(promises);
});
},
readRow: function (row) {
var def = promise.defer();
var dataList = [];
var promises = [];
var self = this;
row.findElements(By.tagName('td')).then(function(cols) {
for(var i=0; i<cols.length;i++){
promises.push(self.readColumn(cols[i]).then(function(colData) { dataList.push(colData); }))
}
});
promise.all(promises).then(function() {
var rowObj = {}, colObj;
for(var i=0; i<dataList.length; i++ ){
colObj = dataList[i];
rowObj[colObj.className] = colObj.text;
}
def.fulfill(rowObj);
});
return def.promise;
},
readColumn: function(col) {
var def = promise.defer();
var colData = {};
var promises = [];
promises.push(col.getAttribute('class').then(function(className) { colData.className = className; }))
promises.push(col.getText().then(function(text) { colData.text = text; }))
promise.all(promises).then(function () {
def.fulfill(colData);
});
return def.promise;
}
};
module.exports = resultTableReader;

View File

@@ -14,12 +14,8 @@ describe('launch of application', function() {
});
});
it('should have no errors title', function() {
sutPage.goToApp().then(function() {
driver.findElements(By.css('.result .error')).then(function(els) {
expect(els.length).toBe(0, "There should be no errors on auto launch");
});
});
it('should have no errors upon loading', function() {
return sutPage.shouldHaveNoErrors();
});
it('should execute clear command', function() {
@@ -73,7 +69,7 @@ describe('launch of application', function() {
it('should do NOT operation', function() {
return assertOperation('~1',
[{ sing: '~', label: '1', bin:'00000000000000000000000000000001', other: '0x1'},
[{ sign: '~', label: '1', bin:'00000000000000000000000000000001', other: '0x1'},
{ sign: '=', label: '-2', bin:'11111111111111111111111111111110', other: '-0x2'}])
});