Running Android and IOS app using Appium
Note:- To Run Android and IOS both test, You will have to install Android SDK (We use for Android apps) and XCode (We use for IOS apps). To install and work with XCode you should have MAC OS. You can not install XCode on windows.
1. Configure your Maven POM.XML file to bring all required dependencies to your project.
Add Properties to pick the testng.xml file
<properties>
<property>
<project.build.sourceEncodeing>UTF-8</project.build.sourceEncodeing>
<suiteXmlFile>testng.xml</suiteXmlFile>
</property>
</properties>
Add Dependencies
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
2. Create main configuration class
public class AppTest{
public static AppiumDriver<WebElement> driver;
public statis DesiredCapabilities cap;
public static void android_LaunchApp(){
cap = new DesiredCapabilities();
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "pixelxlo26");
cap.setCapability("appPackage", "com.example.android.apis");
cap.setCapability("appActivity", "com.example.android.apis.ApiDemos");
driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap));
Assert.assertNotNull(driver);
}
// appActivity and appPackage name you can get it using "apk info" app. You can get it from google play store. Once you will install and run this app, you can find all apps info stored in your android device
public static void ios_LaunchApp(){
cap = new DesiredCapabilities();
cap.setCapability("platformName", "iOS");
cap.setCapability("platformVersion", "11.3");
cap.setCapability("deviceName", "iPhone 8 Plus");
cap.setCapability("bundleId", "com.example.apple-samplecode.UICatalog");
driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap));
Assert.assertNotNull(driver);
}
public static void closeApp(){
driver.quit();
}
}
// to get bundleId of app file, you should have access to code, go to code location , go to Build-> Products->Debug-iphonesimulator->app, now open terminal, and type command as - osascript -e 'id of app "app location path"'
3. Create Android and IOS app test classes to run android .apk file and ios .app file
public class AndroidAppTest{
@Test
public void test() throws MalformedURLException {
AppTest.android_LaunchApp();
}
@AfterTest
public void tearDown(){
AppTest.closeApp();
}
}
public class IOSAppTest{
@Test
public void test() throws MalformedURLException {
AppTest.ios_LaunchApp();
}
@AfterTest
public void tearDown(){
AppTest.closeApp();
}
}
4. Create testng.xml file to run tests
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1">
<test name="TestAndroidApp">
<classes>
<class name="package.AndroidAppTest" />
</classes>
</test>
<test name="TestIOSApp">
<classes>
<class name="package.IOSAppTest" />
</classes>
</test>
</suite>
5. Run Appium server
6. Run testng.xml file or pom.xml as maven test.
1. Configure your Maven POM.XML file to bring all required dependencies to your project.
Add Properties to pick the testng.xml file
<properties>
<property>
<project.build.sourceEncodeing>UTF-8</project.build.sourceEncodeing>
<suiteXmlFile>testng.xml</suiteXmlFile>
</property>
</properties>
Add Dependencies
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.2.0</version>
</dependency>
</dependencies>
Add build plugin to compile and execute testng.xml file
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
public class AppTest{
public static AppiumDriver<WebElement> driver;
public statis DesiredCapabilities cap;
public static void android_LaunchApp(){
cap = new DesiredCapabilities();
cap.setCapability("platformName", "Android");
cap.setCapability("deviceName", "pixelxlo26");
cap.setCapability("appPackage", "com.example.android.apis");
cap.setCapability("appActivity", "com.example.android.apis.ApiDemos");
driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap));
Assert.assertNotNull(driver);
}
// appActivity and appPackage name you can get it using "apk info" app. You can get it from google play store. Once you will install and run this app, you can find all apps info stored in your android device
public static void ios_LaunchApp(){
cap = new DesiredCapabilities();
cap.setCapability("platformName", "iOS");
cap.setCapability("platformVersion", "11.3");
cap.setCapability("deviceName", "iPhone 8 Plus");
cap.setCapability("bundleId", "com.example.apple-samplecode.UICatalog");
driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap));
Assert.assertNotNull(driver);
}
public static void closeApp(){
driver.quit();
}
}
// to get bundleId of app file, you should have access to code, go to code location , go to Build-> Products->Debug-iphonesimulator->app, now open terminal, and type command as - osascript -e 'id of app "app location path"'
3. Create Android and IOS app test classes to run android .apk file and ios .app file
public class AndroidAppTest{
@Test
public void test() throws MalformedURLException {
AppTest.android_LaunchApp();
}
@AfterTest
public void tearDown(){
AppTest.closeApp();
}
}
public class IOSAppTest{
@Test
public void test() throws MalformedURLException {
AppTest.ios_LaunchApp();
}
@AfterTest
public void tearDown(){
AppTest.closeApp();
}
}
4. Create testng.xml file to run tests
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1">
<test name="TestAndroidApp">
<classes>
<class name="package.AndroidAppTest" />
</classes>
</test>
<test name="TestIOSApp">
<classes>
<class name="package.IOSAppTest" />
</classes>
</test>
</suite>
5. Run Appium server
6. Run testng.xml file or pom.xml as maven test.
Comments
Post a Comment