Skip to main content

Posts

Mock HTTP calls with Angular and Protractor

One of the benefits using AngularJS is also using some test tools like Protractor. Protractor is a tool that was made for Angular testing. Many times when testing you will need to mock some data so that you can see if your application responds in the correct manner. Let us assume that we have the following HTML file running our application. <!doctype html> <html ng-app="demo"> <head> <title>Hello</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-mocks.js"></script> <script src="hello.js"></script> </head> <body> <div ng-controller="Hello"> <p>Hello <span id="gname">{{exampleResponse.name}}</span></p> </div> ...

Build your own expectation library.

In the world of impending deadlines, testing is a key a vital role with any project. When building software you can test many functions and processes in a matter of minutes depending on how you setup your tests and how well the code was written to be testable. A key element for that tests is what you expect. Javascript for example, there are many expectation frameworks out there. Jasmine, Chai, Mocha to name a few. Let's build a simple expectation without their input. var expect = function(obj) { return {} } This will be our starting point for what we will expect. Very simply it is a function that will accept an argument obj and then it returns some object back that will be defined in more detail soon. You may be thinking one object? That doesn't seem quite right. For an expect statement to work shouldn't we have something like "expect(a, b, 'equal')". Yes, we could do something like that. However, for this example, we will attempt to make the co...

JSON Schema testing with Postman

Postman is becoming quite the popular tool for accessing and testing REST api services. One of the cool features is that you can write some Javascript tests on your responses. Built into Postman is also the Tiny Validator v4 and Cheerio. Cheerio is a small core JQuery like implementation. The problem I have is that there is no way to import a schema file from a remote place or a file. This will be about how I get around that issue. Pre-request Script var uri='http://someUrl.com'; $.get(uri + '/someFolder/schema.json', function(schema) { postman.setEnvironmentVariable('schema', JSON.stringify(schema)); }); This Pre-request script is what saves the day. There are a couple of timing issues that are present due to async processing. If you were to put this in your test script your chances of it not completing in time are very high and you would get inconsistent results. If you are familiar with JQuery the $.get should look familiar as a ajax call...

Javascript send text function

Simple send text function that you could use for front end tests within a browser. Replacing the value is quite easy with standard Javascript, however the DOM does not realize that the value has changed. For instance lets say you had an input field that is required to be filled in. If you just change the value by its self it will never remove the required message. To complete this we fire off a change event. function sendText(id, val) { //find the element var el = document.getElementById(id); //replace the value of the element with val el.value = val; //create an event to let the DOM know something changed. var event = new Event('change'); //fire off the event el.dispatchEvent(event); }

Display Guitar Chords on webpage

Sometimes its nice to have a handy tool for displaying different chords on a web page. For instance lets say that you want to show off some lyrics with chords to teach someone a song. I created ChordTab to have a simple way to display those chords. Code Sample <style> svg { width: 100px; } </style> <c-tab chord="g" instrument="guitar"> <script src="https://gitcdn.xyz/repo/blh0021/chordTab/master/src/tab.min.js"></script> Sample Chord Other Instruments In addition to guitar, you can also change the instrument attribute to mandolin, ukulele, or banjo. Currently not every chord has been created data wise but work is being done all the time.

Protractor getText from hidden element.

Ran across a point that I wanted to get the value of a hidden element while running my Protractor tests. getText() function was not helpful at all. In order for it to return text it has to be visible. There is an option called getAttribute('textContent'). This will allow you to always return the text. Sample HTML <span style="display: none" name="hiddenSpan">Hidden Text</span> Sample Protractor JS $('[name="hiddenSpan"]').getText(); // returns "" $('[name="hiddenSpan"]').getAttribute('textContent'); // returns "Hidden Text"

Get a little Rusty

Been playing around with Rust lang and I have to say I am starting to really enjoy it as a low level programming language.  Its kinda cool to have some modern functionality from the beginning.  Now as a software developer I can get excited but what about being a test developer.  One should probably think those are the same thing.  Well I wont start up that argument but I tend to believe they are one in the same. Lets do some basic coding. We can create us a new library called test_example. $ cargo new test_example This will create you a basic structure for a project. Included will be a src directory. Underneath that you will see lib.rs. You can open this up in your favorite text editor and see something that looks like. #[cfg(test)] mod test { #[test] fn it_works() { } } So you could run this right now and get some passing tests. $ cargo test Lets simplify what we are looking a little more. Change your lib.rs to look like. pub fn truthy(...