Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test interface option #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 87 additions & 24 deletions lib/Recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,73 @@
})();

var TEMPLATES = {
suiteOpen: [
'define(function (require) {',
' var tdd = require(\'intern!tdd\');',
' tdd.suite(\'recorder-generated suite\', function () {',
''
].join('\n'),
suiteClose: [
' });',
'});',
''
].join('\n'),
testOpen: [
' tdd.test(\'$NAME\', function () {',
' return this.remote'
].join('\n'),
testClose: [
';',
' });',
''
].join('\n')
object: {
suiteOpen: [
'define(function (require) {',
' var registerSuite = require(\'intern!object\');',
' registerSuite({',
' name: \'recorder-generated suite\',',
''
].join('\n'),
suiteClose: [
' });',
'});',
''
].join('\n'),
testOpen: [
' \'$NAME\': function () {',
' return this.remote'
].join('\n'),
testClose: [
';',
' }',
''
].join('\n')
},
tdd: {
suiteOpen: [
'define(function (require) {',
' var tdd = require(\'intern!tdd\');',
' tdd.suite(\'recorder-generated suite\', function () {',
''
].join('\n'),
suiteClose: [
' });',
'});',
''
].join('\n'),
testOpen: [
' tdd.test(\'$NAME\', function () {',
' return this.remote'
].join('\n'),
testClose: [
';',
' });',
''
].join('\n')
},
bdd: {
suiteOpen: [
'define(function (require) {',
' var bdd = require(\'intern!bdd\');',
' bdd.describe(\'recorder-generated suite\', function () {',
''
].join('\n'),
suiteClose: [
' });',
'});',
''
].join('\n'),
testOpen: [
' bdd.it(\'$NAME\', function () {',
' return this.remote'
].join('\n'),
testClose: [
';',
' });',
''
].join('\n')
}
};

function Recorder(chrome, storage) {
Expand All @@ -217,6 +264,7 @@
this.storage = storage;
this.hotkeys = JSON.parse(storage.getItem('intern.hotkeys')) || this._getDefaultHotkeys();
this.strategy = storage.getItem('intern.strategy') || 'xpath';
this.testInterface = storage.getItem('intern.interface') || 'tdd';
this.findDisplayed = storage.getItem('intern.findDisplayed') || false;

this._initializeScript();
Expand Down Expand Up @@ -259,6 +307,8 @@

strategy: null,

testInterface: null,

tabId: null,

clear: function () {
Expand Down Expand Up @@ -727,24 +777,26 @@
this._port.send('setScript', [ this._script ]);
this._port.send('setRecording', [ this.recording ]);
this._port.send('setStrategy', [ this.strategy ]);
this._port.send('setInterface', [ this.testInterface ]);

for (var hotkeyId in this.hotkeys) {
this._port.send('setHotkey', [ hotkeyId, this.hotkeys[hotkeyId] ]);
}
},

_renderScriptTree: function () {
var script = TEMPLATES.suiteOpen;
var template = TEMPLATES[this.testInterface];
var script = template.suiteOpen;

this._scriptTree.forEach(function (test) {
script += TEMPLATES.testOpen.replace('$NAME', test.name);
script += template.testOpen.replace('$NAME', test.name);
test.commands.forEach(function (command) {
script += command.text;
});
script += TEMPLATES.testClose;
script += template.testClose;
});

script += TEMPLATES.suiteClose;
script += template.suiteClose;
this.setScript(script);
},

Expand Down Expand Up @@ -788,6 +840,17 @@
this._contentPort.send('setStrategy', [ value ]);
},

setInterface: function (value) {
if (['object', 'tdd', 'bdd'].indexOf(value) < 0) {
throw new Error('Invalid test interface "' + value + '"');
}

this.storage.setItem('intern.interface', value);
this.testInterface = value;
this._port.send('setInteface', [ value ]);
this._renderScriptTree();
},

setTabId: function (tabId) {
if (tabId && this.tabId !== tabId) {
this.tabId = tabId;
Expand Down
17 changes: 17 additions & 0 deletions lib/RecorderProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@
input.onchange = function (event) {
self.send('setFindDisplayed', [ event.target.checked ]);
};

input = document.getElementById('option-interface');

/* istanbul ignore if: the recorder is broken if this ever happens */
if (!input) {
throw new Error('Panel is missing input for option "interface"');
}

input.onchange = function (event) {
self.send('setInterface', [ event.target.value ]);
};
},

_initializePort: function () {
Expand Down Expand Up @@ -313,6 +324,12 @@
}
},

