Skip to main content

Mocha With Appium

Tutorial To Run Your First Test On LambdaTest


In this topic, you will learn how to configure and run your Mocha automation testing scripts with Appium on LambdaTest Real Device Cloud platform.

Objective


By the end of this topic, you will be able to:

  1. Run a sample automation script of Mocha for application testing with Appium on LambdaTest.
  2. Learn more about Desired Capabilities for Appium testing.
  3. Explore advanced features of LambdaTest.
Sample repo

All the code samples in this documentation can be found on LambdaTest's Github Repository. You can either download or clone the repository to quickly run your tests. Image View on GitHub

Pre-requisites


Before you can start performing App automation testing with Appium, please make sure:

  • You have access to LambdaTest username and accessKey. If you have not registered yet, you can do the same by visiting our website. You will be able to access the credentials in the LambdaTest Profile
  • Download and install NodeJS from official NodeJS website. You should be having NodeJS v6 or newer.
  • Make sure you are using the latest version of JavaScript.
  • Install npm from the official npm website.

Run your first test


1. Upload your application

Upload your iOS application (.ipa file) or android application (.apk file) to the LambdaTest servers using our REST API. You need to provide your Username and AccessKey in the format Username:AccessKey in the cURL command for authentication. Make sure to add the path of the appFile in the cURL request. Here is an example cURL request to upload your app using our REST API:

Using App File from System:

curl -u "undefined:undefined" -X POST "https://manual-api.lambdatest.com/app/upload/realDevice" -F "appFile=@"/Users/macuser/Downloads/proverbial_android.apk"" -F "name="proverbial_app""

Using App URL:

curl -u "undefined:undefined" -X POST "https://manual-api.lambdatest.com/app/upload/realDevice" -F "url=:https://prod-mobile-artefacts.lambdatest.com/assets/docs/proverbial_android.apk" -F "name=Proverbial_App"
tip
  • If you do not have any .apk or .ipa file, you can run your sample tests on LambdaTest by using our sample 🔗 Android app or sample 🔗 iOS app.
  • Response of above cURL will be a JSON object containing the APP_URL of the format - lt://APP123456789123456789 and will be used in the next step.

2. Clone the sample project

Clone the LambdaTest’s 🔗 LT-appium-nodejs-mocha repository and navigate to the code directory as shown below:

git clone https://github.com/LambdaTest/LT-appium-nodejs-mocha
cd LT-appium-nodejs-mocha

Make sure you have your LambdaTest credentials with you to run test automation scripts on LambdaTest. To obtain your access credentials, purchase a plan or access the Automation Dashboard. Then, set LambdaTest Username and Access Key in environment variables with following commands.

export LT_USERNAME=undefined \
export LT_ACCESS_KEY=undefined

4. Write your automation script

An automation script for the sample application available above has been provided here.

ios_test.js
const driver= require("appium-base-driver")

//const { default: driver } = require("appium-android-driver/build/lib/driver");
const { By } = require("selenium-webdriver");
const { element } = require("wd/lib/element-commands");

require("appium-base-driver")
var assert= require("assert"),
webdriver = require("selenium-webdriver"),
conf_file= process.argv[3] || "conf/ios.conf.js";


var caps = require("../" + conf_file).capabilities;

var buildDriver = function(caps) {
return new webdriver.Builder()
.usingServer(
"http://" +
LT_USERNAME +
":" +
LT_ACCESS_KEY +
"@mobile-hub.lambdatest.com/wd/hub"
)
.withCapabilities(caps)
.build();
};

describe("Mocha Appium iOS Test " + caps.browserName, function() {
var driver;
this.timeout(0);
it ('Application is launched', function name(done) {
driver=buildDriver(caps);
driver.findElement(By.xpath('//XCUIElementTypeButton[@name="color"]')).click().then(function(){
console.log("Successfully clicked Color");

});
driver.findElement(By.xpath('//XCUIElementTypeStaticText[@name="Notification"]')).click().then(function(){
console.log("Successfully clicked Notification");
});
driver.findElement(By.xpath('//XCUIElementTypeStaticText[@name="Toast"]')).click().then(function(){
console.log("Successfully clicked Toast");
});
driver.findElement(By.xpath('//XCUIElementTypeButton[@name="Text"]')).click().then(function(){
console.log("Successfully clicked Text");
driver.quit()
});
});
});

Configure the test capabilities

You can update your custom capabilities in the scripts. In our sample script, we are passing platform name, platform version, device name and app url (generated earlier) along with other capabilities like build name and test name via capabilities object.Ensure to update the APP_URL, username and accessKey in the code scripts before running the tests. The capabilities object in the sample code for a single test are defined as:

android.conf.js
LT_USERNAME = process.env.LT_USERNAME || "<your username>";      //Enter your LambdaTest username here
LT_ACCESS_KEY = process.env.LT_ACCESS_KEY || "<your accessKey>"; //Enter your LambdaTest accessKey here

exports.capabilities = {
'build': 'Mocha-Appium-Sample', //Build name
'name': 'Mocha-Android', // Test name
'platformName':'android', // OS name
'deviceName': 'Galaxy S10', // Device name
'platformVersion': '11', // OS version
'app' : 'lt://proverbial-android', // Add app (.apk) url here
'isRealMobile' : true,
'visual': false, // To take step by step screenshot
'network':false, // To capture network Logs
'console':false, // To capture console logs.
'tunnel': false // If you want to run the localhost than change it to true
};
Note

5. Execute your test case

  1. Execute the following commands to install the required dependencies:
npm i
npm install
npm install selenium-webdriver
npm i appium-android-driver
npm i appium-base-driver
npm install --save
  1. The tests can be executed in the terminal using the following command:
npm run android    //to run single test
npm run parallel_android //to run parallel tests
info

Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on the 🔗 LambdaTest App Automation Dashboard.