Skip to main content

Posts

Showing posts with the label JavaScript

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

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"

Query Selector: How to Or Selection

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. <html> <body> <div id='ex1'> <!-- some code here --> </div> <div id='ex2'> <!-- some code here --> </div> </body> </html> 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"]') ...

Javascript - Rounding numbers to decimal

function roundNumber(number, decimal) { return Math .round(number * Math .pow( 10 , decimal)) / Math .pow( 10 , decimal); } How it works The first thing to understand is how the Math.round() function works in Javascript. The round function takes what ever number that it is given and strips off everything after the decimal point. So if you have a number like 343.12345678 it will return 343. For this example lets say you want to have 2 decimal points left for your output like 343.12. To get the decimal points that we want we need to move the decimal point over 2 spots so that they will not be removed from the round function. Each decimal place is a factor of 10. Tenths, Hundredths, Thousandths, etc. Lets now multiply 343.12345678 by 10^2. 10 is the decimal and the 2 is how many we need to move. You should get 34312.345678. Now if you use the round function you will get 34312 right? So now that we have all the correct numbers we need to get the number back to the right value...

Javascript SOAP Client

Sometimes it is nice to get data using a SOAP call. However with using Javascript there are some disadvantages and hurdles that you may need to over come. As with any fun little project you will need some materials before we get started. Favorite Text Editor Browser Endpoint for the SOAP call - should be a url SOAP request string If you have all these things ready to go, the next thing we need to make sure of is that the proper headers are set on the server side. If this is not your server that your are trying to hit then you will need to ask the administrator of the server if they can fix up the proper access. The Headers we need are Access-Control-Allow-Origin: * Access-Control-Allow-Methods: POST Access-Control-Allow-Headers: SOAPAction, Content-Type If you happen to have a PHP SOAP server you can add these lines to the top of your code. Please remember that the header functions will need to load before you start printing other text to the page. heade...