Skip to content

Instantly share code, notes, and snippets.

@NigelThorne
Last active December 17, 2015 19:29
Show Gist options
  • Select an option

  • Save NigelThorne/5661205 to your computer and use it in GitHub Desktop.

Select an option

Save NigelThorne/5661205 to your computer and use it in GitHub Desktop.
POC for generating tests from template files.
<html>
<body>
<table width='100%'>
<tr>
<td>Select a TestTemplate File to Load:</td>
<td><input type="file" id="fileToLoad">
<td><a href="javascript:loadFileAsText()">Load Selected File</a><td>
</tr>
<tr>
<td colspan="3">
<textarea id="inputTemplate" style="width:100%;height:256px">
| Begin | ...| test name [[index]]
| Something | or | Other |
| Click | {{protocol_screen}}/buttonX | | |
| Enabled? | {{protocol_screen}}/buttonY | | |
| Enabled? | {{error_dialog}}/buttonY | | |
| Contains? | {{error_dialog}}/ErrorMessage | [[protocol]] | [[isVisible]] |
{{> login_as_user }}
</textarea>
</td>
</tr>
<tr>
<td>Select a Alias File to Load:</td>
<td><input type="file" id="aliasFileToLoad">
<td><a href="javascript:loadAliasFileAsText()">Load Selected File</a><td>
</tr>
<tr>
<td colspan="3">
<textarea id="inputAliases" style="width:100%;height:256px">
var aliases = {
protocol_screen: "this/That/TheOther/(someThing)/etc./etc/etc/",
error_dialog: "Some/other/long/path/that/you/want/to/avoid/typing/etc"
};
var partials = {
login_as_user: "line1\n\
line2 [[name]]\n\
line3\n\
"
};
var examples = [
{
protocol: "ISH",
isVisible: "true",
name: "Some Name"
},
{
protocol: "IHC",
isVisible: "false",
name: "Some Name"
}
];
</textarea>
</td>
</tr>
<tr>
<td></td>
<td><a href="javascript:applyAliases()">Preview Tests</a></td>
</tr>
<tr>
<td colspan="3">
<textarea id="outputResults" style="width:100%;height:256px"></textarea>
</td>
</tr>
<tr>
<td>Filename Save As:</td>
<td><input id="inputFileNameToSaveAs"></input></td>
<td><a href="javascript:saveTextAsFile()">Save Results to File</a></td>
</tr>
</table>
<script src="http://coenraets.org/tutorials/mustache/js/jquery-1.7.1.min.js"></script>
<script src="http://coenraets.org/tutorials/mustache/js/mustache.js"></script>
<script type='text/javascript'>
function loadFileAsText()
{
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("inputTemplate").value = textFromFileLoaded;
};
fileReader.readAsText(fileToLoad, "UTF-8");
}
function loadAliasFileAsText()
{
var aliasFileToLoad = document.getElementById("aliasFileToLoad").files[0];
var fileReader = new FileReader();
fileReader.onload = function(fileLoadedEvent)
{
var textFromFileLoaded = fileLoadedEvent.target.result;
document.getElementById("inputAliases").value = textFromFileLoaded;
};
fileReader.readAsText(aliasFileToLoad, "UTF-8");
}
function applyAliases()
{
document.getElementById("outputResults").value = calculateExamples().join("\n")
}
function calculateExamples()
{
eval(document.getElementById("inputAliases").value.trim());
var template = Mustache.to_html(document.getElementById("inputTemplate").value, aliases, partials);
template = template.replace(/\[\[/g, "{{").replace(/\]\]/g, "}}");
document.getElementById("outputResults").value = "";
return $.map(examples,function(example, index){
example.index = index;
return Mustache.to_html(template, example);
});
}
function saveTextAsFile()
{
var fileNameToSaveAs = document.getElementById("inputFileNameToSaveAs").value;
$.each(calculateExamples(), function(index, value){
saveToFile(value, fileNameToSaveAs + "_"+ index);
});
}
function saveToFile(textToWrite, fileNameToSaveAs)
{
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var downloadLink = document.createElement("a");
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
downloadLink.download = fileNameToSaveAs;
downloadLink.click();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment