@bytesnz
Jack Farley, Computer Engineer

  • home
  • about
  • CSS is Awesome

    11/26/2024

    Ok. CSS is in an awesome place now. We have functionality, including CSS variables and functions like color-mix() that make writing CSS so compact. Here's what I mean.

    • css
    • software
  • Boolean Attributes in Custom Angular Components

    12/18/2023

    I like clean code so being able to have boolean attributes like required in my custom Angular components makes me happy. To do it however you need an extra bit of javascript.

    • angular
    • javascript
    • software
  • 12/18/2023

    Before I loose it again, one of the best references for creating accessible web sites is the ARIA Authoring Practices Guide (APG). It has good examples of how to create some complex components like navigation with submenus and treegrids, as well as best practices.

    • accessibility
    • software
  • 12/11/2022

    Please! Stop with the mobile web sites! If you are using browser agent strings to change your CSS/layout don't! Use responsive CSS instead. There are other situations when you need a small window and devices that have a small screen where your web site does not work.

    • software
    • web
  • Minimal Accessible Tooltip

    12/2/2022

    Doing a lot of web accessibility fixes, I wondered what would be the minimum amount of code to create a WCAG 2.1 compliant custom tooltip. Unfortunately, it had to include Javascript. Here is what I came up with.

    • software
    • web
    • a11y
  • Day-to-day with Librem 5

    11/28/2022

    After quite a few years of waiting, I finally got my Librem 5 phone. I am now using it as my primary phone… using my work phone as a back-up. Here are the issues and whether I think using it as a primary phone is a good idea so far.

    • librem5
  • 11/28/2022

    If you are wanting to apply accessibility labels onto an image, here are the label (alt, title, aria-description, aria-label) priorities (highest first): aria-description (with aria-label, alt or title); aria-label; alt. Note aria description does not get read out if there is no other label attributes. (Tested on Windows, NVDA, and Firefox and Edge).

    • software
    • web
    • a11y
  • Email dates in Javascript

    11/24/2022

    Javscript is missing functions to format dates in some common formats, such datetime-local inputs and RFC 5322 (email dates) in a non-obsolete way. While Date.toUTCString() is close, the timezone is now obsolete (though it is handy for HTTP Date and other such headers). Here's the basic functions to get the right formats

    • software
    • javascript
  • Visibility and CSS Accordians

    11/24/2022

    When changing a reworked menu to have accordian animations, I came to the realisation that I have been missing a big issue when using max-height transition animations — accessibility. I detail a simple fix in this article.

    • software
    • web
    • a11y
    • css
    • javacsript
  • Can't touch list

    11/24/2022

    Turns out screen readers really don't like you playing with the display CSS property on lists, for example to change it to an inline list — it will cause lists items to be read like a paragraph. Here's how to do without upsetting the screen readers.

    • a11y
    • software
    • css
  • New Zealand Low Power Networks (LTE-M & NB-IoT)

    10/14/2022

    I have started experimenting with low power wide area networks (LP-WAN) in New Zealand using a u-blox SARA-R410-02B. Here is what I have experimented with so far.

    • electronics
    • software
  • JS Binary Decoder

    10/11/2022

    I have recently been dealing with parsing binary data packets from various sources, and have published two NPM libraries from it: binary-decoder and sbd-direct-ip. Here's how they came to be.

    • software
    • javascript
  • Starting with ESP32

    2/7/2022

    I started developing my trap-watch project on an ESP32-CAM using the ESP IDF. Here is the newbie difficulties I ran into.

    • esp32
    • electronics
    • software
    • trap-watch
  • VIM Window Rabbit Hole

    2/4/2022

    Developing ESP-IDF components I thought it would be great if I could make a command to open all the files for a component at once. What a rabbit hole it was. Here is how I did it.

    • linux
  • Parsing Arguments in Bash

    1/28/2022

    Making a script parse arugments in Bash took me way too long last time I did it, so here is a nice full example of how to do it using getopt

    • linux
  • Where's my bait gone?

    1/8/2022

    I have recently started trapping some introduced predators around my local area and have had baits and pre-feed disappear with nothing to show for it, so I decided to get sparky and see if I could catch the culprit in the act.

    • electronics
    • conservation
    • trap-watch
  • Bathymetric Contours

    3/26/2021

    Finally got around to creating a SLD style for GeoServer to display bathymetric contour lines using the GEBCO gridded bathymetric data. Here's how.

    • gis
    • geoserver
    • oceans
  • shm That Cache

    2/25/2021

    I am often trying to find more space on my hard drives and found today my own docker containers wasting space thanks to ! Here's how I fixed it.

    • yarn
    • docker
    • development
  • Blob blob blob blob Stayin' Alive Stayin' Alive

    2/2/2021

    Upon recently trying Deezer again, I found their web app ate all my memory when running in Firefox, so I decided to see if I could find out why. I got as far as memory-file-data/string and Blobs. Here's how.

    • software
    • javascript
    • debugging
    • memory
  • A new Litter Survey

    12/25/2020

    In a culmination of litter surveys and litter picks, linked data and data exploration, and remoteStorage and ActivityPub, I have created a web-based litter pick/survey app that I hope will allow federated citizen science.

    • software
    • litter
    • litter-survey
  • Hoe Stream Trash

    12/14/2020

    My latest litter pick target was Hoe Stream and the White Rose Lane Local Nature Reserve. Here's how it went.

    • litter
    • pi-trash-cam
    • litter-pick
    • environment
  • Release the Beast

    11/24/2020

    I just created a Gitlab CI job to create a release with information from a CHANGELOG.md file for some of my projects. Here's how I did it.

    • software
    • gitlab
    • ci
    • release
  • Gitlab CI Caching

    9/30/2020

    I noticed something strange happening during build process during a multi-tasking bug fix. Turns out I was using Gitlab CI's caching incorrectly. I should have been using artifacts. Here's what I saw.

    • gitlab
    • continuous integration
  • Birthday Trash

    9/22/2020

    As a birthday treat, I took the day off work to try out my electronerised litter picker. Here's how it went.

    • litter
    • pi-trash-cam
    • environment
  • Pi Trash Cam

    9/13/2020

    In preparation for a day of litter picking, I finally got round to a project idea - attaching a camera to a litter picker to record it all. Here's what I did.

    • raspberry pi
    • electronics
    • litter
    • pi-trash-cam
    • environment
  • yarn add --dev webdriverio

    8/25/2020

    I finally started implementing UI testing on first-draft using WebdriverIO. While writing tests was easy, getting the tests running was a little more difficult. Here is how I did it.

    I have been working on first-draft, an opensource (currently) canvas-based collaborative online drawing/annotation tool for a while now. I have been meaning to add UI testing to it, but I could never find something that would allow me to test in multiple browsers, do complex interactions and work with the web socket communications first-draft uses.

    I recently decided to look at UI testing libraries again and while I was looking back over WebdriverIO, a node-based testing framework using the WebDriver API, I managed to come across the function performActions. "The Perform Actions command is used to execute complex user actions." This had been the thing I had been looking for for months and never found it!

    I looked into the spec to see what the "actions" were and they were exactly what I needed - the ability to do complex pointer (mouse and touch) and keyboard interactions. The spec includes an example of pinch and zoom interaction

    [
      {
        "type": "pointer",
        "id": "finger1",
        "parameters": {"pointerType": "touch"},
        "actions": [
          {"type": "pointerMove", "duration": 0, "x": 100, "y": 100},
          {"type": "pointerDown", "button": 0},
          {"type": "pause", "duration": 500},
          {"type": "pointerMove", "duration": 1000, "origin": "pointer", "x": -50, "y": 0},
          {"type": "pointerUp", "button": 0}
        ]
      }, {
        "type": "pointer",
        "id": "finger2",
        "parameters": {"pointerType": "touch"},
        "actions": [
          {"type": "pointerMove", "duration": 0, "x": 100, "y": 100},
          {"type": "pointerDown", "button": 0},
          {"type": "pause", "duration": 500},
          {"type": "pointerMove", "duration": 1000, "origin": "pointer", "x": 50, "y": 0},
          {"type": "pointerUp", "button": 0}
        ]
      }
    ]

    I was instantly in love. Wondering if this could be the test framework for me, I tried to find a way I could deal with the Web Socket communications and I came across browser.execute, which allows you send javascript to run in the browser.

    The stars had aligned. I had everything I needed! Now I just needed to write some code.

    Mocking the Web Socket Client

    Once I have installed webdriverio using the getting started instructions and configured it to work with typescript files, I decided how I was going to run things.

    With the ability to run javascript on the page under test, I decided to create a mock web socket client for the app to use when running tests. The mock allows the test to see all the events that have been emitted by the app, send events to the app and add handler functions to respond to events emitted by the app.

    The mock socket object
    const socket = {
      sent: [],
      queue: [],
      mockers: {},
      addMocker: (event, handler) => {
        if (socket.mockers[event]) {
          if (socket.mockers[event].indexOf(handler) === -1) {
            socket.mockers[event].push(handler);
          }
        } else {
          socket.mockers[event] = [handler];
          let i = 0;
          while (i < socket.queue.length) {
            if (event === '*') {
              handler(socket.queue[i].event, socket.queue[i].data);
            } else if (socket.queue[i].event === event) {
              handler(socket.queue[i].data);
              socket.queue.splice(i, 1);
            } else {
              i++;
            }
          }
        }
      },
      removeMocker: (event, handler) => {
        if (socket.mockers[event]) {
          const index = socket.mockers[event].indexOf(handler);
          if (index !== -1) {
            socket.mockers[event].splice(index, 1);
          }
        }
      },
      handlers: {},
      on: (event, handler) => {
        if (socket.handlers[event]) {
          if (socket.handlers[event].indexOf(handler) === -1) {
            socket.handlers[event].push(handler);
          }
        } else {
          socket.handlers[event] = [handler];
        }
      },
      off: (event, handler) => {
        if (socket.handlers[event]) {
          const index = socket.handlers[event].indexOf(handler);
          if (index !== -1) {
            socket.handlers[event].splice(index, 1);
          }
        }
      },
      emit: (event, data) => {
        console.debug('%c<<< Emitted event', 'color: #860', event, data);
        socket.sent.push({ event, data });
        if (socket.mockers[event]?.length) {
          socket.mockers[event].map((handler) => handler(data));
        } else {
          socket.queue.push({ event, data });
        }
        (socket.mockers['*'] || []).map((handler) => handler(event, data));
      },
      emitMock: (event, data) => {
        console.debug('%c>>> Received event', 'color: #090', event, data);
        (socket.handlers[event] || []).map((handler) => handler(data));
      },
      clear: () => {
        socket.sent = [];
      }
    };

    Writing Tests

    With the mock socket client set up, I set about writing my first test. I wanted to make sure the app was redirecting to a blank draft and loading the toolbar. When first-draft initially loads, it send a connect event to the server requesting the draft from the server, so I had to use my mock socket client to respond with a null value (no exist) response. I then checked that the toolbar was displayed.

    The first test checking it was redirecting and asking for the draft
    import { blankDraft } from '../helpers/utils';
    
    describe('first-draft', () => {
      before(async () => {
        // Go to first-draft
        await browser.url('/');
        // Attach function to return with a blank draft when requested
        await browser.execute(blankDraft);
      });
    
      it('redirect to a random draft', async () => {
        expect(browser).toHaveUrlContaining('/d/');
      });
    
      it('requests the draft', async () => {
        // Get the draftId from the url
        const draftId = (await browser.getUrl()).replace(/^.*\/d\/(.*)\//, '$1');
        const connectEvent = await browser.execute(() =>
          window['socketEvents'].sent.find(
            (event) => event.event === 'draft:connect'
          )
        );
    
        expect(connectEvent).toBeDefined();
        expect(connectEvent.data.name).toEqual(draftId);
        expect(connectEvent.data.getDraft).toEqual(true);
      });
    });

    I saved the test file and ran the test runner

    $ yarn run test:ui
    wdio wdio.conf.js
    ...
     "spec" Reporter:
    ------------------------------------------------------------------
    [Chrome 84.0.4147.125 linux #0-0] Spec: first-draft/test/specs/redirect.ts
    [Chrome 84.0.4147.125 linux #0-0] Running: Chrome (v84.0.4147.125) on linux
    [Chrome 84.0.4147.125 linux #0-0] Session ID: 10f20b22-88dd-4297-965f-3b71302af99e
    [Chrome 84.0.4147.125 linux #0-0]
    [Chrome 84.0.4147.125 linux #0-0] first-draft
    [Chrome 84.0.4147.125 linux #0-0]    ✓ redirect to a random draft
    [Chrome 84.0.4147.125 linux #0-0]    ✓ requests the draft
    [Chrome 84.0.4147.125 linux #0-0]
    [Chrome 84.0.4147.125 linux #0-0] 2 passing (2s)
    
    
    Spec Files:      1 passed, 1 total (100% completed) in 00:00:16

    Excited by the green ticks, I decided to get a little more complex and started on a test to test the pen tool.

    The test had to load a blank draft, select to pen tool and then click and drag to draw a line. Once the line was drawn, I then checked the events that had been emitted by the app to ensure they were as expected.

    The first pen tool tests
    import { asyncFind, blankDraft, getCoordinate } from '../helpers/utils';
    
    describe('first-draft pen tool', () => {
      it('becomes selected when clicked', async () => {
        await browser.url('/');
        await browser.execute(blankDraft);
        const toolbar = await $('#toolbar');
        expect(toolbar).toExist();
        const toolButton = await asyncFind(
          await toolbar.$$('button'),
          async (button) => (await button.getAttribute('title')) === 'Pen tool'
        );
        expect(toolButton).toExist();
    
        await toolButton.click();
        expect(toolButton).toHaveAttribute('aria-selected', 'true');
        const changeEvent = await browser.execute(() =>
          window['socketEvents'].sent.find(
            (event) => event.event === 'draft:changeTool'
          )
        );
    
        expect(changeEvent).toBeDefined();
        expect(changeEvent.data.tool).toEqual('pen');
      });
    
      it('draws a line with a mouse', async () => {
        await browser.execute(() => window['socketEvents'].clear());
    
        // Draw line
        const actions = [
          {
            type: 'pointer',
            id: 'mouse',
            parmeters: {
              pointerType: 'mouse'
            },
            actions: [
              { type: 'pointerMove', duration: 0, x: 20, y: 100 },
              { type: 'pointerDown', button: 0 },
              { type: 'pause', duration: 500 },
              {
                type: 'pointerMove',
                duration: 500,
                origin: 'pointer',
                x: 50,
                y: 50
              },
              { type: 'pause', duration: 500 },
              {
                type: 'pointerMove',
                duration: 500,
                origin: 'pointer',
                x: 50,
                y: 50
              },
              { type: 'pause', duration: 500 },
              { type: 'pointerUp', button: 0 },
              {
                type: 'pointerMove',
                duration: 500,
                origin: 'pointer',
                x: 50,
                y: 50
              },
              { type: 'pause', duration: 500 }
            ]
          }
        ];
    
        await browser.performActions(actions);
    
        const drawEvents = await browser.execute(() =>
          window['socketEvents'].sent.filter(
            (event) => event.event === 'draft:draw'
          )
        );
    
        const lastEvent = drawEvents.pop();
        const elementName = lastEvent.data.element.name;
    
        expect(lastEvent.data.element.type).toBe('Path');
        expect(lastEvent.data.active).toBe(false);
    
        for (let i = 0; i < drawEvents.length; i++) {
          expect(drawEvents[i].data.active).toBe(true);
          expect(drawEvents[i].data.element.name).toBe(elementName);
          expect(drawEvents[i].data.element.type).toBe('Path');
        }
    
        expect(lastEvent.data.element.segments.length).toBeLessThan(
          drawEvents[drawEvents.length - 1].data.element.segments.length
        );
    
        // Check didn't take in last pointer move
        const first = getCoordinate(lastEvent.data.element.segments, 0);
        const last = getCoordinate(lastEvent.data.element.segments, -1);
        expect(last[0] - first[0]).toBe(100);
        expect(last[1] - first[1]).toBe(100);
      });
    });

    I saved the test and ran the test runner again.

    For some reason it wasn't finding any events. I ran it again and looked to see if it was drawing a line on the canvas and it wasn't. Puzzled, I remembered having seen something that seemed to suggest that Chrome (the default browser) didn't currently support actions, so I tried switching to Firefox.

    [0-2]  Error:  Only Nightly release channel is supported in Devtools/Puppeteer for Firefox. Refer to the following issue:
            https://bugzilla.mozilla.org/show_bug.cgi?id=1606604
    
            You can use the following link to download Firefox Nightly edition:
            https://www.mozilla.org/en-US/firefox/channel/desktop/
    
            Adding the following binary capability in Firefox Options is mandatory to run with Nightly edition:
    
            'moz:firefoxOptions': {
                binary: '/path/to/firefox'
            }
    
            Note: "Nightly" as a term should be present in the "Firefox Application Name" across all OS's in binary path mentioned above for this to work.

    Drats. After downloading pointing the tests to use firefox nightly, I got successful test run.

    It turns out the giberish at the bottom of the error means you don't need to include the path to the binary option if a command like firefox-nightly exists (just needs to contain nightly).

    Running the Tests on Gitlab CI

    Having written and got the tests running, I now needed to commit them and get them running on Gitlab CI.

    Having found an article about getting webdriver running on gitlab CI, I added a similar job and script to my projects .gitlab-ci.com and package.json files.

    .gitlab-ci.yml addition

    ui-test:
       stage: test
       image: node:lts-alpine
       services:
         - name: selenium/standalone-firefox
           alias: selenium
       before_script:
         - yarn
       script:
         - yarn run test:ui --host=selenium

    package.json addition

      "scripts": {
        "test:ui": "NODE_ENV=test npx wdio",

    Commiting and pushing I was greeted with same need Firefox Nightly error. Noooooooo.

    After a bunch of trial and error, including building custom versions of the standalone-firefox docker image, I found, by running npx wdio run --help, that the --host argument used to select the driver host should have actually been --hostname.

    Fixing the config files and pushing to gitlab, I finally got the tests passing.

    Success.

    Other Gotchas

    While WebdriverIO is incredibly powerful, it can be quite finicky to get right. Below are some of issues I have had so far.

    Actions Not Running

    Mouse interactions seem to be very flakey unless you give them a lot of time to run (by specifying duration values that would be expected if a user was actually doing the tests). My problems with Chrome not running my actions in the first tests I wrote was actually caused by this. Changing the action (pointerDown, pointerMove etc) times to 1000ms and adding 500ms pauses between the actions solved this issue.

    Returning Complex Objects

    If try to return data when using browser.execute that can't be JSON.stringifyed, the test execution will fail. This can be avoided cleaning objects before returning them, for example returning only the properties that you need.

    Failing Tests Due to Still Loading

    If you are loading things such as images after the page has loaded and trying to test things that should happen after those things have loaded, you need to wait for those things to load. On testing the initial pan/zoom ws working correctly, the tests ran fine on my local machine, but always failed running on the Gitlab runner. Adding a large pause using browser.pause before the tests proved that it was an loading issue. I ended up adding the emitting of a ready event once all the images had loaded.

    • first-draft
    • software
    • testing
    • gitlab
  • 8/16/2020

    Hooray! My new blog is live! Based on Sapper, using MongoDB and eventually ActivityPub and ActivityStreams, it will be my federated posting hub to the world.

    • software
    • blog
  • Removing EXIF Data from Photos

    8/9/2020

    Creating this new blog, I wanted to make sure there was no metadata data leaking personal information. Here's how I removed all the metadata tags except the ones I wanted from my photos.

    • exif
    • photos
  • tmux List and Reattach

    8/9/2020

    Using tmux for your terminal multiplexer but want an easy to reattach to a session? Here's a small bash script to do it.

    • tmux
    • bash
    • software
  • Selectable Shell Examples

    8/8/2020

    Here's how to help your readers save time by making your post's shell commands easy to select and copy - with a simple CSS property.

    • markdown
    • syntax
    • css
  • Be Dates You

    8/5/2020

    Making my new blog, I didn't initially set the published dates to be native dates in the database. Here what I did to change them …and do all the upgrades I needed.

    • mongodb
    • docker
  • Testing vue components

    7/8/2020

    I recently needed to test that some Vue components were creating the correct HTML. To do this, I decided to create snapshots of Object representations of the rendered HTML.

    • software
    • vue
    • testing
    • javascript
  • No More Numbers

    2/20/2019

    HTML5 number inputs aren't useful, but tel inputs, have all the power

    • javascript
    • html5
    • software
  • A Hacker "Hacked" Me

    12/21/2018

    I decided to look into the extortion emails I have been getting and wrote a small script to extract the bitcoin addresses that have been used.

    • security
    • email
    • grep
  • Mouse Surgery

    12/7/2018

    As part of my pledge not to upgrade, I decided to repair two of my failing mice instead of replacing them with a brand new model (as tempting as it was). Here's what I did.

    • pledge to not upgrade
    • electronics
  • Danger Danger, Highly Reactive

    10/29/2018
    • vue
    • reactive
    • graphing
    • data
  • Switching to SSR

    9/11/2018
    • react
    • ssr
    • react-loader
  • Clean Docker Registry

    8/10/2018
    • node
    • docker
    • console
    • javascript
  • Testing on the Filesystem

    2/1/2018
    • testing
    • javascript
  • Tag You Are

    1/26/2018
    • javascript
    • tags
  • NaN Got Me

    1/25/2018
    • rubber duck save
    • javascript
    • nodejs
    • json
  • All our app's tabs are belong to us

    4/28/2017
    • angular
    • javascript
    • localstorage
    • rxjs
    • sessionstorage
  • Developing NPM modules

    2/20/2016
    • nodejs
    • npm
  • Nfa + Nfb or N(f+fa+fb)

    2/20/2016
    • array
    • function
    • javascript
    • jsperf
  • Photo Layout

    10/31/2015
    • arranger
    • css
    • gallery hierarchy
    • html
    • javascript
    • photos
    • tinymce
    • wordpress
    • software
  • Browning Pass HideAway Web Site

    10/31/2015
    • css
    • gallery hierarchy
    • html
    • wordpress
  • Think Mobile

    10/17/2015
    • mobile
    • web
  • Pledge to Refuse and Not Buy Bottled Water

    10/13/2015
    • environment
    • pledges
  • Run PHP run!

    2/25/2015
    • maximum execution time
    • php
    • trial and error
  • Gallery Hierarchy

    11/26/2014
    • gallery
    • hierarchy
    • photos
  • Vim and functions

    10/3/2014
    • function
    • navigation
    • tags
    • vim
  • I'm making hierarchies

    9/26/2014
    • hierarchical data
    • hierarchy
    • php
  • Google Sheets fun

    9/4/2014
    • google
    • google script
    • google sheets
    • javascript
    • sheets
  • Travel Photos

    8/13/2014
    • photo management
    • photos
    • travel photos
  • Image managment scripts

    8/13/2014
    • photo management
    • photos
  • MeldCE logo

    7/31/2014
    • css
    • html5
    • javascript
    • svg
  • MoltenDB

    4/26/2014
    • database
    • moltendb
    • mongodb
    • nodejs
  • The Expensive Side

    10/29/2013
    • house
  • abcde

    9/22/2013
    • music
  • Energizer Power Plus Rechargeable Batteries

    7/16/2013
    • gadgets
  • USAR FOGTeX

    3/4/2013
    • fogtex
  • Personal Gear Bag

    2/21/2013
    • rescue
    • gear
  • Geocaching Stamp

    2/18/2013
    • stamp
    • geocaching
  • Call Record Presenter

    4/2/2007
    • parser
    • python
    • sqlite
    • web
  • Kimi Ora School

    9/25/2004
    • css
    • html
  • All About Catering Web Site

    6/25/2004
    • css
    • html
  • Quantum Accounting Web Site

    7/26/2003
    • css
    • html
Copyright BytesNZ 2025. Powered by Sapper
[matrix]