如何使用Java用不同的端口一一启动appium?

时间:2019-06-27 10:15:49

标签: java appium

我想使用Java编程通过不同的端口运行appium的多个实例。

例如伪

for( 2 times){  
int port = getRendomFreePort()
StartAppiumUsingPort(port)
driver = CreateAppiumDriver() 
PerformTest()
CloseAppiumDriver()
CloseAppium()
}

使用实际代码执行上述步骤时,在appium启动之后,appium驱动程序出现错误。

我写的代码如下:

第一个示例:只需使用相同的端口打开两次并成功运行Java程序

for( 2 times){
int port = getSamePort() // Using Same port
StartAppiumUsingPort(port)

// node creating appium driver
CloseAppiumDriver()
CloseAppium()
}

第二个示例:打开两次并使用相同的端口创建一个appium驱动程序,并在创建驱动程序时出错

for( 2 times){
int port = getSamePort() // Using Same port
StartAppiumUsingPort(port)
driver = CreateAppiumDriver() 
PerformTest()
CloseAppiumDriver()
CloseAppium()
}

APK.java

public class APK {
    public static void main(String[] args) throws Exception {
        AppiumDriver<MobileElement> driver = null;
        DesiredCapabilities caps = null;
        APK apk = new APK();
        AppiumServerJava appiumServer = new AppiumServerJava();

        for (int i = 0; i < 2; i++) {

            System.out.println("======== " + i + " ==============");
            String appiumURl = null;
            int port = 4723;
            if (!appiumServer.checkIfServerIsRunnning(port)) {
                appiumURl = appiumServer.startServer(port);
            } else {
                System.out.println("Appium Server already running on Port - " + port);
            }

            // Storing required values
            Map<String, String> params = new HashMap<String, String>();
            params.put("platform_", "ANDROID"); // IOS || ANDROID

            if (params.get("platform_").equals("ANDROID")) {

                params.put("deviceName_", "emulator-5554");
                params.put("UDID_", "emulator-5554");
                params.put("platformVersion_", "7.0.0");
                params.put("URL_", appiumURl);

                params.put("appUrl_",
                        "apkPath");

            }

            caps = new DesiredCapabilities();
            apk.initCaps(params, caps);


            driver = new AppiumDriver<MobileElement>(new URL(params.get("URL_")), caps);

            if (!appiumServer.checkIfServerIsRunnning(port)) {
                appiumServer.stopServer();
                System.out.println("stopped");
            }
        }
    }

    private void initCaps(Map<String, String> params, DesiredCapabilities caps) {
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, params.get("deviceName_"));
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, params.get("platform_").split("_")[0]);
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, params.get("platformVersion_"));
        caps.setCapability("newCommandTimeout", 1500);
        if (params.get("platform_").equalsIgnoreCase("ANDROID")) {
            File app = new File(params.get("appUrl_"));
            caps.setCapability("app", app.getAbsolutePath());
            caps.setCapability(MobileCapabilityType.NO_RESET, false); //false
        } 
        caps.setCapability("udid", params.get("UDID_"));
        caps.setCapability("appActivity", "pack.android.MainActivity");
        caps.setCapability("automationName", "uiautomator2");
        caps.setCapability("autoGrantPermissions", "true");
    }

}

AppiumServer.java

public String startServer(int port) throws Exception {
        //Set Capabilities
        cap = new DesiredCapabilities();
        cap.setCapability("noReset", "false");

        //Build the Appium service
        builder = new AppiumServiceBuilder();
        builder.withIPAddress("127.0.0.1");
        builder.usingPort(port);
        builder.withCapabilities(cap);
        builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
        builder.withArgument(GeneralServerFlag.LOG_LEVEL,"debug");

        //Start the server with the builder
        service = AppiumDriverLocalService.buildService(builder);
        service.start();
        System.out.println("URL:" + service.getUrl().toString());
        return service.getUrl().toString();
    }

public void stopServer() {
        if(service.isRunning()) {
            System.out.println("Server is getting closed : " + service.getUrl());
            service.stop();
            System.out.println("Server is  closed.");
        }

    }

错误日志:

Exception in thread "main" org.openqa.selenium.WebDriverException: It is impossible to create a new session because 'createSession' which takes HttpClient, InputStream and long was not found or it is not accessible
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
System info: host: 'CF-LP00152.local', ip: 'fe80:0:0:0:4f6:6753:cd93:37c7%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.6', java.version: '1.8.0_171'
Driver info: driver.version: AppiumDriver
    at io.appium.java_client.remote.AppiumCommandExecutor$1.createSession(AppiumCommandExecutor.java:195)
    at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:209)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:231)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
    at io.appium.java_client.DefaultGenericMobileDriver.<init>(DefaultGenericMobileDriver.java:38)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:84)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:94)
    at sample.code.help.APK.main(APK.java:68)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at io.appium.java_client.remote.AppiumCommandExecutor$1.createSession(AppiumCommandExecutor.java:185)
    ... 11 more
Caused by: org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
Driver info: driver.version: AppiumDriver
remote stacktrace: UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not find adb Please set the ANDROID_HOME environment variable with the Android SDK root directory path.
    at getResponseForW3CError (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/errors.js:826:9)
    at asyncHandler (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:447:37)
    at <anonymous>
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:122)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:125)
    ... 16 more

0 个答案:

没有答案