2014-05-15 178 views
1

android設備是sony xperia Z,版本4.3。如何在真實設備上啓動android應用程序

我想自動化本機計算器應用程序。

的代碼:

import java.net.MalformedURLException; 
    import java.net.URL; 
    import org.openqa.selenium.By; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement; 
    import org.openqa.selenium.remote.CapabilityType; 
    import org.openqa.selenium.remote.DesiredCapabilities; 
    import org.openqa.selenium.remote.RemoteWebDriver; 
    import org.testng.annotations.*; 

    public class Calculator { 

    WebDriver driver; 

    @BeforeClass 
    public void setUp() throws MalformedURLException{ 

      //Set up desired capabilities and pass the Android app-activity and app-package to Appium 
      DesiredCapabilities capabilities = new DesiredCapabilities(); 
     capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android"); 
     capabilities.setCapability(CapabilityType.VERSION, "4.3"); 
     capabilities.setCapability(CapabilityType.PLATFORM, "Windows"); 
     capabilities.setCapability("device",""); 
     capabilities.setCapability("app-package", "com.android.calculator2"); // This is package name of your app (you can get it from apk info app) 
     capabilities.setCapability("app-activity", "com.android.calculator2.Calculator"); // This is Launcher activity of your app (you can get it from apk info app) 
      //Create RemoteWebDriver instance and connect to the Appium server. 
      //It will launch the Calculator App in Android Device using the configurations specified in Desired Capabilities 
     driver = new RemoteWebDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities); 

    } 
    @Test 
    public void testCal(){ 
     //locate the Text on the calculator by using By.name() 
     WebElement two=driver.findElement(By.name("2")); 
     two.click(); 
     WebElement plus=driver.findElement(By.name("+")); 
     plus.click(); 
     WebElement four=driver.findElement(By.name("4")); 
     four.click(); 
     WebElement equalTo=driver.findElement(By.name("=")); 
     equalTo.click(); 
     //locate the edit box of the calculator by using By.tagName() 
     WebElement results=driver.findElement(By.tagName("EditText")); 
       //Check the calculated value on the edit box 
       assert results.getText().equals("6"):"Actual value is : "+results.getText()+" did not match with expected value: 6"; 
     } 

    @AfterClass 
    public void teardown(){ 
     //close the app 
     driver.quit(); 
    } 
    } 

以下步驟:用系統

1.連接的設備和與命令ADB裝置檢查 - 它顯示我的設備連接。

2.使用命令啓動appium:appium & - 服務器啓動成功。

執行代碼:

我得到這個錯誤

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Parameter 'appPackage' is required for launching application) (WARNING: The server did not provide any stacktrace information) 
Command duration or timeout: 9.50 seconds 
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32' 
System info: host: 'Admin-PC', ip: '192.168.1.13', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_55' 
Driver info: org.openqa.selenium.remote.RemoteWebDriver 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193) 
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595) 
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240) 
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126) 
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:153) 
    at ExecuteTest.initialize(ExecuteTest.java:30) 
    at ExecuteTest.main(ExecuteTest.java:16) 
+0

你的主要活動和你的Manifest文件在哪裏? –

+0

你的應用程序在avd上運行嗎? – Abhishek

+0

當user2385504詢問它是否在模擬器上運行時,它會在您的設備上崩潰還是根本無法在您的設備上啓動? – Shishi

回答

1

請的能力,以下列表匹配您的代碼,並使用Appium 1.0:

DesiredCapabilities capabilities = new DesiredCapabilities(); 
    capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); 
    capabilities.setCapability("deviceName","sony xperia Z"); 
    capabilities.setCapability("platformVersion", "4.4"); 
    capabilities.setCapability("platformName","Android"); 
    capabilities.setCapability("appPackage", "com.android.calculator2"); 
    capabilities.setCapability("appActivity", "com.android.calculator2.Calculator"); 
    driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); 

或者如果包裝和活動細節如下所示使用相同的

 capabilities.setCapability("appPackage", "com.sonymobile.smallapps.calc"); 
capabilities.setCapability("appActivity", "com.sonymobile.smallapps.calc.TestActivity"); 
+0

請將'app-package'更改爲'appPackage'和'app-activity'改爲'appActivity'以使其工作。 –

相關問題