Wednesday, 23 September 2015

Software Testing - Part 1

Software Testing Fundamentals

Testing is the process of examining an application to ensure it fulfills the requirements for which it was designed and meets quality expectations.
Testing measures the quality of an application or project.
Developers should take the view that your project does have bugs or defects that have not yet been discovered. Testing helps find and correct those defects.
A bug is an error in coding or logic that causes a program to malfunction or to produce incorrect results.


Importance of Software Testing
  • Reduces the cost of developing the program
  • Ensures that your application behaves exactly as intended
  • Reduces the total cost of ownership for end users
  • Develops customer loyalty and word-of-mouth market share



Testing benefits for End Users

Early testing results in software with better usability and reliability, as well as a lower cost of ownership.
  • Bugs caught during testing do not require users to spend time identifying bugs
  • Bugs caught before a project is delivered do not cost the user any downtime while fixes are created and updates are installed
  • Software that behaves as expected requires less training and user support
  • Software that is well-tested results in increased user satisfaction



Reference:


Thursday, 10 September 2015

Another Knockout example and a lesson learnt

Setup the project to use Knockout in a Visual Studio project, see: Setting up a Visual Studio Project for use with Knockout


1. Add the following Knockout code in the <script> tag in the header
<script>
/// <reference path="/Scripts/knockout-3.3.0.js"/>
   function AppViewModel() {
       this.firstName = ko.observable();
       this.lastName = ko.observable();
       this.msg = ko.observable();
           
       this.writeMsg = function () {
           var p1 = this.firstName() + " " + this.lastName();
           this.msg("Hello " + p1);
       };
   }
   // Activates knockout.js
   ko.applyBindings(new AppViewModel());
</script>


2. Add the following html code in the body of the document
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<button data-bind="click: writeMsg">Show</button>
<p data-bind="text: msg"></p>


3. Save your document and right-click the background of the document and select View in Browser
4. You are getting an error:”Error: Unable to get property ‘nodeType’ of undefined or null reference” - a lesson learnt
5. The reason is that the Knockout code is trying to execute before the objects in the document has been initialised
6. To fix this, move the script block with the Knockout code to the bottom of the document, just before the closing </body> tag
7. Run the page again and it should work as expected
8. See the complete code below:


<!DOCTYPE html>
<html>
<head>
   <title>Knockout Example</title>
<meta charset="utf-8" />
   <script src="Scripts/knockout-3.3.0.js"></script>
</head>
<body>
   <p>First name: <input data-bind="value: firstName" /></p>
   <p>Last name: <input data-bind="value: lastName" /></p>
   <button data-bind="click: writeMsg">Show</button>
   <p data-bind="text: msg"></p>


   <script>
   /// <reference path="/Scripts/knockout-3.3.0.js"/>
       function AppViewModel() {
           this.firstName = ko.observable();
           this.lastName = ko.observable();
           this.msg = ko.observable();
           this.writeMsg = function () {
               var p1 = this.firstName() + " " + this.lastName();
               this.msg("Hello " + p1);
           };
       }
       // Activates knockout.js
       ko.applyBindings(new AppViewModel());
   </script>
</body>

</html>

Basic usage of Knockoutjs in a website

Setup the project to use Knockout in a Visual Studio project, see: Setting up a Visual Studio Project for use with Knockout


1. In this example, the styles and the javascript were declared inline in the document
2. Add the styles in the <style> tag in the header,
3. Add the View (the HTML) in the <body> of the document
4. Add the Model in a <script> tag in the body of the page
5. Add the ViewModel, also in the above <script> tag
6. Bind the ViewModel, also in the above <script> tag
7. See the complete example below
8. Set your Index page as the default page for the project
9. Run the project and see what Knockout can do for you


<!DOCTYPE html>
<html>
<head>
   <title>Using KnockoutJS in a Web Page</title>
   <meta charset="utf-8" />
   <script src="Scripts/knockout-3.3.0.js"></script>
   <style>
       body {
           margin: 10px;
       }
       span {
           margin: 0 10px 0 0;
       }
       .descArea {
           padding: 10px;
           background-color: lightgray;
           border: black 1px solid;
       }
   </style>
</head>
<body>

   <span data-bind="text: shortDesc"></span>
   <div data-bind="text: description" class="descArea"></div>
   <span data-bind="text: formatCurrency(salesPrice)"></span>

   <script>
 /// <reference path="/Scripts/knockout-3.3.0.js"/>
       //The Model
       var data = {
           "Id": 123,
           "SalesPrice": 12500,
           "ListPrice": 16250,
           "ShortDesc": "Yamaha RD 350",
           "Description": "Yamaha RD 350 roadbike with helmet, jacket and gloves"
       };

       //The ViewModel
       var viewmodel = {
           id: ko.observable(data.id),
           salesPrice: ko.observable(data.SalesPrice),
           listPrice: ko.observable(data.ListPrice),
           shortDesc: ko.observable(data.ShortDesc),
           description: ko.observable(data.Description),
           formatCurrency: function (value) {
               return "R " + value().toFixed(2);
           }
       };

       //Bind the Viewmodel
       ko.applyBindings(viewmodel);
   </script>

