Harrington Web

JSON Schema testing with Postman

Thursday, October 27, 2016

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 that does a get request to a schema file. One thing to note it that what ever means you are using to serve the file you will need the proper Access-Control-Allow-Origin set. In most cases this will be set so that only that domain can access the file. If you were running this call from nodejs for example you wouldn't have this issue. Postman however is a Chrome app. Its basically running as a webpage. Please note that setting the Access-Control-Allow-Origin on a server can be a dangerous thing. Only set it where properly monitored and administered.

The Tests

var schema = JSON.parse(environment["schema"]);

var jsonData = JSON.parse(responseBody);

var results = tv4.validateMultiple(jsonData, schema);
for (var i = 0; i < results.errors.length; i++) {
      tests[JSON.stringify(results.errors[i])] = false;

The tests are fairly simple. We read in the schema that was input in the Pre-request and read in the response from the query and parse them into JSON objects. Then we run tv4.validateMultiple on them. In most examples you would just run tv4.validate. However I wanted to see each failure issue within the JSON data. In order to fail a test you would just set the tests array at a certain position to false. To make it understandable I put the error message as the key to the array and set it to false. Each one that is false will now show up under the tests when you run the query.

Final Thoughts

You have to note that this is a work around. It does have its potential issues. For example the promise for the get request could take longer than it takes to get response and the validate step. Or Access-Control-Allow-Origin opens up potential security hole. Use your best judgement.

Javascript send text function

Thursday, September 29, 2016
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

Display Guitar Chords on webpage

Wednesday, September 14, 2016
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

svg {
    width: 100px;

<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.

Thursday, August 25, 2016
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

// returns ""
// returns "Hidden Text"

Get a little Rusty

Tuesday, July 26, 2016

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.
mod 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() -> bool {
    return true;

fn its_truthy() {
Now you have a test that checks for the truthiness of truthy. Happy developing. On your red mark, get set on green, refactor...

I-Team vs We-Team

Wednesday, May 11, 2016
Agile Agile Agile....  The loudest buzzword of many I get to hear lately.  Even though we hear it does it actually sink in to our everyday practices.  So many seem to know the Agile Manifesto to heart, and also the 12 principles.  What is interesting to me in working in some of the teams is that even though we call our team an agile team, it still acts like the old waterfall project work.

I could spell out many reasons why I see conflicts that inject a lot of churn within the team.  However the biggest and I believe root cause is the word TEAM itself.   For instance within your own group have you ever said or heard something like, "we all should be responsible for the outcome"?  Then when someone is asked to do something a little outside their comfort zone they get mad or offensive and say "That is not my job", or "I do not feel qualified to do that."

This is what I like to call the I-Team.  Or Individual Team.  In General these people can come from an array of places, but their primary focus is on either their specialized role, or they just do not want to expand their horizon of knowledge any.

The opposite of this would be what I call the We-Team.  This is the complete reversal in which a person can still be specialized in a particular area, like developer or qa, yet if the need arises they could without asking jump in a fill the role.  They may also not be fully trained at the role, but they would know how and where to get the necessary information to help complete the needs of the team.

Where do you sit within your team, and how does that coincide with agility. Back to Agile big 4.
  1. Individuals and interactions over processes and tools
  2. Working software over comprehensive documentation
  3. Customer collaboration over contract negotiation
  4. Responding to change over following a plan

Individuals and interactions over processes and tools.  Imagine you are part of a team that has many members in the I-Team environment.  You may have certain timelines and processes so that each role can work on a particular assignment.  For example.  Developer may need 2 days to work on a story, but then because you are an I-Team QA will also need a day to do their work.  Sounds great right?  As it states individuals.  But what we are missing is the interactions.  That same model but it is a We-Team.  Developer and QA can pick up a card together and work through it.  Though it still might be the same amount of time to work on the card, they can do so in parallel, and also have a quicker feedback loop.  You may be asking what do you mean about feedback loop.

In the first example of I-Team this was really a happy path.  Meaning the developer is super awesome and can code every time without creating bugs.  I really want to work here by the way....  But that is not the case we live in the real world.  So lets say qa finds a bug.  Well now it has to go back to the Developer and within the sprint dev and qa will keep passing the buck back and forth until it is satisfactory.

Now you may be thinking whooopppeeee.  You would still have to pass it back and forth working on the card at the same time.  Guess what you are right.  But here is one of the benefits of the We-Team.  Both individuals are focused on the same thing.  If you are working on a story synchronously then you can reduce the feedback time, and also keep you mind on the goal.  When you are the I-Team once you pass the buck you are now moving on to the next thing in your queue.  This can lead to a very distracted validation of your new development.

Working software over comprehensive documentation.  Come on man there is no mention of people here.  Well... Your right about no mention however remember the first principle.  Process and tools.  Having the I-Team in place means you will need more rules, process and documentation of contracts so that everyone has a level playing field and expectations drawn out so everyone has nice neat lines.  To be honest the more documentation I have to keep up with the more I get annoyed.  Even more so than that how many people want to take the time to remember 500 rules of how to get something done.  Many people have trouble remembering these 4 main principles of Agile.

Customer collaboration over contract negotiation.  To me a customer is whomever you will be delivering something too.  It can be an employee or someone who is actually paying for the item.  In a team though a customer is also your other teammates. Remember from the manifesto that we value the things on the left over those on the right.  In the I-Team mentality we now have to keep up with this large set of rules and responsibilities that each role will complete.  The more rules and contracts you have the more you have to spend time maintaining those rules versus working on actual value add.  On the We-Team, we know that our responsibilities are working together and collaborating not only among out trade but with actual paying consumers as well.

Responding to change over following a plan.  The last and final section of my rant.   Responding to change.  This is such a big topic that even a small paragraph doesn't do justice.   There are long books and tons of topics on change alone.  If you work in a project with lots of different types of customers, you will see lots of change.  Change can be very good, and one of the benefits to making an agile team is allowing the team to morph and re-mold to a new shape if needed to complete the goal.  Think again about someone on the I-Team.  On an I-Team certain members may get the communication and do their part.  Awesome job done.  However just because 1 part of the team is complete doesn't mean that everything needed for the story is done.  Maybe there is some integration or testing dependencies?  Its really hard to tell when it is on the I-Team because you can never really be sure who is in the loop of what is going on.  We-Team imagine that idea of people working together and maybe even pairing on developing a story.  It would be much easier to keep everyone in the loop.  Now it also says over following a plan.  Some people and especially those who like the I-Team really love completely worked out plans.  However when then they go bad or need to be completely changed, they get offensive.  When they become offensive all of a sudden there are meetings.  Then more meetings to talk about meetings and trying to cool everyone down.  Have you been there done that?  What just happened?  Oh we got off topic and talked about how everyone feels and not about solving the issue on the change that is happening and keeping our customer happy.

Thanks for taking the time to read my rant.  But I encourage you to think about it are you on the We-Team or I-Team.  Are you willing to step outside your little cube and see what other team members are doing and how you can help?  My boss has a great phrase of "What can you do to move the needle?"  So engage with your team and see what you can do to move the needle.

Juniper VPN using OpenConnect

Wednesday, April 20, 2016

Finally there is a tool worth using for vpn besides the default from Juniper that works on Linux. Its rather quick to get started.

Install Arch

$ pacman -S openconnect

Install Ubuntu

$ apt-get install openconnect

Install Fedora/RedHat

$ dnf install openconnect

or this if you have older Fedora/RedHat

$ yum install openconnect

Once you have it installed you may need to reboot. This will allow proper access to /dev/net/tun

Run it

$ openconnect --juniper vpn.domain.com

I'll Fly Away - Lyrics and Chords

Wednesday, March 30, 2016


G Some glad morning when this life is o'er, C I'll fly away

GTo a home on God's celestial shore, C I'll fly away


G I'll fly away, O glory, C I'll fly away G In the morning.

G When I die, E hallelujah, by and by G I'll D I'll fly away


G When the shadows of this life have grown, C I'll fly away

G Like a bird from prison bars has flown, C I'll fly away


G Just a few more weary days, and then, C I'll fly away

GTo a land where joys shall never end, C I'll fly away

Calculate your Macronutrients

Tuesday, March 29, 2016

Macronutrient Calculator

All Weights are in grams.
Protein Carb Fat

Waiting for asynchronous calls in Protractor

Tuesday, March 22, 2016

Sometimes when dealing with tests the asynchronous objects are just a little ugly and hard to read. For instance if you have 3 buttons on a page and you wanted to make sure that all 3 were the same size. You would have to wrap all 3 elements together with a promise all. I am sure you can imagine the fun. With ES6 there is as new option to await asynchronous calls.

First you will need to add some libraries to your project

$ npm install babel-core babel-plugin-transform-async-to-generator

Then you will need to add some information to the protractor-conf.js

//You can add this either at the top of the file or you can put it within your onPrepare section.

Now create a .bashrc file

  "plugins": ["transform-async-to-generator"]

Now the fun part accessing the power of async and await. On you it statement we need to add an async.

it('should give us text', async function() {...

or like this if your are more current in syntax

it('should give us text', async() => {...

Now lets say you want to write the text to the console within your test.

it('should give us text', async() => {
  let someTxt = await $('.someElement').getText();
It is that simple. To compare the same using a then.
it('should give us text', () => {
    .then((val) => { console.log(val) });
If you wanna see more Protractor help ideas you can checkout the book I am working on. Protractor: Learn Testing Angular

Query Selector: How to Or Selection

Thursday, March 17, 2016

This is probably an odd topic, however I was recently asked how could one select between 2 possible elements on a browser using Protractor. If you are unfamiliar with Protractor it is a testing tool for AngularJS applications. To start lets show a code example of what we are looking at.

    <div id='ex1'>
      <!-- some code here -->
    <div id='ex2'>
      <!-- some code here -->

Notice from the code that we have 2 divs with id's ex1 and ex2. Now depending on the code you ex1 or ex2 can be hidden. Lets say hypothetically they both do the exact same thing and you just want to click which ever one happens to be available. At the same time you also don't want to add expensive checks for isPresent or waits. To get around this you can do the following with the by.css query selector.

$('[id="ex1"], [id="ex2"]')

Did you know that this same query selector will also work within JQuery, and the DevTools Console on your browser? Give it a try on this page.

  1. Goto your DevTools Console in Browser
  2. Type: $('[id="ex1"], [id="ex2"]')
  3. Hit Enter

Add Search to Blogger

Wednesday, March 16, 2016

Simple way to add a search to your blog on Blogger.

  • Click -> Layout
  • Click -> Add a Gadget
  • Click -> HTML/Javascript
  • Paste -> Code below
<form action="/search" id="searchthis" method="get" style="display: inline;">
  <input id="search-box" name="q" size="30" style="height: 30px;" type="text" />
  <input id="search-btn" type="submit" value="Search" />

Docker Compose a Selenium Grid

There are times when you would like to Selenium Grid quickly so that it can scale to whatever testing goal is you want to achieve.
To start out you will need to get Docker you can goto Docker.com to find out more information on installing.

Create a docker-compose.yml

Make a new directory and name a file docker-compose.yml inside that directory. The contents are below.
  image: selenium/hub
    - "4444:4444"
  image: selenium/node-chrome
    - hub
    - "5555"
  image: selenium/node-firefox
    - hub
    - "5555"
Once you have created your file goto that directory in your command line. This will create your instances and download necessary files.
$ docker-compose build
To run your instances you can run the following and it will start your servers.
$ docker-compose up
At this point you can now point your test code to as the selenium server. You will notice at this point that you are running only 1 Chrome and 1 Firefox container on your grid. Scaling is very simple. With your servers running you just need to run an additional command to scale to your needs. For instance lets say we want to scale up to 10 Chrome instances.
$ docker-compose scale chrome 10
Voila! you now have 10 chrome containers running on your machine. Scaling works very simple, however keep in mind that memory and processors play a big role in how many you can run on a single machine. With the Chrome container memory is very important. I find that I generally want 1gb of physical memory per Chrome container that I need. Firefox seems to be less and can tolerate 512mb most the time.