【发布时间】:2014-11-06 18:27:53
【问题描述】:
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 devices 检查 - 它显示我的设备已连接。
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)
【问题讨论】:
-
您的主要活动和清单文件在哪里?
-
您的应用程序是否在 avd 上运行???
-
正如 user2385504 询问的那样,它是在模拟器上运行,是在您的设备上崩溃还是根本无法在您的设备上启动?
-
我已根据我的应用程序功能更改了包名称和应用程序活动。setCapability("app-package", "com.sonymobile.smallapps.calc"); // 这是您的应用程序的包名称(您可以从 apk info app 中获取)capabilities.setCapability("app-activity", "com.sonymobile.smallapps.calc.TestActivity"); // 这是您应用的 Launcher 活动(您可以从 apk info 应用中获取)
-
但仍然显示相同的错误.. 我没有在模拟器上运行它。我正在我的设备上执行应用程序。它没有启动