Created
July 1, 2015 23:04
-
-
Save swashcap/e9884b5b260dce8030c9 to your computer and use it in GitHub Desktop.
Query Builder test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Test Query builder. | |
*/ | |
'use strict'; | |
var _ = require('lodash'); | |
var config = require('config'); | |
var client = require('./lib/client').client; | |
var nav = require('./lib/nav/navigation')(client, config); | |
var micis = require('./lib/auth/micis')(client); | |
var should = require('should'); | |
var sampleStudyId = '(MRN) VCALHOUN: [99-998] NITEST'; | |
var sampleUrsis = ['M87120826', 'M87128533', 'M87100451', 'M87159685', | |
'M87181629', 'M53742922', 'M53790706', 'M53771006', 'M53739226', | |
'M53749098', 'M53708287', 'M53711867', 'M87168375']; | |
/** | |
* Set up a query. | |
* | |
* This manages fields in the "Select subjects" form section. | |
* | |
* @param {function} callback | |
* @param {object} options | |
* @return {object} | |
*/ | |
var setupQuery = function (options, callback) { | |
var defaults = { | |
studyId: sampleStudyId, | |
subjectTagType: '', | |
subjectTags: [], | |
subjectType: '', | |
ursis: [] | |
}; | |
/** | |
* Sometimes the `options` argument is the callback. | |
* | |
* @todo Figure out a better lodash-y way to handle. | |
*/ | |
if (options instanceof Function) { | |
callback = options; | |
options = {}; | |
} else if (!(callback instanceof Function)) { | |
callback = _.noop; | |
} | |
options = _.extend(defaults, options); | |
if ( | |
options.ursis.length || | |
(options.subjectTags.length && options.subjectTagType) | |
) { | |
/** | |
* Click "Subjects appear in list" checkbox. This results in 2 | |
* sub-options: direct URSI entry or "Subject Tags" selection. | |
*/ | |
client | |
.scroll(0, 0) | |
.click('#optListOfSubjects') | |
.waitForVis('#ursiListDiv'); | |
if (options.ursis.length) { | |
/** Option 1: direct URSI entry */ | |
client | |
.click('#subject_list_type_ursi') | |
.setValue('#subjectListInput', options.ursis.join(' ')); | |
} else { | |
/** Option 2: subject tags */ | |
client | |
.click('#subject_list_type_tag') | |
.waitForVisible('#subject_list_tag_details') | |
.selectByVisibleText( | |
'#subject_list_tag_study_id', | |
options.studyId | |
) | |
.selectByVisibleText( | |
'#subject_list_tag_id', | |
options.subjectTagType | |
) | |
.setValue('#subjectListInput', options.subjectTags.join(' ')); | |
} | |
/** Click out of the text field to trigger the URSI query */ | |
client.click('#ursiListDiv fieldset'); | |
} else if (options.subjectType) { | |
/** | |
* Click "Subjects by subject type", select a study and subject type. | |
*/ | |
client | |
.click('#subjectTypeCheckbox') | |
.selectByVisibleText( | |
'#subjectTypeStudySelectBox', | |
options.studyId | |
) | |
.waitForVis('#selectTypeDiv') | |
.selectByVisibleText( | |
'#subjectTypeSelectBox', | |
options.subjectType | |
); | |
} else { | |
/** | |
* Click "Subjects are enrolled in a study" and enroll in | |
*/ | |
client | |
.click('#optAllSubjectsInStudy') | |
.selectByVisibleText( | |
'#studySelectBox', | |
options.studyId | |
); | |
} | |
return client | |
.waitForPaginationComplete() | |
.waitForText('#windowPreview') | |
.getText('#windowPreview', function (err, res) { | |
if (err) { | |
throw err; | |
} | |
/** | |
* Make sure there's at least one URSI in the "Preview of | |
* selected" pane. | |
*/ | |
should(res).match(/^M\d+/g); | |
}) | |
.call(callback); | |
}; | |
/** | |
* Add field(s) to query data. | |
* | |
* @param {object} options | |
* @return {undefined} | |
*/ | |
var addFieldsToQuery = function (options) { | |
/** Test to ensure value is in table */ | |
var testTableRow = function (err, res) { | |
if (err) { | |
throw err; | |
} | |
should(res).match(new RegExp(options.values[i])); | |
}; | |
for (var i = 0, il = options.values.length; i < il; i++) { | |
client | |
.selectByVisibleText(options.select, options.values[i]) | |
.click(options.button) | |
/** | |
* The first `tr` is always used as a heading. Start on the next. | |
*/ | |
.waitForExist(options.table + ' tr:nth-of-type(' + (i + 2) + ')') | |
.getText( | |
options.table + ' tr:nth-of-type(' + (i + 2) + ')', | |
testTableRow | |
); | |
} | |
}; | |
/** | |
* Set up demographic data in the "Data Criteria" form section. | |
*/ | |
var setupDemograpicData = function (callback) { | |
callback = callback instanceof Function ? callback : _.noop; | |
return client | |
.moveToObject('#optDemoDataOutput') | |
.click('#optDemoDataOutput') | |
.waitForVis('#demoDataDiv') | |
.selectByVisibleText( | |
'select[name="selDemoStudy"]', | |
sampleStudyId, | |
function (err) { | |
if (err) { | |
throw err; | |
} | |
addFieldsToQuery({ | |
select: 'select[name="selDemoField"]', | |
button: '#btnDemoAddList', | |
table: '#outputDemoRows', | |
values: ['Gender', 'Subject Type'] | |
}); | |
} | |
) | |
.call(callback); | |
}; | |
var setupAssessmentData = function (callback) { | |
callback = callback instanceof Function ? callback : _.noop; | |
return client | |
.moveToObject('#optAsmtDataOutput') | |
.click('#optAsmtDataOutput') | |
.waitForVis('#asmtDataDiv') | |
.selectByVisibleText( | |
'#asmtDataDiv select[name=selStudy]', | |
sampleStudyId | |
) | |
.waitForExist( | |
'#asmtDataDiv select[name=selProtocol] option[value="simple test"]', | |
1500 | |
) | |
.selectByVisibleText( | |
'#asmtDataDiv select[name=selInstrument]', | |
'simple test' | |
) | |
.waitForExist( | |
'select[name=selField] option:nth-child(2)', | |
1500, | |
function (err) { | |
if (err) { | |
throw err; | |
} | |
addFieldsToQuery({ | |
select: 'select[name=selField]', | |
button: '#btnAddList', | |
values: ['All Fields'], | |
table: '#outputRows' | |
}); | |
} | |
) | |
.call(callback); | |
}; | |
var setupScanData = function (callback) { | |
callback = callback instanceof Function ? callback : _.noop; | |
return client | |
.moveToObject('#optScanDataOutput') | |
.click('#optScanDataOutput') | |
.waitForVis('#scanDataDiv') | |
.selectByVisibleText( | |
'select[name=selScanStudy]', | |
'(MRN) JBUSTILLO: [08-049] FIRST' | |
) | |
/** Wait for the `select` to be populated with the study's protocols. */ | |
.waitForExist( | |
'select[name=selProtocol] option[value=mprage_5e]', | |
1500, | |
function (err) { | |
if (err) { | |
throw err; | |
} | |
addFieldsToQuery({ | |
select: 'select[name=selProtocol]', | |
button: '#btnDataAddProtList', | |
values: ['mprage_5e', 'mprage_5e_rms'], | |
table: '#queryOutputProtRows' | |
}); | |
} | |
) | |
.call(callback); | |
}; | |
/** | |
* Preview and export. | |
*/ | |
var previewAndExport = function (options, callback) { | |
if (_.isFunction(options)) { | |
callback = options; | |
options = {}; | |
} else if (!_.isFunction(callback)) { | |
callback = _.noop; | |
} | |
var defaults = { | |
delimiter: 'comma', | |
isAssessment: false, | |
isLegacy: false | |
}; | |
var delimiterSelector; | |
var exportButtonSelector; | |
options = _.extend(defaults, options); | |
client | |
.click('#btnPreview') | |
/** | |
* Hi. | |
* | |
* There's some twisted DOM state logic that prevents the page from | |
* being used while running Selenium tests. The modal UI, enclosed in | |
* `div.tmask` and `div.tbox` appears to be the culprit. Removing them | |
* entirely completely fixes the problem. | |
*/ | |
.executeAsync(function (done) { | |
[].forEach.call(window.document.querySelectorAll('.tmask, .tbox'), function (node) { | |
// console.log(node); | |
node.parentElement.removeChild(node); | |
}); | |
done(); | |
}) | |
.waitForText('#resultsarea', 10000) | |
.getText('#step3 .frmHeaderText', function (err, res) { | |
if (err) { | |
throw err; | |
} else if (res.trim() === 'Preview Finished') { | |
/** It's assessment data. Results are output | |
* differently, check appropriately. | |
*/ | |
client.getText('#resultsarea', function (err, res) { | |
if (err) { | |
throw err; | |
} | |
should(res).match(/Number of Assessment Records Found: \d+/); | |
}); | |
} else { | |
/** | |
* It's regular data. Check for matches in `.frmHeaderText` and | |
* the table. | |
*/ | |
should(res).match(/Results: \d+/); | |
/** | |
* Confirm the results area's rows has content. The first row | |
* serves as table headers, so results should be > 2. | |
*/ | |
client.elements('#resultsarea tr', function (err, res) { | |
if (err) { | |
throw err; | |
} | |
should(res.value.length).be.above(1); | |
}); | |
} | |
}) | |
.click('input[name=btnExport]') | |
.scroll(0, 0); | |
if (options.isLegacy) { | |
client | |
.click('#safeExportButtons input[value*="Legacy Export"]') | |
.waitForVis('#delimiters') | |
.scroll(0, 0); | |
} | |
exportButtonSelector = (options.isAssessment && !options.isLegacy) ? | |
'#safeExportDashboard input[type=button]' : | |
'#frmDnlLink input[name=btnExport]'; | |
delimiterSelector = (options.isAssessment && !options.isLegacy) ? | |
'#safeDelimiters' : | |
'#delimiters'; | |
delimiterSelector += options.delimiter === 'tab' ? | |
' input[value*="\\t"]' : | |
' input[value=","]'; | |
return client | |
.click(delimiterSelector) | |
.click(exportButtonSelector) | |
.call(callback); | |
}; | |
var goBack = function (callback) { | |
callback = _.isFunction(callback) ? callback : _.noop; | |
return nav.goToQueryBuilder(callback); | |
}; | |
describe('Query Builder', function () { | |
this.timeout(config.defaultTimeout); | |
before('initialize', function (done) { | |
client.clientReady.then(function boot() { | |
if (!micis.loggedOn) { | |
micis.logon(); | |
} | |
nav.goToQueryBuilder(done); | |
}); | |
}); | |
/** | |
* Test output of demographic data. | |
* | |
* Subject selection via: | |
* | |
* * Direct URSIs | |
* * A subject tag | |
* | |
* For each subject selection, sample demographic data with: | |
* | |
* * "Gender" label | |
* * "Subject Type" label | |
*/ | |
describe('Demographic Data', function () { | |
/** | |
* Direct URSIs | |
*/ | |
describe('export via direct URSIs', function () { | |
it('should set up', _.partialRight(setupQuery, { | |
ursis: sampleUrsis | |
})); | |
it('should setup demo data', setupDemograpicData); | |
it('should preview and export', previewAndExport); | |
}); | |
/** | |
* By subject tag | |
*/ | |
describe('export via subject tag', function () { | |
before(goBack); | |
it('should set up', _.partial(setupQuery, { | |
subjectTagType: 'Temporary Subject ID', | |
subjectTags: ['6001'] | |
})); | |
it('should setup demographic data', setupDemograpicData); | |
it('should preview and export', previewAndExport); | |
}); | |
}); | |
/** | |
* Test output of assessment data. | |
* | |
* Steps: | |
* | |
* 1. Choose "NITEST" (the helper function's default) for subject | |
* selection. | |
* 2. Choose "NITEST" for the particular assessment data | |
* 3. Leave assessment defaults (double entry, no date filters, all | |
* instruments and all visits) | |
* 4. Select "All Fields" under "Fields" dropdown | |
* | |
* Two output modes must be tested: | |
* | |
* * Select comma, leave other stuff. Click “Begin export” | |
* * Click legacy export, reselect comma, Click Export | |
*/ | |
describe('Assessment Data', function () { | |
/** | |
* Standard export | |
*/ | |
describe('export via standard export', function () { | |
before(goBack); | |
it('should set up', setupQuery); | |
it('should setup assessment data', setupAssessmentData); | |
it('should preview and export', function (done) { | |
previewAndExport({isAssessment: true}).pause(15000).call(done); | |
}); | |
}); | |
/** | |
* Legacy export | |
*/ | |
describe('export via legacy export', function () { | |
before(goBack); | |
it('should set up', setupQuery); | |
it('should setup assessment data', setupAssessmentData); | |
it('should preview and export', _.partialRight(previewAndExport, { | |
delimiter: 'tab', | |
isAssessment: true, | |
isLegacy: true | |
})); | |
}); | |
}); | |
/** | |
* Test output of scan data. | |
*/ | |
describe('Scan Data', function () { | |
before(goBack); | |
after(goBack); | |
/** | |
* Use a different subject selection: | |
* | |
* 1. Choose "Subjects By Type:" | |
* 2. Choose "JBUSTILLO's 'First'" study | |
* 3. Choose "First Episode Patient" subject type | |
*/ | |
it('should set up', _.partialRight(setupQuery, { | |
studyId: '(MRN) JBUSTILLO: [08-049] FIRST', | |
subjectType: 'First Episode Patient' | |
})); | |
it('should setup scan data', setupScanData); | |
it('should preview and export', previewAndExport); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment