2015-06-22 55 views
0

在單獨項目中嘗試時,Appium運行完美。但是當我使用WebDriver/Sikuli獲得以下例外時,使用帶有webdriver/sikuli的Appium時出現「原始錯誤:無法找到連接的Android設備」錯誤

org.openqa.selenium.SessionNotCreatedException:無法創建新會話。 (原來的錯誤:無法找到一個連接的Android設備。)

我的代碼是:

public class HelloAppium { 
static AppiumDriver appiumDriver; 
static WebDriver driver; 
static AndroidDriver androidDriver; 

public static void main(String[] args) throws InterruptedException, MalformedURLException { 


    AppiumServerUtils aServer = new AppiumServerUtils(); 
    aServer.startServer(); 
    System.out.println("Started"); 

    setUp(); 

    WebElement ele = androidDriver.findElement(By.id("com.android.device:id/editPin")); 
    ele.click(); 
    ele.sendKeys("1111"); 
    ele.clear(); 

    ele.sendKeys(""); 
    androidDriver.hideKeyboard(); 

    aServer.stopServer(); 

} 

private static void setUp() throws MalformedURLException { 
    DesiredCapabilities mDesiredCapabilities = new DesiredCapabilities(); 
    mDesiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); 
    mDesiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto X"); 
    mDesiredCapabilities.setCapability(MobileCapabilityType.APP_PACKAGE, "net.android.device"); 
    mDesiredCapabilities.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.launcher.Main"); 
    mDesiredCapabilities.setCapability(MobileCapabilityType.APP_WAIT_ACTIVITY, "com.android.tools.remotecontrol.dialogs.Authentication"); 

    mDesiredCapabilities.setCapability(MobileCapabilityType.VERSION, "4.4.4"); 

    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), mDesiredCapabilities); 
    androidDriver = (AndroidDriver)driver; 
    appiumDriver = (AppiumDriver)driver; 
    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS); 
    androidDriver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS); 
    appiumDriver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS); 

} 

}

的CommandLine代碼開始Appium是,

public class AppiumServerUtils { 

public void startServer(){ 
    CommandLine command = new CommandLine("cmd"); 
    command.addArgument("/c",false); 
    command.addArgument("F:\\Softwares\\Selenium\\Appium\\AppiumForWindows-1.2.4.1\\Appium\\node.exe",false); 
    command.addArgument("F:\\Softwares\\Selenium\\Appium\\AppiumForWindows-1.2.4.1\\Appium\\node_modules\\appium\\bin\\appium.js", false); 
    command.addArgument("--address",false); 

    command.addArgument("127.0.0.1",false); 
    command.addArgument("--port",false); 
    command.addArgument("4723",false); 
    command.addArgument("--bootstrap-port",false); 
    command.addArgument("4724",false); 
    command.addArgument("--selendroid-port",false); 
    command.addArgument("8082",false); 
    command.addArgument("--no-reset",false); 
    command.addArgument("--local-timezone",false); 
    command.addArgument("--log",false); 
    command.addArgument("F:\\Softwares\\Selenium\\Appium\\appiumServerLogs.txt",false); 
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); 
    DefaultExecutor executor = new DefaultExecutor(); 
    executor.setExitValue(1); 


    InputStream is = new InputStream() { 
     @Override 
     public int read() throws IOException { 
      return 0; 
     } 
    }; 

    try{ 
     //code for looking Appium server logs 
     //executor.getStreamHandler().setProcessOutputStream(is); 

     executor.execute(command, resultHandler); 

     for(int i=1;i<=10;i++){ 
      if(new File("F:\\Softwares\\Selenium\\Appium\\appiumServerLogs.txt").length()!=0){ 
       System.out.println("file size is not Zero "); 
       break; 
      } 

      System.out.println("file size is Zero "); 
       Thread.sleep(4000); 

     } 

    }catch(IOException e){ 
     e.printStackTrace(); 
    } 
    catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public void stopServer(){ 

    CommandLine command = new CommandLine("cmd"); 
    command.addArgument("/c"); 
    command.addArgument("taskkill"); 
    command.addArgument("/F"); 
    command.addArgument("/IM"); 
    command.addArgument("node.exe"); 

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); 
    DefaultExecutor executor = new DefaultExecutor(); 
    executor.setExitValue(1); 

    try { 
     executor.execute(command, resultHandler); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


} 

}

日誌:

[36minfo[39m: [37m-->[39m [37mPOST[39m [37m/wd/hub/session[39m [90m{"desiredCapabilities":{"platformName":"Android","deviceName":"Moto X","appActivity":"com.android.launcher.Main","appWaitActivity":"com.android.tools.remotecontrol.dialogs.Authentication","appPackage":"net.android.device","version":"4.4.4"}}[39m [36minfo[39m: [debug] The following desired capabilities were provided, but not recognized by appium. They will be passed on to any other services running on this server. : version [36minfo[39m: [debug] Didn't get app but did get Android package, will attempt to launch it on the device [36minfo[39m: [debug] Creating new appium session adee76d7-4d68-46a7-96d2-ae617d5a900d [36minfo[39m: Starting android appium [36minfo[39m: [debug] Using fast reset? false [36minfo[39m: [debug] Preparing device for session [36minfo[39m: [debug] Not checking whether app is present since we are assuming it's already on the device [36minfo[39m: [debug] Checking whether adb is present [36minfo[39m: [debug] Using adb from D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe [36minfo[39m: Retrieving device [36minfo[39m: [debug] Trying to find a connected android device [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Could not find devices, restarting adb server... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" kill-server [31merror[39m: Error killing ADB server, going to see if it's online anyway [36minfo[39m: [debug] Getting connected devices... [36minfo[39m: [debug] executing: "D:\Saravanan\Softwares\android-sdk\platform-tools\adb.exe" devices [36minfo[39m: [debug] Sent shutdown command, waiting for UiAutomator to stop... [33mwarn[39m: UiAutomator did not shut down fast enough, calling it gone [31merror[39m: Failed to start an Appium session, err was: Error: Could not find a connected Android device. [36minfo[39m: [debug] Cleaning up android objects [36minfo[39m: [debug] Cleaning up appium session [36minfo[39m: [debug] Error: Could not find a connected Android device. at ADB.getDevicesWithRetry (F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:612:15) at androidCommon.prepareActiveDevice (F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\lib\devices\android\android-common.js:387:12) at null. (F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\lib\devices\android\android-common.js:325:26) at F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\node_modules\async\lib\async.js:610:21 at F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\node_modules\async\lib\async.js:249:17 at iterate (F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\node_modules\async\lib\async.js:149:13) at F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\node_modules\async\lib\async.js:160:25 at F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\node_modules\async\lib\async.js:251:21 at F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\node_modules\async\lib\async.js:615:34 at androidCommon.prepareEmulator (F:\Softwares\Selenium\Appium\AppiumForWindows-1.2.4.1\Appium\node_modules\appium\lib\devices\android\android-common.js:377:5) [36minfo[39m: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Could not find a connected Android device.)","origValue":"Could not find a connected Android device."},"sessionId":null} [36minfo[39m: [37m<-- POST /wd/hub/session [39m[31m500[39m[90m 27317.283 ms - 206[39m [90m[39m Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Could not find a connected Android device.) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 27.41 seconds Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50' System info: host: 'Saravanan-PC', ip: '192.168.101.22', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.6.0_45' Driver info: io.appium.java_client.android.AndroidDriver at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599) at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:180) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:153) at io.appium.java_client.AppiumDriver.(AppiumDriver.java:109) at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:40) at com.helloappium.HelloAppium.setUp(HelloAppium.java:134) at com.helloappium.HelloAppium.main(HelloAppium.java:49)

誰能幫&任何建議,以解決這個問題?提前致謝。

回答

0

這看起來很奇怪,你的APP_PACKAGE "net.android.device"APP_ACTIVITY "com.android.launcher.Main"指向不同的包。

這通常在AndroidManifest.xml中定義如下

APP_PACKAGE com.example.simpleaapp 
APP_ACTIVITY com.example.simpleaapp.MyActivity 
在我們的應用程序
+0

,修改的軟件包名稱。它的一個可選的你可以改變或不改變 – saravana

+0

但是這也顯示在日誌中 - 不同的包 – Eugene

相關問題