Tuesday 15 March 2016

Appium Setup in Android Studio with TestNG



Hi all, I hope you all are doing well. I have posted an article about Setting Up Appium Server with Android Studio. I felt very glad as thousands of people visited this post and found it useful. As I explained how to use JUnit Testing Framework in my previous post , so now in this post you will learn about using TestNG framework in Android Studio.

Video -
 
I request you to go through my previous page and do the exact same steps as per below recap -

  1. Download -
    1. Android Studio
    2. Appium Jar files
    3. Latest Appium Client Library
  2. Install Android Studio -click here
  3. Open android studio and create a new project - How to create android project?
4. Add the Appium jars into your project - click on project -> click on app->copy all the jars in lib. Select the copied jars except Selenium, Java client and Junit Jar ,then right click on it and click on "Add as Library".

5. Click on build.gradle in app you will see all the libs added like below. But you need to Add TestNG specific Libs given below -


 testCompile 'org.assertj:assertj-core:2.0.0'
    testCompile 'org.testng:testng:6.9.10'

TestNG Gradle Dependency
TestNG Gradle Dependency

6. Add below TestNG test case by creating new Java Class in src->main


public class TestNGSampleTestCase{

AppiumDriver driver;

@BeforeTest
    public void testCaseSetup()throws  Exception
    {

        DesiredCapabilities cap=new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
        cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "4000");
        cap.setCapability(MobileCapabilityType.APP, "c://apks//seekbarsample.apk");
        cap.setCapability("noReset", true);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }

@Test
    public void testSeekBar()throws  Exception
    {
           //Locating seekbar using resource id
            WebElement seek_bar=driver.findElement(By.id("seek_bar"));
            // get start co-ordinate of seekbar
            int start=seek_bar.getLocation().getX();
            //Get width of seekbar
            int end=seek_bar.getSize().getWidth();
            //get location of seekbar vertically
            int y=seek_bar.getLocation().getY();

        // Select till which position you want to move the seekbar
        TouchAction action=new TouchAction(driver);

        //Move it will the end
        action.press(start,y).moveTo(end,y).release().perform();

        //Move it 40%
        int moveTo=(int)(end*0.4);
        action.press(start,y).moveTo(moveTo,y).release().perform();

        captureScreenShots("Seekbar");
    }
@AfterTest
    public void testCaseTearDown()
    {
        driver.quit();
    }
}

7. Running the TestNG test case
  • Click on build variant
  • Select Unit Testing
  • Start  the appium server with Specific port "4444"click here
  • Connect device with USB debugging on or start an emulator.
  • Right click on the test class and click on "Run".
I hope this tutorial helps and in case you have any queries do post it as a comment.

26 comments:

  1. It says cannot resolve the method captureScreenShots. Is this a user defined method? please help

    ReplyDelete
    Replies
    1. Yes it is user defined method. This post is to explain Test Suit structure using testNG framework. If you want to write code to capture screenshot then refer - http://qaautomated.blogspot.in/2016/03/how-to-take-screenshot-in-appium.html

      Delete
    2. About the error you are getting please remove the below line from your depencies in build.gradle
      testCompile 'junit:junit:4.12'

      Delete
    3. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

      Delete
  2. After removing junit line getting this error -
    Error:Gradle: Execution failed for task ':app:transformClassesWithDexForDebug'.
    > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_73\bin\java.exe'' finished with non-zero exit value 2

    ReplyDelete
    Replies
    1. add this -
      multiDexEnabled true

      in defaultconfig
      {
      multiDexEnabled true
      }

      Delete
    2. Hi,
      I am also getting same error:

      Error:Gradle: Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
      > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: io/appium/java_client/android/AndroidDeviceActionShortcuts.class

      Delete
  3. Now seeing the below errors. I have added testCompile 'org.testng:testng:6.9.10' in build.gradle.
    Error:Gradle: Execution failed for task ':app:compileDebugJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.
    Error:(5, 29) Gradle: error: package org.testng.annotations does not exist
    Error:(6, 29) Gradle: error: package org.testng.annotations does not exist
    Error:(7, 29) Gradle: error: package org.testng.annotations does not exist
    Error:(23, 5) Gradle: error: cannot find symbol class BeforeTest
    Error:(37, 5) Gradle: error: cannot find symbol class Test
    Error:(61, 5) Gradle: error: cannot find symbol class AfterTest

    ReplyDelete
  4. Have synced gradle after making changes? did the sync went sucessful? once you do it add necessary imports then you will be good to go

    ReplyDelete
  5. Yes the gradle is synced successfully and imports are added. I have sent you an email which has screenshots

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hi we configure TestNG android studio but we try to execute individual testcase
    we facing this issue below:
    org.testng.TestNGException: org.xml.sax.SAXParseException; lineNumber: 3;

    columnNumber: 44; Attribute "parallel" with value "none" must have a value from the list "false methods tests classes instances ".

    but executing througt testng.xml working fine, individual testcase not working

    ReplyDelete
  8. 6. Add below TestNG test case by creating new Java Class in src->main

    according to step 6 of this tutorial do i need to create java class directly under main folder or in java folder under main. Kindly suggest.

    ReplyDelete
    Replies
    1. you need to create java class under src ->main ->java

      Delete
  9. Hi Anuja, Hope you are doing well.

    I am stuck at the first line in my code. I am trying to use testng with appium. But getting error while initializing DesiredCapabilities instance.

    Error: Cannot resolve symbol- DesiredCapabilities

    Thanks.

    ReplyDelete
    Replies
    1. Hi, please check that your Appium Setup is successfully built and all the required libs are added in dependency section. Then your error will get resolved check this out http://www.qaautomated.com/2016/01/setting-up-appium-with-android-studio.html

      Regards,
      Anuja

      Delete
    2. Thanks Anuja. It was due to the missing selenium jar file. Santosh

      Delete
    3. Im getting the same error.I have followed the same steps mentioned in the blog to setup appium .Can you tell which selenium jar file was missing ?

      Delete
  10. Hi Anuja!
    In build.gradle:
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    but i want use TestNG. May be this string need to be replased?

    ReplyDelete
  11. Hi Anuja

    I am new to Android Studio. I am Created the project as you mentioned in your blog "Appium Setup in Android Studio with TestNG" but facing this following Error. I have added testng dependencies, also copied the testng-6.9.9 jar file to libs folder and added to library. Can you help :-)
    org.testng.TestNGException:
    Cannot find class in classpath: TestNGSampleTestCase
    at org.testng.xml.XmlClass.loadClass(XmlClass.java:81)

    ReplyDelete
  12. I have set up my project with above configuration and it will throw me error and not able to complete.

    Error:Gradle: Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
    > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/openqa/selenium/SearchContext.class

    ReplyDelete
  13. Hi Anuja!

    I am new in appium.Please can u tell me how to write 2 test cases in android studio using TestNg and how to creat testng.xml file.Please suggest me

    ReplyDelete
  14. How to fix this error:
    Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
    > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/openqa/selenium/SearchContext.class

    ReplyDelete
  15. How to fix this error:

    Error:Gradle: Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
    > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/openqa/selenium/SearchContext.class

    ReplyDelete
    Replies
    1. Did you find solution for this error?

      Delete
    2. Soled this error.
      I added the selenium jars with standalone server(following more than 1 tutorials for config!), so as it was something about duplication, I commented all the jars of selenium and only kept standalone server in build.gradle. This solved my issue.

      Delete