15 Jul 2016

Automated Functional Testing with Intern

What is automated testing?

Automated testing is a way for developers to test their code without having to manually go through and review everything they have built. It allows for warnings to be thrown when specific parts of the code are broken, this helps developers quickly narrow down what is defective. Efficient automated tests can save hours of debugging and QA time. There are two types of automated testing: Unit testing and Functional testing. Unit testing takes in inputs and checks that the functions being tested return the expected output. Functional testing tests the interactions a human would have with a program. In this Blog Post we will be focusing on writing functional tests with an automated testing framework called Intern.

Why use intern…

When picking a framework to use for your automated testing you have a few different choices. You need to consider what you need from the framework and what type of application you are testing.

leadfootLogo

We chose to use Intern as our testing framework because of its flexibility and compatibility with web applications written with JavaScript. Intern is able to assist in running both unit and functional tests. chaiJSLogoIntern uses a few different tools within the framework. Leadfoot API is included to give the developer a selection of methods that can be utilized in order to mimic user interactions with different UI elements. Intern can use Selenium in order to automate browsers that are being tested and can also be leveraged to use cloud-based testing tools such as BrowserStack. Intern also uses assertion libraries to run specific tests on targeted elements. It is compatible with any assertion library, we are choosing to use Chai.js in our example.

The following is a simple example of how to use Intern

The following is a basic functional test that we are using to load the Intern.io website and check that the title text says “Intern.”

define([
'intern!object',
'intern/chai!assert',
'require'
], function (registerSuite, assert, require, registry) {
registerSuite({
name: 'intern',
'00-page-load': function () {
.get(require.toUrl('https://theintern.github.io/'))
.sleep(8000)


return this.remote
.get(require.toUrl('https://theintern.github.io/'))
.sleep(8000)
.findByClassName('logo')
.getVisibleText()
.then(function (text) {
assert.strictEqual(text, 'Intern.', ' The visible page title should say “Intern.” ');
})
.end()
}
});
});

Lets break this down…

We are using define in order to load in the modules required to write our test with Intern then registering the suite.

define([
'intern!object',
'intern/chai!assert',
'require'
], function (registerSuite, assert, require, registry) {
registerSuite({

Name of our test suite, multiple tests can share this name
name: 'intern',

Our first test name, this needs to be unique
'00-page-load': function () {

This loads the page we will be testing (Intern’s homepage) then gives it 8 seconds to load before running the next method.
return this.remote
.get(require.toUrl('https://theintern.github.io/'))
.sleep(8000)

This loads the page we will be testing (Intern’s homepage) then gives it 8 seconds to load before running the next method.
return this.remote
.get(require.toUrl('https://theintern.github.io/'))
.sleep(8000)

The title of the Intern.io page is the text in the logo. We are looking for the logo class and checking to make sure this class is visible and displaying the correct text.

.findByClassName('logo')
.getVisibleText()
.then(function (text) {


Uses the Chai Assertion to make sure the visible page title says Intern.

assert.strictEqual(text, 'Intern.', ' The visible page title should say “Intern.” ');
})

Ends the most recent filtering operation. In this case it is .findByClassName(). If you do not include the end method your next filtering operation will fail.
.end()
}
});
});

Chrome Plugin

Intern has created a Chrome plugin to make testing even easier. The Intern Recorder plugin helps track user interactions in your browser and translates them to code in the Chrome console under a tab labeled ‘Intern’. The Intern recorder is useful for targeting UI elements and tracking user processes . One of the biggest issues we found in depending on the Intern Recorder is that it relies on Xpath methods from Leadfoot that are not currently supported by IE.

In conclusion

Automated functional testing is a great way to cut down on QA and debugging time. Find the framework that is the most compatible with the code you are writing. If you are working with web applications we suggest Intern. If you need more help setting up your first test here is a link to Intern’s getting started documentation .

Now have fun writing functional tests!

Links

Share this

© 2017 RealEyes Media, LLC. All rights reserved.