Ashish
Ashish

Reputation: 195

Crossplatform appium code for iOS and Android platform

I have written Appium code for Android platform and recently my organisation came with similar iOS app too.

I am planning to use same code for both platform.

I am successfully able to run the same code on both platform but organising code is getting difficult with every piece of code.

Guide with an appropriate approach to achieve.

My code on Android is running pretty good and same runs on iOS too. I have created two sets of capabilities like:

Capabilities File:

protected static void initCapabilities() {
getDirectory();
//Android
//deviceName = "33009a1eaa0b32a7";
//DEVICE_NAME = "emulator-5554";
//AUTOMATION_NAME = "uiautomator2";
//PLATFORM_NAME = "Android";
//BUILD = "app-internal-release - 628.apk";
//app = new File(appDirectory, BUILD);
//iOS
PLATFORM_NAME = "iOS";
PLATFORM_VERSION = "12.1";
DEVICE_NAME = "iPhone XR";
AUTOMATION_NAME = "XCUITest";
BUILD = "Jiffle.app";

app = new File(iOSAppDirectory, BUILD);
System.out.println("Build picked is: "+app);
}

Launch Code:

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

import Utils.ValidationUtils;
import Utils.LogUtils;
import Global.Constants;
import Global.SetDataInExcel;

import TestbedDataLibrary.TestCases;

import Access.Login;
import EventList.EventListing;
import MeetingList.MLElementRepositoryConstant;
import BadgeScan.BadgeScan;
import BookMeeting.RequestMeeting;
import QuickDemo.QuickDemo;
import io.appium.java_client.ios.IOSDriver;

public class Launch extends Constants {
    private static AndroidDriver<MobileElement> androidDriver;
    private static IOSDriver<MobileElement> iosDriver;

    @BeforeTest
    private void setup() {
        initCapabilities();
        initAppData();
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", PLATFORM_NAME);
        capabilities.setCapability("platformVersion", PLATFORM_VERSION);
        capabilities.setCapability("deviceName", DEVICE_NAME);
        capabilities.setCapability("automationName", AUTOMATION_NAME);
        capabilities.setCapability("app", app.getAbsolutePath());
        try {
            System.out.println("Connecting to Appium Server...");
            if (PLATFORM_NAME.equals("Android")) {
                System.out.println("Launching Android Driver...");
                androidDriver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            }
            else if (PLATFORM_NAME.equals("iOS")) {
                System.out.println("Launching iOS Driver...");
                iosDriver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            }
        } catch (MalformedURLException mle) {
            System.out.println("Caught exception in connecting to Appium Server!!!");
            mle.printStackTrace();
        }
//        androidDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        iosDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test(priority = 1)
    public void Login() throws Exception {
        System.out.println("Login Execution...");
        initTestBedData();
        long startTimeLogin = System.currentTimeMillis();

        iosDriver.switchTo().alert().accept();

        //Login.initLogin(androidDriver);
        Login.initLogin(iosDriver);
        SetDataInExcel.setTime(Constants.LoginTestBed, Constants.testSuiteSheet, startTimeLogin, "Login", 7);
    }

Please let me know if you need any other details.

Issue: My main concern is that while passing driver Login.initLogin(androidDriver); , how should I decide which driver should I pass? Also, please suggest me some improvement here.

Upvotes: 1

Views: 2267

Answers (2)

Suban Dhyako
Suban Dhyako

Reputation: 2526

You can define AppiumDriver instead of separate AndroidDriver and IOSDriver.

private static AppiumDriver<MobileElement> driver;

public AppiumDriver<MobileElement> getDriver() throws IOException {
    if (PLATFORM_NAME.equals("Android")) {
       driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    } else if (PLATFORM_NAME.equals("iOS")) {
       driver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }
    return driver;
}

In login test, you can use driver like following

@Test(priority = 1)
    public void Login() throws Exception {
        //other codes
        Login.initLogin(driver);
        //other codes
    }

Now you don't need to switch your android or ios driver. you can simply use driver

Upvotes: -1

Sameer Arora
Sameer Arora

Reputation: 4507

You can make one generic method which will return the androidDriver if the platformType is android and iosDriver if the platformType is ios and then you can use this method wherever you want to use the driver. For example:

private static AppiumDriver<MobileElement> driver;

public AppiumDriver<MobileElement> getDriver() throws IOException {
    if (PLATFORM_NAME.equals("Android")) {
        // setup the android driver
    } else if (PLATFORM_NAME.equals("iOS")) {
        // setup the ios driver
    }
    return driver;
}

Upvotes: 3

Related Questions