适用于iOS和Android平台的跨平台应用程序代码

时间:2019-01-31 07:21:40

标签: java android ios appium

我已经为Android平台编写了Appium代码,最近我的组织也附带了类似的iOS应用。

我打算在两个平台上使用相同的代码。

我能够在两个平台上成功运行相同的代码,但是组织代码变得越来越困难。

用适当的方法指导实现。

我在Android上的代码运行得很好,同样在iOS上也运行。 我创建了两组功能,例如:

功能文件:

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);
}

发射代码:

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);
    }

如果您需要其他详细信息,请告诉我。

问题:我主要担心的是,在传递驱动程序Login.initLogin(androidDriver);的同时,如何确定应该传递哪个驱动程序? 另外,请在这里建议我一些改进。

2 个答案:

答案 0 :(得分:2)

您可以创建一个通用方法,如果platformType为android,则将返回androidDriver;如果platformType为ios,则将返回iosDriver,然后可以在要使用驱动程序的任何地方使用此方法。 例如:

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;
}

答案 1 :(得分:-1)

您可以定义 AppiumDriver ,而不是单独的 AndroidDriver 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;
}

在登录测试中,您可以使用以下驱动程序

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

现在,您无需切换android或ios驱动程序。您只需使用驱动程序