Skip to main content

Appium With Mocha

In this documentation, you will learn how to configure and run your Mocha automation testing scripts with Appium on LambdaTest, set the desired capabilities for appium testing, and other advanced features of LambdaTest.

Prerequisites

  • Your LambdaTest Username and Access key.
  • Install npm from the official npm website.
  • 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.

Try our Sample Repository

Step 1: Get a Sample Project

You can use your own project to configure and test it. For demo purposes, we are using the sample repository.

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

Step 2: Setup the Environment Variables

You need to export your environment variables LT_USERNAME and LT_ACCESS_KEY that are available in your LambdaTest Profile page. Run the below mentioned commands in your terminal to setup the environment variables.

export LT_USERNAME="undefined"
export LT_ACCESS_KEY="undefined"

Step 3: Upload your Application

Upload your iOS application (.ipa file) or android application (.apk or .aab 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. Below is an example cURL request to upload your app using our REST API:

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""
tip
  • If you do not have any .apk or .ipa file, you can run your sample tests on LambdaTest by using our sample apps, 🔗 Android app or 🔗 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.

Step 4: Update 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()
});
});
});

Step 5: Configure the Test Capabilities

You can update your custom capabilities in test scripts. In this sample project, 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 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
};
info

Step 6: Execute and Monitor your Tests

  • 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
  • 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

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

Reference Guides

Test across 3000+ combinations of browsers, real devices & OS.

Book Demo

Help and Support

Related Articles