Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android tests with Appium and Gradle

I recently started looking at doing some functional testing with Appium. I would like to run the Appium tests through Android studio via gradle.

Has anyone attempted to do this and if so can you give me some information on the setup, such as what gradle tasks to use etc.

I have included the necessary dependencies in my build file:

androidTestCompile('io.appium:java-client:2.0.0')

I have a sample test below, I just need a way of running it via gradle :)

package com.appium.trial;

import junit.framework.Assert;

import io.appium.java_client.AppiumDriver;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class TrialTest {
private static WebDriver wd;

@Before
public void setUp() {
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("appium-version", "1.0");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("platformVersion", "4.4");
    capabilities.setCapability("deviceName", "Samsung Galaxy S4 - 4.2.2 - API 17 - 1080x1920");
    capabilities.setCapability("app", "/Users/chuckster/Documents/Dev/AppiumTrial/appium-trial.apk");
    capabilities.setCapability("appPackage", "com.appium.trial");
    capabilities.setCapability("appActivity", "com.appium.trial.TrialTest");

    try {
        wd = new AppiumDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}

@Test
public static void testThatClickingTheMotorSectionLeadsToSubSection(){

    wd.findElement(By.xpath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[2]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.widget.ScrollView[1]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[5]/android.widget.TextView[1]")).click();
    wd.close();
}

@After
public void tearDown() {
    if (wd != null) {
        wd.quit();
    }
    }
}
like image 906
chuckliddell0 Avatar asked Nov 07 '14 16:11

chuckliddell0


People also ask

How do I run a test using Gradle command?

You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.

Does Gradle build also run tests?

By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.

Can I run Android Appium test in emulator?

Appium allows us to execute our tests on both real devices and emulators/simulators.


1 Answers

Running this in the command line should look through all the classes in your project for one called TrialTest and run only those tests

gradle -Dtest.single=TrialTest

You have to have a gradle task called test though. Make sure you have this in your build.gradle file

test {
    testLogging{
        events 'started', 'passed'
    }
}
like image 185
BenJi Avatar answered Sep 29 '22 05:09

BenJi