setInterface: function (value) {
if (this.contentWindow) {
this.contentWindow.document.getElementById('option-interface').value = value;
}
},

_show: function (contentWindow) {
if (this.contentWindow !== contentWindow) {
this.contentWindow = contentWindow;
Expand Down
9 changes: 9 additions & 0 deletions lib/panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@
</select>
</div>

<div class="option">
<label class="option-label" for="option-interface">Test Interface</label>
<select class="option-input" id="option-interface">
<option value="object">Object</option>
<option value="tdd">TDD</option>
<option value="bdd">BDD</option>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want to also add the new qunit interface?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered it, but I haven't actually used it, and I wasn't sure if the this.remote convention worked within the qunit runner. If it operates the same as the others I'll add it to this PR if you'd like.

</select>
</div>

<div class="option">
<input type="checkbox" id="option-findDisplayed">
<label for="option-findDisplayed">
Expand Down
9 changes: 9 additions & 0 deletions tests/data/output/bdd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(function (require) {
var bdd = require('intern!bdd');
bdd.describe('recorder-generated suite', function () {
bdd.it('Test 1', function () {
return this.remote
.get('http://example.com');
});
});
});
10 changes: 10 additions & 0 deletions tests/data/output/object.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
define(function (require) {
var registerSuite = require('intern!object');
registerSuite({
name: 'recorder-generated suite',
'Test 1': function () {
return this.remote
.get('http://example.com');
}
});
});
9 changes: 9 additions & 0 deletions tests/data/output/tdd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(function (require) {
var tdd = require('intern!tdd');
tdd.suite('recorder-generated suite', function () {
tdd.test('Test 1', function () {
return this.remote
.get('http://example.com');
});
});
});
30 changes: 29 additions & 1 deletion tests/unit/Recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ define(function (require) {
mouseMove: require('dojo/text!../data/output/mouseMove.txt'),
navigation: require('dojo/text!../data/output/navigation.txt'),
newTest: require('dojo/text!../data/output/newTest.txt'),
type: require('dojo/text!../data/output/type.txt')
type: require('dojo/text!../data/output/type.txt'),
object: require('dojo/text!../data/output/object.txt'),
bdd: require('dojo/text!../data/output/bdd.txt'),
tdd: require('dojo/text!../data/output/tdd.txt')
};

function assertScriptValue(port, value, assertMessage) {
Expand Down Expand Up @@ -191,6 +194,7 @@ define(function (require) {
expected.push([ { method: 'setRecording', args: [ false ] } ]);
expected.push([ { method: 'setStrategy', args: [ 'xpath' ] } ]);
expected.push([ { method: 'setFindDisplayed', args: [ false ] } ]);
expected.push([ { method: 'setInterface', args: [ 'tdd' ] } ]);

assert.sameDeepMembers(
actual,
Expand Down Expand Up @@ -701,6 +705,30 @@ define(function (require) {
}, 'Invalid search strategy');
},

'#setInterface': function () {
devToolsPort.postMessage.clear();

var eventProxyPort = mockChromeApi.createPort('eventProxy');
chrome.runtime.onConnect.emit(eventProxyPort);
eventProxyPort.postMessage.clear();

recorder.setTabId(1);
recorder.toggleState();
recorder.setInterface('bdd');
assert.lengthOf(devToolsPort.postMessage.calls, 5);
assertScriptValue(devToolsPort, testData.bdd, 'Script should use "bdd" interface');

recorder.setInterface('tdd');
assertScriptValue(devToolsPort, testData.tdd, 'Script should use "tdd" interface');

recorder.setInterface('object');
assertScriptValue(devToolsPort, testData.object, 'Script should use "object" interface');

assert.throws(function () {
recorder.setInterface('invalid');
}, 'Invalid test interface');
},

'#setTabId': function () {
assert.isNull(recorder.tabId);
recorder.setTabId(1);
Expand Down