Appium Tip #6: Jazzing JavaScript with Node.js

Everything About Appium Test Automation

This is the sixth blog in our 37 Things You Should Know About Appium and this time our focus will be on JavaScript, with some additional flavors on Node.js stuff. Furthermore, I have great news regarding getting all these tips and tricks delivered to you: everyone who has signed up to our Appium Weekly will have these blog posts automatically delivered and also get some additional info within those emails (some extra tips and tricks).

tip#6-thumbnail

JavaScript has become a de-facto programming language in web-based applications and it is widely and commonly used in other software domains, such as in game development and naturally server-side network programming, as well. Appium can be run with JavaScript through Node.js – thanks to different testing frameworks such as Mocha and Nightwatch. We truly encourage you guys to take a look at these – IF you consider using JavaScript and/or Node.js for your mobile app testing. Meanwhile, we recommend downloading our free beginner’s guide and get started with Appium mobile testing with JavaScript today.

Getting Started

There is one awesome example (with some source code) by Aaron Colby using Nightwatch framework and this example provides an insight on how to launch Safari browser and use your tests on navigating to a specific website. Nightwatch is the framework for browsers and web apps specifically. Take a look at the example first – it’s available at https://github.com/acolby/Testdroid_Example.

To recap some of the essential things from Aaron’s example and some further things to consider:

Add Dependencies

1.) npm dependencies

$ npm install

2.) install mocha globally

$ npm install -g mocha

Add Credentials to ./.creds.json

You will need to add an object testdroid to the file ./.creds.json, as it is .gitignored:

{
    "testdroid": {
        "username": "TESTDROID_USERNAME",
        "password": "TESTDROID_PASSWORD"
    }
}

Run the test

$ mocha ios_safari.js

All dependencies MUST be installed as instructed in the readme file. Then, create a new file called .creds.json and add your Testdroid credentials as illustrated and exampled above.

You also have to create a directory named as "temp" for all screenshots that will be taken during the test session. Explore the ios_safari.js file to see how the test itself is built. In order to make the device finder look for freemium devices, add the creditsPrice value as 0 (or 1 for premium devices) to the criteria, as follows:

if(devices.data[i].locked === false && devices.data[i].creditsPrice === 0) 
{
  device = devices.data[i];
  i = devices.data.length;
}

Example of Appium for Mocha based Node.js tests

Let’s use the earlier sample as a base to create a native iOS app test next! The difference here is that we won’t be needing the Nightwatch framework at all.

Desired capabilities

Edit the desired capabilities, namely the testdroid_target, testdroid_app and bundleId. Also, make sure to remove the ‘browser’ capability.

desired.testdroid_username = creds.testdroid.username;
desired.testdroid_password = creds.testdroid.password;
desired.testdroid_target = 'ios';
desired.testdroid_project = 'Test iOS Mocha';
desired.testdroid_testrun = 'TestRun A';
desired.testdroid_device = device.displayName;
desired.platformName = 'iOS';
desired.deviceName = 'iOS Device';
desired.testdroid_app = 'sample/BitbarIOSSample.ipa';
desired.bundleId = 'com.bitbar.testdroid.BitbarIOSSample';

Using testdroid_app = 'sample/BitbarIOSSample.ipa' you can default to the pre-existing sample app which is always available in cloud! For android the same would be 'sample/BitbarSampleApp.apk'.

Test case

Let’s edit the test case to do something with the native sample app, instead of the safari targeted commands:

it("should set text to 'Tester'", function() {
return driver
.sleep(4000)
.saveScreenshot(SCREEN_SHOT_PATH + '/test.png')
.sleep(4000)
.elementByClassName('UIATextField')
.click()
.keys('Tester')
.saveScreenshot(SCREEN_SHOT_PATH + '/test2.png')
.hideKeyboard('Return')
.sleep(4000)
.saveScreenshot(SCREEN_SHOT_PATH + '/test3.png')
.sleep(4000)
});

You may of course change the name of the ios_safari.js file to something that makes more sense, like ios_native.js or so. The test case function describe("ios safari", function() { should probably be re-named at this point too.

Documentation

For the Selenium based driver commands, best place to refer would be the Selenium Webdriver’s Node.js documentation’s full jsonwire mapping.

Running Tests in Cloud – with REAL Devices

Missing some hot Android or iOS device in your testing roster? Or maybe all of them? That’s why Testdroid Cloud was originally created: to serve all Android and iOS developers and testers to access any device, any time, and from anywhere.

First, get your environment set up properly.

Secondly, select suitable programming language that you use regular basis and adopt/build your tests around it.

Finally, follow up with our best practices – and you’ll be good.

See you again next week and keep up the great work on your testing!


The Beginner’s Guide of Using Appium for Mobile App Testing

Learn all the basics about Appium, how to set it up and how to use it for mobile app testing.

Download

  • Matthew McIntyre

    Thanks for this article! I am just wondering how I add Credentials to ./.creds.json. I could not find a .creds.json in the Testdroid_Example repository. Do I need to create that file in the project root folder?

    • Ville-Veikko Helppi

      Hi Matthew! All dependencies MUST be installed as instructed in the readme file. Then, create a new file called .creds.json and add your Testdroid credentials as illustrated and exampled above. Hopefully this helped!

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close