Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

Wednesday, November 9, 2016

QUnit Parameterize plugin

I got my hands on QUnit tool for automatic unit testing and found that some of the plugins for it were obsolete. One example is QUnit Parameterize plugin, it was made for QUnit below 2.x versions. I reworked it using Aptana Studio with JSlint enabled and created my own solution: Parameterize Pull request
I also created SO discussion page which I also answered once I had solution here: http://stackoverflow.com/questions/39017452/cannot-use-parametrize-plugin-for-qunit/39103346#39103346

This is how a QUnit Parameterize test should look like, utilizing QUnit.module(moduleName, hooks) function where hooks object contains before(), beforeEach(), afterEach() and after() functions to cover test atomicity:



If you scroll at the middle part after the module() function you will see a statement qunit.cases.init([/* array of tests parameters */]).test("test name", function (params, assert) {/* your test code goes here */});

"cases" subobject of QUnit is created by Parameterize plugin and I made it to use init() function to set the array of parametric tests.

Params variable sent to the test function contains the parameters sent with each object of the array inside init([/* your test parameters here */]).

You can also add expected number of assertions in your test, and QUnit will check if the asked number of assertions are run. The test will be failure if not.

Also another useful feature, if you see, the matrices defined inside beforeEach() are added to the "this" object. This means the matrices are available inside the test callback function this "this" keyword, as I use it: this[params.matrixStubName]

Another useful thing I do is, I added the "t" parameter for each test containing the order number of the test, so when a test fails you can easiliy search it in your code.

Parameterize contains also sequential and combinatorial parametric tests. I however didn't have a need for those. Combinatorial can create combinations of parametric tests from two sets, and as for sequential, I think my test here in the example is already sequential test.

You may be asking how can I automate build process using  QUnit with Jenkins CI?

Well I used Karma JS test runner and it has plugins for QUnit and for Jenkins CI.
I was working on a project and created two configuration files for Karma JS, one for Jenkins and one for local test execution.

karma.conf.local.js
karma.conf.js

If you have a problem with a Jenkins build in hanging state but everything else seems good then you need to set "singleRun" to true in karma.conf.js. This will execute the tests once and it will stop the karma test runner. If it is set to false the build will hang forever :)))

You should configure Jenkins with Karma as in this guide.


Jenkins Build
Jenkins Build

Jenkins source code management
Jenkins source code management

Build environment
Build environment

No need to tell Jenkins to upload the node_modules folder, just install Node JS plugin for Jenkins and then in your build add the command "npm install". This will automaticall install all Node JS dependencies.

Also you may want the "karma start" shell command to trigger running of your unit tests.

Jenkins shell commands
Jenkins shell commands

Karma will export the tests using the JUnit reporter plugin and a post build action is required to export the tests in xml format. I had folder called test-reports and set post build action to "test-reports/**/*.xml"

QUnit Test reports in Jenkins
QUnit Test reports in Jenkins

Monday, June 29, 2015

"Socket.IO Real-time Web Application Development" - book review

In this post I will share my thoughts on a book called "Socket.IO Real-time Web Application Development" written by Rohit Rai, published in 2013.
It has 6 chapters and 2 appendices in around 120 pages. This book is made for developers who want to start developing highly interactive and real-time web applications or multiplayer web based video games. The book starts with brief introduction to real-time server side of web applications. How it was and how it is now.
It then continues with building a simple chat application using NodeJS, ExpressJS, JQuery, Socket.IO and a templating engine called Jade. It is expected that the reader understands well JavaScript. Also NodeJS knowledge would be a plus but it is not necesarry as all needed aspects are well explained. 

