Use Page Object pattern in e2e tests: send commands

This commit is contained in:
BorysLevytskyi
2015-12-06 16:49:14 +02:00
parent fa2a2bec15
commit 4c98914e8c
4 changed files with 76 additions and 69 deletions

View File

@@ -2,7 +2,6 @@ exports.config = {
seleniumAddress: 'http://127.0.0.1:4444/wd/hub', seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
specs: [ specs: [
'./e2e/cmdDriver.js',
'./e2e/spec.js' './e2e/spec.js'
], ],

View File

@@ -2,7 +2,6 @@ exports.config = {
seleniumAddress: 'http://127.0.0.1:4444/wd/hub', seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
specs: [ specs: [
'./e2e/cmdDriver.js',
'./e2e/spec.js' './e2e/spec.js'
], ],

44
tests/e2e/pageObject.js Normal file
View File

@@ -0,0 +1,44 @@
var Key = protractor.Key;
var By = protractor.By;
function BitwiseCmdPage(driver, appUrl) {
this.driver = driver;
this.appUrl = appUrl;
}
BitwiseCmdPage.prototype.goToApp = function (hashValue) {
var url = this.appUrl;
var hash = hashValue || '-notrack';
if(hash.indexOf('-notrack') < 0) {
hash += "||-notrack";
}
if(url.indexOf("#") < 0) {
url += "#" + hash;
} else {
url += "||" + hash;
}
return this.driver.get(url);
};
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);
});
};
BitwiseCmdPage.prototype.clearResults = function () {
return this.sendCommand("clear");
};
BitwiseCmdPage.prototype.executeExpression = function(expr) {
var self = this;
return this.clearResults().then(function() {
return self.sendCommand(expr);
})
};
module.exports = BitwiseCmdPage;

View File

