Execute Appium test for Hybrid Apps


#1

Hi all,
I would like to understand better how to execute an Appium Test. I read many tutorials and blogs (also official documentation) but it’s not clear at all for me.

I’m developing a Hybrid app (Angular, Ionic, Cordova). I installed appium in my environment (also Appium Desktop); I wrote a very basic appium test (appium_android.js, below; I don’t know if correct or not).
Now, the question is: how can I run it? I run the Appium server (on Appium desktop or on cli via “appium” command"). With Appium Desktop I’m able to run the built apk but not the e2e test written.

function main() {

  var webdriver = require("selenium-webdriver");

  var LOCAL_APPIUM = "http://127.0.0.1:4723/wd/hub";

  var driver = new webdriver.Builder().usingServer(LOCAL_APPIUM).withCapabilities(webdriver.Capabilities.chrome()
    .set('platformName', 'Android').set('platformVersion', '8.1.0')
    .set('deviceName', 'Android Emulator')).build();

  try {

    driver.get("https://www.google.it");

    driver.findElement(webdriver.By.tagName('button')).click();

} finally {

    driver.quit();
}

}
main();

Thanks!


#2

@jio
Hi Jio!
I am using Java + Selenium with Appium to test a Cordova app but I think I can at least get you going down the right path here. In my experience, it helps to view the components involved (Test script + Appium + App in test + Device) as independent items that get glued together at runtime. What I mean is that all four of those things are separate executables and they only get connected to each other once you call [ var driver = new webdriver … ] in your test script (which means you have to turn your test script into an executable).

I am going to attach some of my Java code as an example in a moment but first I’m going to talk through some more details in plain English. When I am running a test against my App, the steps before anything starts are:

  1. Have the .apk to be tested somewhere on the file system of your host machine (I believe there is a way to hook into an app already on the device but this adds complications)
  2. Have a device or emulator hooked up and running (knowing the device ID might help)
  3. Start an Appium server (take note of the IP address it says it is running on)

Now you are ready to execute your test script which upon startup will establish a WebDriver/AppiumDriver. While you are already doing this in your example, you should provide a few more Capabilities so that everything gets connected. Below is the Java equivalent of webdriver.withCapabilities() which basically says:

“I am making a new WebDriver. The app I want to test is located at file path xxx/xxx.apk… The device I want to test it on is ID xxxx and the Appium server to make that happen is at IP xxxx” The moment your test script says “new WebDriver” with those capabilities, all of the independent components are now connected… The apk will get loaded onto the device and start; the Appium server will begin listening for Selenium commands (through your new Webdriver) to pass through to the Android device.

Here is that Java code:

File appDir = new File("/FooAppsFolder/");
File app = new File(appDir, “app-debug.apk”);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“device”, “Android”);
capabilities.setCapability(“udid”, deviceId);
capabilities.setCapability(“deviceName”, “Android”);
capabilities.setCapability(“bundleId”, “com.FooBarApps.BazApp”);
capabilities.setCapability(“app”, app.getAbsolutePath());
capabilities.setCapability(“nativeEvents”, false);
capabilities.setCapability(“newCommandTimeout”, 120);
driver = new AndroidDriver(new URL(“http://0.0.0.0:” + appiumPort + “/wd/hub”), capabilities);

Of course all of this assumes that you already have the PATHs and Android tools such as ADB configured properly.

Now, the final step for you is getting your test script itself to execute when you are ready to test. Since I am playing in the Java world, I can just tell my IDE to “start debugging” or build a .jar executable. If you are going to stick to Javascript, you will need use a JS engine such as NodeJS to execute. I’m a little rusty on my setting up and configuring NodeJS but I did find a quick and dirty example of running a JS file here:

I hope that made sense / helps get you moving in the right direction.

Good Luck and Happy Testing!


#3

Hey there’s a new edition of appium pro article that just got released and it’s all about hyprid apps.

Hope this helps!

Automating Cross-Platform Hybrid Apps