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 -
Video -
I request you to go through my previous page and do the exact same steps as per below recap -
- Download -
- Install Android Studio -click here
- Open android studio and create a new project - How to create android project?
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 |
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".
It says cannot resolve the method captureScreenShots. Is this a user defined method? please help
ReplyDeleteYes 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
DeleteAbout the error you are getting please remove the below line from your depencies in build.gradle
DeletetestCompile 'junit:junit:4.12'
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.
DeleteAfter removing junit line getting this error -
ReplyDeleteError: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
add this -
DeletemultiDexEnabled true
in defaultconfig
{
multiDexEnabled true
}
Hi,
DeleteI 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
Now seeing the below errors. I have added testCompile 'org.testng:testng:6.9.10' in build.gradle.
ReplyDeleteError: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
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
ReplyDeleteYes the gradle is synced successfully and imports are added. I have sent you an email which has screenshots
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi we configure TestNG android studio but we try to execute individual testcase
ReplyDeletewe 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
6. Add below TestNG test case by creating new Java Class in src->main
ReplyDeleteaccording 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.
you need to create java class under src ->main ->java
DeleteHi Anuja, Hope you are doing well.
ReplyDeleteI 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.
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
DeleteRegards,
Anuja
Thanks Anuja. It was due to the missing selenium jar file. Santosh
DeleteIm 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 ?
DeleteHi Anuja!
ReplyDeleteIn build.gradle:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
but i want use TestNG. May be this string need to be replased?
Hi Anuja
ReplyDeleteI 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)
I have set up my project with above configuration and it will throw me error and not able to complete.
ReplyDeleteError:Gradle: Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/openqa/selenium/SearchContext.class
Hi Anuja!
ReplyDeleteI 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
How to fix this error:
ReplyDeleteError:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/openqa/selenium/SearchContext.class
How to fix this error:
ReplyDeleteError:Gradle: Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/openqa/selenium/SearchContext.class
Did you find solution for this error?
DeleteSoled this error.
DeleteI 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.