From c9538c0dfb09e7841c291490ed582c5095dcbfad Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 7 Dec 2017 18:09:56 +0100 Subject: [PATCH] add test recorder --- src/app/tabs/run-tab.js | 4 +- src/universal-dapp.js | 4 +- test-browser/helpers/contracts.js | 56 +++++++++-- test-browser/tests/compiling.js | 4 +- test-browser/tests/units/testRecorder.js | 118 +++++++++++++++++++++++ 5 files changed, 174 insertions(+), 12 deletions(-) create mode 100644 test-browser/tests/units/testRecorder.js diff --git a/src/app/tabs/run-tab.js b/src/app/tabs/run-tab.js index 8067730d0e..cc377ee5c6 100644 --- a/src/app/tabs/run-tab.js +++ b/src/app/tabs/run-tab.js @@ -269,8 +269,8 @@ function makeRecorder (appAPI, appEvents) { width: 135px; } ` - var recordButton = yo`` - var runButton = yo`` + var recordButton = yo`` + var runButton = yo`` var el = yo`
${recordButton} diff --git a/src/universal-dapp.js b/src/universal-dapp.js index de5762a45b..f7da8f5174 100644 --- a/src/universal-dapp.js +++ b/src/universal-dapp.js @@ -290,10 +290,10 @@ UniversalDApp.prototype.renderInstanceFromABI = function (contractABI, address, function remove () { instance.remove() } - var instance = yo`
` + address = (address.slice(0, 2) === '0x' ? '' : '0x') + address.toString('hex') + var instance = yo`
` var context = executionContext.isVM() ? 'memory' : 'blockchain' - address = (address.slice(0, 2) === '0x' ? '' : '0x') + address.toString('hex') var shortAddress = helper.shortenAddress(address) var title = yo`
${contractName} at ${shortAddress} (${context})
diff --git a/test-browser/helpers/contracts.js b/test-browser/helpers/contracts.js index 45798bb2c4..e7a193c5eb 100644 --- a/test-browser/helpers/contracts.js +++ b/test-browser/helpers/contracts.js @@ -11,7 +11,9 @@ module.exports = { checkDebug, goToVMtraceStep, useFilter, - addInstance + addInstance, + clickFunction, + verifyCallReturnValue } function getCompiledContracts (browser, compiled, callback) { @@ -65,6 +67,46 @@ function testContracts (browser, fileName, contractCode, compiledContractNames, }) } +function clickFunction (fnFullName, expectedInput) { + this.waitForElementPresent('.instance button[title="' + fnFullName + '"]') + .perform(function (client, done) { + client.execute(function () { + document.querySelector('#optionViews').scrollTop = document.querySelector('#optionViews').scrollHeight + }, [], function () { + if (expectedInput) { + client.setValue('#runTabView input[title="' + expectedInput.types + '"]', expectedInput.values, function () {}) + } + done() + }) + }) + .click('.instance button[title="' + fnFullName + '"]') + .pause(500) + return this +} + +function verifyCallReturnValue (browser, address, checks, done) { + browser.execute(function (address, checks) { + var nodes = document.querySelectorAll('#instance' + address + ' div[class^="contractProperty"] div[class^="value"]') + var ret = {sucess: true} + for (var k in checks) { + var text = nodes[k].innerText ? nodes[k].innerText : nodes[k].textContent + text = text.replace('\n', '') + if (checks[k] !== text) { + ret.sucess = false + ret.expected = checks[k] + ret.got = text + return ret + } + } + return ret + }, [address, checks], function (result) { + if (!result.value.sucess) { + browser.assert.fail('verifyCallReturnValue failed', JSON.stringify(result.value), '') + } + done() + }) +} + function testFunction (fnFullName, txHash, log, expectedInput, expectedReturn, expectedEvent) { // this => browser this.waitForElementPresent('.instance button[title="' + fnFullName + '"]') @@ -101,13 +143,13 @@ function testFunction (fnFullName, txHash, log, expectedInput, expectedReturn, e function addInstance (browser, address, done) { browser.setValue('.ataddressinput', address, function () { browser.click('div[class^="atAddress"]') - .perform((client) => { - browser.execute(function () { - document.querySelector('#modal-footer-ok').click() - }, [], function (result) { - done() + .perform((client) => { + browser.execute(function () { + document.querySelector('#modal-footer-ok').click() + }, [], function (result) { + done() + }) }) - }) }) } diff --git a/test-browser/tests/compiling.js b/test-browser/tests/compiling.js index b9704b2c50..1b6df78165 100644 --- a/test-browser/tests/compiling.js +++ b/test-browser/tests/compiling.js @@ -3,6 +3,7 @@ var contractHelper = require('../helpers/contracts') var init = require('../helpers/init') var sauce = require('./sauce') var async = require('async') +var testRecorder = require('./units/testRecorder') module.exports = { before: function (browser, done) { @@ -19,12 +20,13 @@ module.exports = { function runTests (browser) { browser.testFunction = contractHelper.testFunction + browser.clickFunction = contractHelper.clickFunction browser .waitForElementVisible('.newFile', 10000) .click('.compileView') .perform(() => { // the first fn is used to pass browser to the other ones. - async.waterfall([function (callback) { callback(null, browser) }, testSimpleContract, testReturnValues, testInputValues], function () { + async.waterfall([function (callback) { callback(null, browser) }, testSimpleContract, testReturnValues, testInputValues, testRecorder], function () { browser.end() }) }) diff --git a/test-browser/tests/units/testRecorder.js b/test-browser/tests/units/testRecorder.js new file mode 100644 index 0000000000..188687b6b0 --- /dev/null +++ b/test-browser/tests/units/testRecorder.js @@ -0,0 +1,118 @@ +'use strict' +var contractHelper = require('../../helpers/contracts') + +module.exports = function (browser, callback) { + contractHelper.addFile(browser, 'scenario.json', {content: records}, () => { + browser + .click('.runView') + .click('#runTabView .runtransaction') + .clickFunction('getInt - call') + .clickFunction('getAddress - call') + .waitForElementPresent('div[class^="contractProperty"] div[class^="value"]') + .perform(() => { + contractHelper.verifyCallReturnValue(browser, '0xec5bee2dbb67da8757091ad3d9526ba3ed2e2137', ['0: uint256: 1', '0: address: 0xca35b7d915458ef540ade6068dfe2f44e8fa733c'], () => { callback() }) + }) + }) +} + + +var records = `{ + "accounts": { + "account{0}": "0xca35b7d915458ef540ade6068dfe2f44e8fa733c" + }, + "transactions": [ + { + "timestamp": 1512661974836, + "record": { + "value": "0", + "parameters": [ + 23 + ], + "abi": "0xe8e77626586f73b955364c7b4bbf0bb7f7685ebd40e852b164633a4acbd3244c", + "contractName": "test", + "bytecode": "6060604052341561000f57600080fd5b6040516020806102098339810160405280805190602001909190505080600081905550506101c7806100426000396000f300606060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632f30c6f61461005c57806338cc48311461009e57806362738998146100f3575b600080fd5b341561006757600080fd5b61009c600480803590602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061011c565b005b34156100a957600080fd5b6100b1610168565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156100fe57600080fd5b610106610192565b6040518082815260200191505060405180910390f35b8160008190555080600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050565b6000600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b600080549050905600a165627a7a7230582021de204df5493bae860efacfbdea0118cbb3d73760ad48ee0838d07d37fe3c9e0029", + "linkReferences": {}, + "name": "", + "type": "constructor", + "from": "account{0}" + } + }, + { + "timestamp": 1512661987446, + "record": { + "value": "0", + "parameters": [ + 1, + "0xca35b7d915458ef540ade6068dfe2f44e8fa733c" + ], + "to": "created-contract{1512661974836}", + "abi": "0xe8e77626586f73b955364c7b4bbf0bb7f7685ebd40e852b164633a4acbd3244c", + "name": "set", + "type": "function", + "from": "account{0}" + } + } + ], + "linkReferences": {}, + "abis": { + "0xe8e77626586f73b955364c7b4bbf0bb7f7685ebd40e852b164633a4acbd3244c": [ + { + "constant": true, + "inputs": [], + "name": "getInt", + "outputs": [ + { + "name": "", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": true, + "inputs": [], + "name": "getAddress", + "outputs": [ + { + "name": "", + "type": "address" + } + ], + "payable": false, + "stateMutability": "view", + "type": "function" + }, + { + "constant": false, + "inputs": [ + { + "name": "_t", + "type": "uint256" + }, + { + "name": "_add", + "type": "address" + } + ], + "name": "set", + "outputs": [], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "name": "_r", + "type": "uint256" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "constructor" + } + ] + } +}`