JavaScript Unit Testing with Jellyfish and OnDemand
Sauce AI for Test Authoring: Move from design to execution in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from design to execution in minutes.
|
x
Web application test turn out to be very similar to an onion. You keep peeling back layers and finding more testing that can, and believably should, be done. But you shouldn & # x27; t only be refer with testing the smooth outer layer that your users will see. There & # x27; s still plenty of automated testing to be done on those inner layer, and it doesn & # x27; t command much extra employment. The testing I & # x27; m referring to is the JavaScript unit tests. For most development team, this is an incredibly important step and should be done well before using Selenium for testing web UI and exploiter flows.
Most people think about Selenium as the & quot; click & quot; here, & quot; type & quot; there project. And for many aim and purposes, that & # x27; s correct. However,Selenium 2is offering some new functionality that lends itself to a slightly bigger set of use example. What I & # x27; m talking about is the & # x27; execute & # x27; method. This let you to execute arbitrary JavaScript in the AUT (covering under test) scope, without get to deal with the frustrating complexities of & # x27; getEval & # x27; inSelenium 1. Specifically because of this functionality, I wrote a driver forJellyfish(jelly.io), a framework that allows you to write node .js scripts that run JavaScript in the browser. In order to do this, I apply a tiny subset of theWebDriver JSON wire protocol in node.jsand ring it & # x27;wd& # x27;. These project are concern all in their own right, but to get consecutive to the point:If you receive tests in FooUnit, QUnit or any other JavaScript screen framework that run in the browser, we now have a way for you to do all of that in the Sauce OnDemand cloud.
For this example, permit useQUnit. In the hope of covering all the bases, permit assume your QUnit exam are hosted on an internal web site to your company. Lets also assume that the way you currently run them is by open a browser window and going to & # x27; file open & # x27;, or navigating to a local URL and sit there view the tests run. When they are done, you & # x27; ll manually look through the tests and results to adjudicate if you interrupt things or not. With Jellyfish and OnDemand, the way this now works is that you have a node.js script that runs after every check-in (perhaps byJenkins) using different browser and program combinations. When the tests are accomplished, an email will be sent to all your developers each time you have a failure.
So how do we do this?Start by establish Jellyfish, the direction for which are front and center atjelly.io. Next we will construct a straightforward Jellyfish script that will start a Sauce Labs session, navigate to your QUnit examination URL, and assert the results when they finish. For my examples, I am going to be using the total & # x27; JQuery Test Suite & # x27; that I got from & # x27; jquery.com & # x27;. Lets talk about the contents of the handwriting one part at a time:
var jellyfish = require (& # x27; jellyfish & # x27;)
, assert = require (& # x27; assert & # x27;);
This imports the jellyfish bundle and the assert module, which is a skillful lightweight way to do asseveration in node.js.
SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.
var sauce = jellyfish.createSauce ();
sauce.opts.platform = & quot; VISTA & quot;;
sauce.opts.browserName = & quot; iexplorer & quot;;
sauce.opts.version = & quot; 9 & quot;;
This instantiate a new Jellyfish target and lets it know that this one will be talking to Sauce Labs. The username and password for your Sauce Labs account can be pass through here directly, but in this case, I & # x27; m store it in my ~/.jfrc file --, which is nice for simplicity. Next we & # x27; ll specify the platform, browser, and version we want to use to run our tryout (more information about .jfrccan be found here).
sauce.on (& # x27; effect & # x27;, function (res) {
console.log (sauce.name + & # x27;: & # x27; +sauce.tid + & # x27; - \x1b [33m % s\x1b [0m & # x27;, JSON.stringify (res));
});
This one is optional, but isn & # x27; t it nice to get well-formatted output in your logs?
sauce.on (& # x27; accomplished & # x27;, function (res) {
console.log (sauce.name + & # x27;: & # x27; +sauce.tid + & # x27; - \x1b [33m % s\x1b [0m & # x27;, JSON.stringify (res));
sauce.js (& quot; window.testOutput & quot;, mapping (o) {
try {
assert.equal (o.result.failed, 0);
sauce.stop (function () {
process.exit (0);
})
}
haul (e) {
console.log (e);
sauce.stop (function () {
process.exit (1);
})
}
})
});
This listener is rattling important since broadly our communication with Selenium on Sauce OnDemand is one directional. However, that has been pinch away. During the script, Jellyfish will canvass Selenium every few mo and ask & # x27; has window.jfComplete been set to true & # x27;? If so, an event will be triggered called & # x27; accomplished & # x27;. In the next section, you will see how the results are set, but at this point, I just have Jellyfish ask the page for the contents of & # x27; window.testOutput & # x27;. Next we swear the value of the figure of failures. In this case, I am catch that exception so that I can print it to the terminal and exit the handwriting with the correct return code for CI.
sauce.go (& quot;http: //myurl.com/jquery/test/index.html & quot;)
.js (& quot; QUnit.done = function (res) {
window.testOutput=res;
window.jfComplete=true;
}");
This is where the magic happens. This code navigates to the URL of the QUnit tests that start automatically, and as soon as we get navigated there, I redefine the QUnit perform event to set the window.jfComplete fleur-de-lis. Then I set the upshot yield of the test run in a place I can access it in the above codification. Usually the inaugural query inquire here is how to entree QUnit examine that are on an internal network. Fortunately, we have a production at Sauce Labs telephoneSauce Connectthat allows our cloud & # x27; s VMs to firmly accession whatever your test machines access on the internal mesh. It is incredibly simple to setup and you can do so by follow the. After that tunnel is running (you can only leave it escape), you can now update the start URL in & # x27; sauce.go & # x27; to reflect the URL you use internally to access your tests in the browser. Jenkins has overcharge for essentially any deployment program or germ control repository, and it also has post-deploy hooks that can be configured to send you an e-mail. You can also use either a & # x27; matrix projection & # x27; or surround variables to make one job run your test on all of our. The illustration book can bedownloaded herefrom github and I likewise made one of myJellyfish QUnit test runspublic for your viewing pleasure. Felicitous examination!
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free