@@ -1,19 +1,20 @@
browser.ignoreSynchronization = true; browser.ignoreSynchronization = true;
var BitwiseCmdPage = require('./pageObject.js');
var By = protractor.By; var By = protractor.By;
var driver = browser.driver; var driver = browser.driver;
var appUrl = browser.params.appUrl || 'http://localhost:63342/BitwiseCmd/src/#clear'; var appUrl = browser.params.appUrl || 'http://localhost:63342/BitwiseCmd/src/#clear';
var Key = protractor.Key; var sutPage = new BitwiseCmdPage(driver, appUrl);
describe('launch of application', function() { describe('launch of application', function() {
it('should have title', function() { it('should have title', function() {
goToApp().then(function() { sutPage.goToApp().then(function() {
expect(driver.getTitle()).toEqual('BitwiseCmd'); expect(driver.getTitle()).toEqual('BitwiseCmd');
}); });
}); });
it('should have no errors title', function() { it('should have no errors title', function() {
goToApp().then(function() { sutPage.goToApp().then(function() {
driver.findElements(By.css('.result .error')).then(function(els) { driver.findElements(By.css('.result .error')).then(function(els) {
expect(els.length).toBe(0, "There should be no errors on auto launch"); expect(els.length).toBe(0, "There should be no errors on auto launch");
}); });
@@ -21,9 +22,7 @@ describe('launch of application', function() {
}); });
it('should execute clear command', function() { it('should execute clear command', function() {
goToApp() sutPage.clearResults().then(function () {
.then(function() { return sendCommand('clear')})
.then(function () {
return driver.findElements(By.css('.result')).then(function(list) { return driver.findElements(By.css('.result')).then(function(list) {
expect(list.length).toBe(0, "There should be no results after clear"); expect(list.length).toBe(0, "There should be no results after clear");
}); });
@@ -31,27 +30,23 @@ describe('launch of application', function() {
}); });
it('should execute list of commands without errors', function() { it('should execute list of commands without errors', function() {
sutPage.goToApp()
goToApp() .then(function() { return sutPage.executeExpression('clear')})
.then(function() { return sendCommand('clear')}) .then(function() { return sutPage.executeExpression('1')})
.then(function() { return sendCommand('1')}) .then(function() { return sutPage.executeExpression('1|2')})
.then(function() { return sendCommand('1|2')}) .then(function() { return sutPage.executeExpression('1^2')})
.then(function() { return sendCommand('1^2')}) .then(function() { return sutPage.executeExpression('0x1>>>0xf')})
.then(function() { return sendCommand('0x1>>>0xf')}) .then(function() { return sutPage.executeExpression('0x1 0xf')})
.then(function() { return sendCommand('0x1 0xf')}) .then(function() { return sutPage.executeExpression('0x1 | 0xf')})
.then(function() { return sendCommand('0x1 | 0xf')}) .then(function() { return sutPage.executeExpression('0x1 ^ 123')})
.then(function() { return sendCommand('0x1 ^ 123')}) .then(function() { return sutPage.executeExpression('1|2&3|5 |5')})
.then(function() { return sendCommand('1|2&3|5 |5')}) .then(function() { return sutPage.executeExpression('dark')})
.then(function() { return sendCommand('dark')}) .then(function() { return sutPage.executeExpression('light')})
.then(function() { return sendCommand('light')})
.then(assertNoErrors); .then(assertNoErrors);
}); });
it('should execute list of numbers', function() { it('should execute list of numbers', function() {
sutPage.executeExpression('3 0xf')
goToApp()
.then(function() { return sendCommand('clear')})
.then(function() { return sendCommand('3 0xf')})
.then(assertNoErrors) .then(assertNoErrors)
.then(function() { .then(function() {
return assertExpressionResult(driver, return assertExpressionResult(driver,
@@ -82,7 +77,7 @@ describe('launch of application', function() {
}); });
it('should execute multiple expressions from hash arguments', function() { it('should execute multiple expressions from hash arguments', function() {
return goToApp("16,15||16&15") return sutPage.goToApp("16,15||16&15")
.then(function() { return driver.navigate().refresh(); }) .then(function() { return driver.navigate().refresh(); })
.then(assertNoErrors) .then(assertNoErrors)
.then(function() { .then(function() {
@@ -160,15 +155,14 @@ describe('launch of application', function() {
xit('should emphasize bytes', function() { xit('should emphasize bytes', function() {
goToApp() goToApp()
.then(function() { return sendCommand('clear')}) .then(function() { return sutPage.executeExpression('1')})
.then(function() { return sendCommand('1')})
.then(function() { .then(function() {
return assertExpressionResult(driver, [{ label: '1', bin:'00000001', other: '0x1'}]) return assertExpressionResult(driver, [{ label: '1', bin:'00000001', other: '0x1'}])
}) })
.then(function() { return sendCommand('clear')}) .then(function() { return sutPage.executeExpression('clear')})
// .then(function() { return sendCommand('em')}) // .then(function() { return sendCommand('em')})
.then(assertNoErrors) .then(assertNoErrors)
.then(function() { return sendCommand('1 3')}) .then(function() { return sutPage.executeExpression('1 3')})
.then(function() { .then(function() {
return assertExpressionResult(driver, [{ label: '1', bin:'01', other: '0x1'}, { label: '3', bin:'11', other: '0x3'}]) return assertExpressionResult(driver, [{ label: '1', bin:'01', other: '0x1'}, { label: '3', bin:'11', other: '0x3'}])
}); });
@@ -181,9 +175,8 @@ describe('interaction with results', function() {
// Given: 0x2a 00101010 42 // Given: 0x2a 00101010 42
// Expected: 0x6a 01101010 106 // Expected: 0x6a 01101010 106
goToApp() sutPage.goToApp()
.then(function() { return sendCommand('clear')}) .then(function() { return sutPage.executeExpression('0x2a')})
.then(function() { return sendCommand('0x2a')})
.then(function() { assertExpressionResult(driver, [{ label: "0x2a", bin: '00101010', other: '42' }]); }) .then(function() { assertExpressionResult(driver, [{ label: "0x2a", bin: '00101010', other: '42' }]); })
.then(function() { return flipBit(2); }) .then(function() { return flipBit(2); })
.then(function() { return assertExpressionResult(driver, [{ label: "0x6a", bin: '01101010', other: '106' }]); }) .then(function() { return assertExpressionResult(driver, [{ label: "0x6a", bin: '01101010', other: '106' }]); })
@@ -192,14 +185,6 @@ describe('interaction with results', function() {
}) })
}); });
function sendCommand(cmd) {
console.log('\r\nSend command: ' + cmd + "\r\n");
return driver.findElement(By.id('in')).then(function (el) {
return el.sendKeys(cmd + Key.ENTER);
});
}
function assertNoErrors() { function assertNoErrors() {
return driver.findElements(By.css('.result .error')).then(function(els) { return driver.findElements(By.css('.result .error')).then(function(els) {
expect(els.length).toBe(0, "There should be no errors"); expect(els.length).toBe(0, "There should be no errors");
@@ -263,31 +248,11 @@ function assertSingleRowResult(row, label, bin, other, sign) {
} }
function assertOperation(op, expected) { function assertOperation(op, expected) {
return goToApp().then(function() { return sutPage.executeExpression(op)
return sendCommand(op)
.then(assertNoErrors) .then(assertNoErrors)
.then(function() { .then(function() {
return assertExpressionResult(driver, expected) return assertExpressionResult(driver, expected)
}); });
})
}
function goToApp(hashValue) {
var url = appUrl;
var hash = hashValue || '-notrack';
if(hash.indexOf('-notrack') < 0) {
hash += "||-notrack";
}
if(url.indexOf("#") < 0) {
url += "#" + hash;
} else {
url += "||" + hash;
}
return driver.get(url);
} }
function flipBit(bitNumber) { function flipBit(bitNumber) {