Chapters:
  1. Going Real-Time on the Web. Covers history of XMLHTTPRequest, AJAX, FlashSocket, etc... and of course introduction to real-time web of today.
  2. Getting Started with Node.js. Covers brief introduction to Node.js programming. How to install it, how to import npm packages, and how to create a simple "hello world" application.
  3. Let's Chat. Explains how to start with Socket.IO on the server side but also on the client side. Some basic information what is what on the client/server and how it works.
  4.  Making it More Fun. This chapter is the longest one and I would recommend passing it in 2 or 3 parts with breaks between. It adds nicknames to chat users and "create rooms" functionality.
  5. The Socket.IO Protocol. Advantages od Socket.IO over WebSockets API. Socket.IO is much more capable and complex, therefore it needs dedicated protocol to function. It also covers all protocol types of messages that exist.
  6. Deploying and Scaling. Covers some information on how to keep your app on Node.js up and running and how to monitor background processes with Monit and Forever. Scaling with HAProxyNode Cluster and Redis.
  7. Appendix A: Socket.IO quick reference.
  8. Socket.IO Backends. Covers brief information about Socket.Io backends for Erlang, Google Go, Java, Perl and Python.
 The problems I faced while developing the sample chat application were:
  1. The book is made for Socket.IO version 0.9 which is discontinued and no longer available for download over npm. So I downloaded version 1.3.5.
  2. The protocol API is a bit different from version 0.9 to 1.3.5 so if you get deep into Socket.IO structure you'll be a bit confused.
  3. The web documentation is kind of poor so you will need to search the web for information how to get valuable data from the Socket.IO Server object.
My thoughts on the book, it is well made for what it was meant for, but the problem with these JS libraries is that they change very fast, so this book is getting old already. On the other hand it gives a good example to practice with newer versions of Socket.IO so therefore I recommend it for new Socket.IO developers.
There were some inconsistencies but all in all it serves its function. I give 4.2/5.

I uploaded the demo chat application on this location:
Its not finished, because I have other things to do at the moment, but it is good start. I used https://www.openshift.com/ with Node.js 0.10 with preinstalled HAProxy load balancer. 
If it is unavailable the service sets the app to idle state. So remind me or write an email if it stopped, to restart it.

Notes:
  •  Socket.IO creates a room for each tab you have on every browser. So if you have two tabs on Chrome and three tabs in Firefox connected to Socket.IO server, there will be 5 rooms generated for you inside the server state. 
  •  Socket.IO creates unique id for each socket connected to the server and that same id will be for a pregenerated room for the socket. You can create your room with custom name, and the socket you are using will be in two rooms, yours and the pregenerated one.
  •  Be careful how you connect to namespaces because the internal server state might get mess and you cannot get information properly. 
  • Socket.IO is easy to code. The API is same on both server and client.It works siemlesly though it is over HTTP layer.
Useful links I found on the web about Socket.IO:

https://github.com/SocketCluster/socketcluster
https://en.wikipedia.org/wiki/WebSocket
https://forums.openshift.com/keep-getting-program-node-serverjs-exited-with-code-8-and-503-service-unavailable
http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/
https://www.npmjs.com/package/roomdata
http://fzysqr.com/2011/02/28/nodechat-js-using-node-js-backbone-js-socket-io-and-redis-to-make-a-real-time-chat-app/
http://howtonode.org/websockets-socketio
http://coenraets.org/blog/2012/10/real-time-web-analytics-with-node-js-and-socket-io/
http://ejosh.co/de/2015/01/node-js-socket-io-and-redis-intermediate-tutorial-server-side/
https://en.wikipedia.org/wiki/Appserver.io

Wednesday, August 31, 2011

JQuery basics

In this post I will explain the basics of JQuery, what it is and what it is used for.

JQuery is cross browser javascript library that is easy to learn and easy to use. It has two versions one is for production which is compressed (about 31KB) and other which is not compressed (about 230KB) and is for development. So when developing or learning you should use the development version and when you are publishing some website use the production version.
JQuery is written by several IT proffesionals whose main mission is to make JQuery the best javascript library for web.

You might wanna visit JQuery .
There are several other good javascript libraries like prototype, scriptaculous, Google Web Toolkint, and many others list of js libs. What makes JQuery so popular is that it is cross-browser, CSS3 compliant, easy to learn and to use. It has a wide variety of tools built on it like widgets, interactions, utilities, special effects.
For example implementing simple accordion with text data is no more than writing div tags and importing js files in your html.

I will be writing some tutorials on using JQuery with AJAX, implementing widgets like accordion, slider, progressbar etc. so make sure you have Firefox with firebug installed or maybe Chrome. About IE I haven't used it much so maybe I will make some tut on it in future.