TestNG With Appium
Tutorial To Run Your First Test On LambdaTest
In this topic, you will learn how to configure and run your TestNG automation testing scripts with Appium on LambdaTest Real Device Cloud platform.
Objective
By the end of this topic, you will be able to:
- Run a sample automation script of TestNG for application testing with Appium on LambdaTest.
- Learn more about Desired Capabilities for Appium testing.
- Explore advanced features of LambdaTest.
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. 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
- Ensure you have the Java client library installed for Selenium and Appium.
- Download and install Maven following the steps from the official website. Maven can also be installed easily on Linux/MacOS using Homebrew package manager.
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"
- 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-java-testng repository and navigate to the code directory as shown below:
git clone https://github.com/LambdaTest/LT-appium-java-testng
cd LT-appium-java-testng
3. Set up your authentication
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.
- Linux / MacOS
- Windows
export LT_USERNAME=undefined \
export LT_ACCESS_KEY=undefined
set LT_USERNAME=undefined `
set LT_ACCESS_KEY=undefined
4. Write your automation script
An automation script for the sample application available above has been provided here. Ensure to update the APP_URL
, username
and accesKey
in the code scripts before running the tests.
- Android
- iOS
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import java.net.URL;
import java.util.List;
public class AndroidApp {
String userName = System.getenv("LT_USERNAME") == null ?
"username" : System.getenv("LT_USERNAME"); //Add username here
String accessKey = System.getenv("LT_ACCESS_KEY") == null ?
"accessKey" : System.getenv("LT_ACCESS_KEY"); //Add accessKey here
public String gridURL = "@mobile-hub.lambdatest.com/wd/hub";
AppiumDriver driver;
@Test
@org.testng.annotations.Parameters(value = {"device", "version", "platform"})
public void AndroidApp1(String device, String version, String platform) {
try {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build","Java TestNG Android");
capabilities.setCapability("name",platform+" "+device+" "+version);
capabilities.setCapability("deviceName", device);
capabilities.setCapability("platformVersion",version);
capabilities.setCapability("platformName", platform);
capabilities.setCapability("isRealMobile", true);
capabilities.setCapability("app", "APP_URL"); //Enter your app (.apk) url
capabilities.setCapability("deviceOrientation", "PORTRAIT");
capabilities.setCapability("console", true);
capabilities.setCapability("network", false);
capabilities.setCapability("visual", true);
capabilities.setCapability("devicelog", true);
String hub = "https://" + userName + ":" + accessKey + gridURL;
driver = new AppiumDriver(new URL(hub), capabilities);
MobileElement color = (MobileElement) driver.findElementById("com.lambdatest.proverbial:id/color");
//Changes color to pink
color.click();
Thread.sleep(1000);
//Back to orginal color
color.click();
MobileElement text = (MobileElement) driver.findElementById("com.lambdatest.proverbial:id/Text");
//Changes the text to "Proverbial"
text.click();
//toast will be visible
MobileElement toast = (MobileElement) driver.findElementById("com.lambdatest.proverbial:id/toast");
toast.click();
//notification will be visible
MobileElement notification = (MobileElement) driver.findElementById("com.lambdatest.proverbial:id/notification");
notification.click();
Thread.sleep(2000);
//Opens the geolocation page
MobileElement geo = (MobileElement) driver.findElementById("com.lambdatest.proverbial:id/geoLocation");
geo.click();
Thread.sleep(5000);
//takes back to home page
MobileElement home = (MobileElement) driver.findElementByAccessibilityId("Home");
home.click();
//Takes to speed test page
MobileElement speedtest = (MobileElement) driver.findElementById("com.lambdatest.proverbial:id/speedTest");
speedtest.click();
Thread.sleep(5000);
MobileElement Home = (MobileElement) driver.findElementByAccessibilityId("Home");
Home.click();
//Opens the browser
MobileElement browser = (MobileElement) driver.findElementByAccessibilityId("Browser");
browser.click();
MobileElement url = (MobileElement) driver.findElementById("com.lambdatest.proverbial:id/url");
url.sendKeys("https://www.lambdatest.com");
MobileElement find = (MobileElement) driver.findElementById("com.lambdatest.proverbial:id/find");
find.click();
driver.quit();
} catch (Exception e) {
e.printStackTrace();
try{
driver.quit();
}catch(Exception e1){
e.printStackTrace();
}
}
}
}
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import org.testng.annotations.Test;
public class iOSApp {
String userName = System.getenv("LT_USERNAME") == null ?
"username" : System.getenv("LT_USERNAME"); //Add username here
String accessKey = System.getenv("LT_ACCESS_KEY") == null ?
"accessKey" : System.getenv("LT_ACCESS_KEY"); //Add accessKey here
public String gridURL = "@mobile-hub.lambdatest.com/wd/hub";
AppiumDriver driver;
@Test
@org.testng.annotations.Parameters(value = {"device", "version", "platform"})
public void iOSApp1(String device, String version, String platform) {
try {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build","Java TestNG iOS");
capabilities.setCapability("name",platform+" "+device+" "+version);
capabilities.setCapability("deviceName", device);
capabilities.setCapability("platformVersion",version);
capabilities.setCapability("platformName", platform);
capabilities.setCapability("isRealMobile", true);
capabilities.setCapability("app", "APP_URL"); //Enter your app (.ipa) url
capabilities.setCapability("deviceOrientation", "PORTRAIT");
capabilities.setCapability("console", true);
capabilities.setCapability("network", false);
capabilities.setCapability("visual", true);
capabilities.setCapability("devicelog", true);
//capabilities.setCapability("geoLocation", "HK");
String hub = "https://" + userName + ":" + accessKey + gridURL;
driver = new AppiumDriver(new URL(hub), capabilities);
WebDriverWait Wait = new WebDriverWait(driver,30);
//Changes the color of the text
Wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("color"))).click();
Thread.sleep(1000);
//Changes the text to "Proverbial"
Wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("Text"))).click();
Thread.sleep(1000);
//Toast will be visible
Wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("toast"))).click();
Thread.sleep(1000);
//Notification will be visible
Wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("notification"))).click();
Thread.sleep(4000);
//Opens the geolocation page
Wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("geoLocation"))).click();
Thread.sleep(4000);
//Takes back
driver.navigate().back();
//Takes to speedtest page
Wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("speedTest"))).click();
Thread.sleep(4000);
driver.navigate().back();
//Opens the browser
Wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("Browser"))).click();
Thread.sleep(1000);
MobileElement url = (MobileElement) driver.findElementByAccessibilityId("url");
url.click();
url.sendKeys("https://www.lambdatest.com");
Wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("find"))).click();
Thread.sleep(1000);
driver.quit();
} catch (Exception e) {
e.printStackTrace();
try{
driver.quit();
}catch(Exception e1){
e.printStackTrace();
}
}
}
}
Configuring Your 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. The capabilities object in the sample code are defined as:
- Android
- iOS
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build","Java TestNG Android");
capabilities.setCapability("name",platform+" "+device+" "+version);
capabilities.setCapability("deviceName", device);
capabilities.setCapability("platformVersion",version);
capabilities.setCapability("platformName", platform);
capabilities.setCapability("isRealMobile", true);
capabilities.setCapability("app", "APP_URL"); //Enter your app (.apk) url
capabilities.setCapability("deviceOrientation", "PORTRAIT");
capabilities.setCapability("console", true);
capabilities.setCapability("network", false);
capabilities.setCapability("visual", true);
capabilities.setCapability("devicelog", true);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build","Java TestNG iOS");
capabilities.setCapability("name",platform+" "+device+" "+version);
capabilities.setCapability("deviceName", device);
capabilities.setCapability("platformVersion",version);
capabilities.setCapability("platformName", platform);
capabilities.setCapability("isRealMobile", true);
capabilities.setCapability("app", "APP_URL"); //Enter your app (.ipa) url
capabilities.setCapability("deviceOrientation", "PORTRAIT");
capabilities.setCapability("console", true);
capabilities.setCapability("network", false);
capabilities.setCapability("visual", true);
capabilities.setCapability("devicelog", true);
- You must add the generated APP_URL to the
"app"
capability in the config file. - You can generate capabilities for your test requirements with the help of our inbuilt Capabilities Generator tool. For more details, please refer to our guide on Desired Capabilities in Appium.
5. Execute your test case
- Execute the following commands to install the required dependencies:
mvn clean install
- The tests can be executed in the terminal using the following command:
- Android
- iOS
mvn test -P android-single
mvn test -P ios-single
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.