</body>
</html>

Monday, 7 September 2015

Setting up a Visual Studio Project for use with Knockout

1. Open up Microsoft Visual Studio (this example was prepared on MS Visual Studio 2015)
2. Create a New Project
3. Choose ASP.Net Web Application from Installed templates
4. Choose Empty from Select a Template window
5. The Solution Explorer will look as follows:


6. Add KnockoutJS with NuGet package manager


7. Your solution will now look as follow:


8. Create a new Index.html page
9. Add the references to the knockout libraries in the <head> tag
10. See the code below:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="Scripts/knockout-3.3.0.js"></script>
</head>
<body>


</body>
</html>

11. With above document, you are ready to do some exercises with KnockoutJS!

Sunday, 6 September 2015

Introduction to KnockoutJS



  1. What is Knockout?


Knockout is a JavaScript library that helps to create rich, responsive display and editor user interfaces with a clean underlying data model.


It is a JavaScript implementation of the Model-View-ViewModel pattern with templates.


MVVM: Model-View-ViewModel:
* Model - Objects in Business domain
* View - User interface that is visible to the user
* View Model - code representing data on a UI


Knockout is not a replacement for other JavaScript frameworks, it is complementary to it.


Knockout works on 3 areas when developing a web pages:
* HTML
* JavaScript, and
* CSS


You need a reference to a Knockout library in your web page, either downloaded to your local project or referenced somewhere on the Internet.


Just bear in mind that Knockout keeps on evolving, so you need to make sure you have the latest Knockout libraries.


At the time of writing this blog post, the latest release is V 3.3.0 and it can be downloaded or referenced here: http://knockoutjs.com/downloads/knockout-3.3.0.js


  1. Tools to check or write Knockout code


JSFIDDLE
To test some Knockout code, just navigate to JSFIDDLE and start using the tool to create HTML, JavaScript and CSS code and to see the result of the code.


You need to setup a reference to the Knockout libraries, by clicking on: Add Resources and then copying the above url and then clicking the plus (+) sign to add it, before start writing some Knockout code.


See the basic layout of the JSFiddle website below:


Use of code Editor like Visual Studio
* You can download an express version of Visual Studio from Microsoft’s website by clicking HERE!


You can also use a normal Text Editor to create websites!


  1. Short tutorial by using a text editor


Create a basic web page with some Knockout functionality, by using a plain text editor.


* Create a folder to host your example: c:\KnockoutTest
* Create a file and call it: index.html and open it up in a text editor of your choice
* Create the basic HTML code
* Add the reference to the Knockout library to your header section
* Add the HTML code in between the <body> tags
* Add a <script> block just before the end </body> tag
* Add the Knockout code between above script block
* Save the file, and
* Run the file by double clicking on it in Windows explorer
* See code below:
<!DOCTYPE html>
<html>
<head>
<title>Knockout Example</title>
<meta charset="utf-8" />
<script src="http://knockoutjs.com/downloads/knockout-3.3.0.js"></script>
</head>
<body>
<p>First name: <input/></p>
<p>Last name: <input/></p>
<button>Show</button>
<p></p>
<script>
(function () {
function AppViewModel() {


}
ko.applyBindings(new AppViewModel());
}());
</script>
</body>
</html>


* When running the above in a Web browser, it does not do much:



* Now, let us add some Knockout code to the ViewModel:
<script>
(function () {
function AppViewModel() {
this.firstName = ko.observable();
this.lastName = ko.observable();
this.msg = ko.observable();
this.writeMsg = function () {
var p1 = this.firstName() + " " + this.lastName();
this.msg("Hello " + p1);
};
}
ko.applyBindings(new AppViewModel());
}());
</script>


* Still, not much will be happening when refreshing the page in the browser
* Let us bind the Knockout properties to the html in the view and see the action:
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<button data-bind="click: writeMsg">Show</button>
<p data-bind="text: msg"></p>


* See the complete code below:
<!DOCTYPE html>
<html>
<head>
<title>Knockout Example</title>
<meta charset="utf-8" />
<script src="http://knockoutjs.com/downloads/knockout-3.3.0.js"></script>
</head>
<body>
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<button data-bind="click: writeMsg">Show</button>
<p data-bind="text: msg"></p>


<script>
(function () {
function AppViewModel() {
this.firstName = ko.observable();
this.lastName = ko.observable();
this.msg = ko.observable();
this.writeMsg = function () {
var p1 = this.firstName() + " " + this.lastName();
this.msg("Hello " + p1);
};
}


// Activates knockout.js
ko.applyBindings(new AppViewModel());
}());
</script>
</body>
</html>


  1. Some references to check out