If I’m building a UI that needs to scale and support hundreds of users and never fail, I need to write tests. But tests are cumbersome and require “coders” knowledge and lots of unecessary time. However, not all tests take a long time to write. In fact, some require little to no coders knowledge.
The simplest testing library I know is cucumber and it’s description, explains it all:
Cucumber is a tool for running automated tests written in plain language. Because they're written in plain language, they can be read by anyone on your team. Because they can be read by anyone, you can use them to help improve communication, collaboration and trust on your team.
Here’s a small snippet from the tests
These are called features. Each feature has a scenario which covers tests. Each step is essentially a test. When it runs it will be either green or red. If the command is not recognized it will be yellow. Although it doesn’t take a developer to write these scripts above, it will take a developer to setup the environment. Luckily, I built a starter.
Running your first test
Now, let’s go over an example here - [https://github.com/evanjmg/EndToEndStarter]
It requires node and Java, please follow the instructions in the repo for setup.
Once your all setup (after npm install and installing Java as instructed),
you’ll want to run npm run pree2e
Then you want to start the server:
npm run watch
After it’s launched, you can run your tests:
npm run test:e2e
For safari, you’ll need to enable automation under the develop tab.
You should get the following:
You’re probably wondering where’s all magic. There are bits of code you need to write to match the selenium commands, but after writing a simple set of steps. They actually are called steps. They are regex matchers to the commands above. These are very flexible, I’ve built up about 20 or so that I use to write massive suites that test really complex user flows, some up to 10 minutes long!
Here are the following ‘steps’ from the test above:
The statements are matched through regex and the “” area is where I fill in the value I want to pass through to a function.
Then I take that value and use protractor, a javascript library that serves as a wrapper around selenium webdriver - a testing driver, to run the commands.
Seems easy right? That was just one live server example, what about a really complex UI like…
Dragging and Dropping
I built a grid view demo earlier this year using D3 and ideally I’d like to test if I can drag and add items to the grid.
You should get a cli command prompt in your terminal.
Before we start using those same protractor commands like ‘browser.get(‘/’)’, we’re going to need tell protractor that we’re not using Angular with:
browser.ignoreSynchronization = true
After, we need to go to the page we want to test (this could be anything you want!).
Now let’s create a square programmatically:
And it should add a square to the grid view!
Now let’s move it!
In order to do so, you need to cmd + j (ctrl + j - windows) to inspect the browser and look for the element’s position. In this case, I have an item on x=400, so I’ll grab it and move it:
Note that x: 300 is not the position on the grid view but the pixel position on the browser. So you won’t get one to one matching, because of zoom etc in this grid view
What’s next? Test other UIs like Fullcalendar, [https://fullcalendar.io/], or implement cucumber protractor in your own app. Here are some great resources to get you started:
Don’t like Typescript? Here’s a good Javascript example: [https://github.com/mlev/protractor-cucumber-example]
Learn more about protractor here: [http://www.protractortest.org/#/]
Learn more about cucumber: [https://cucumber.io/]
Love this cool typescript syntax? Check out Typescript: [https://www.typescriptlang.org/]
Here’s Protractor Cucumber (the bit that connects them together): [https://github.com/protractor-cucumber-framework/protractor-cucumber-framework]
If you have any questions or suggestions, please leave them in the comments.