如何在真实设备上启动Android应用程序

时间:2014-05-15 08:00:55

标签: java android appium

android设备是sony xperia Z,版本4.3。

我想自动化原生计算器应用。

守则:

 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 & - 服务器已成功启动。

3。执行代码:

我得到错误

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)

1 个答案:

答案 0 :(得分: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");