Question

I am currently trying to test some javascript code that is embedded in an html file. I do understand that it would be easier to test if it was external. That is not an option for me. Also I am currently using JsTestDriver as my framework. Other frameworks are not an option.
Any ideas on how to test the functions that are embedded?
Some info: I have yet to discover a way that JsTestDriver facilitates this, ive looked, but if you know a way id be interested. Ive spent some extensive time on this issue.

Was it helpful?

Solution

--This isnt the whole answer, but its the hard part, just get to know JsTestDriver and this will make sense, this needs to be a function inside the test file where you have the test functions for the embedded JS--

var url = "/test/<path to your html file>";
var request = new XMLHttpRequest();
request.open('GET', url, false);
request.send(null);

var doc = document.implementation.createHTMLDocument("SomeTitle");
doc.documentElement.innerHTML = request.responseText;

var allScripts = doc.scripts;
var script = allScripts.item(#);//the # is the specific script you want, look up .item()

var scriptline = script.innerHTML.replace(/\s/g, ' ');//everything needs to be in one line

var script2 = document.createElement('script');

script2.innerHTML = sample;

var h = document.getElementsByTagName('head')[0];

h.appendChild(script2);

YOUR ALL WELCOME!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top