代码如下:
package com.longze.testcase; import io.appium.java_client.android.AndroidDriver; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import java.net.URL; import java.util.concurrent.TimeUnit; /** * Instrumented test, which will execute on an Android device. * * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> */ @RunWith(JUnit4.class) public class setupTest { public AndroidDriver driver; @Test public void loginTest() { } @Before public void setUp() throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); //设置模拟器设备名称,命令行命令:adb devices可以查看到 capabilities.setCapability("deviceName", "192.168.158.102:5555"); //设置平台版本号 capabilities.setCapability("platformVersion", "7.0"); //设置自动化引擎名 capabilities.setCapability("automationName","Appium"); //设置平台名称:Android或者IOS等 capabilities.setCapability("platformName", "Android"); //设置需要安装的应用名称,注意:必须是本地的绝对路径 // capabilities.setCapability("app","F:\\work\\LongChunag\\app\\build\\outputs\\apk\\debug\\app-debug.apk"); //设置应用所在的包名,和下面的启动页面可以用命令行命令:aapt dump badging XXX.apk可以查到. capabilities.setCapability("appPackage", "com.android.calculator2"); //"com.longze.longchuang"); //设置该应用启动页面 capabilities.setCapability("appActivity", ".Calculator"); //设置等待设备就就绪的超时时间 capabilities.setCapability("deliveredTimeout",30); //下面二个参数使Appium支持中文输入. capabilities.setCapability("unicodeKeyboard",true); capabilities.setCapability("resetKeyboard",true); //设置Appium 服务器端侦听端口.并实例化一个Session. driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities); Thread.sleep(3000); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.findElementById("com.android.calculator2:id/digit_9").click(); Thread.sleep(1000); driver.findElementById("com.android.calculator2:id/digit_8").click(); //driver.findElementById() } @After public void tearDown() throws Exception { driver.quit(); } }
错误日志如下:
org.openqa.selenium.InvalidSelectorException: Locator Strategy 'css selector' is not supported for this session
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z'
System info: host: 'USER-Q47EI64IRI', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_181'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {appActivity: .Calculator, appPackage: com.android.calculator2, automationName: Appium, databaseEnabled: false, deliveredTimeout: 30, desired: {appActivity: .Calculator, appPackage: com.android.calculator2, automationName: Appium, deliveredTimeout: 30, deviceName: 192.168.158.102:5555, platformName: android, platformVersion: 7.0, resetKeyboard: true, unicodeKeyboard: true}, deviceManufacturer: Genymotion, deviceModel: Custom Phone - 7.0.0 - API ..., deviceName: 192.168.158.101:5555, deviceScreenSize: 768x1280, deviceUDID: 192.168.158.101:5555, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, platform: LINUX, platformName: LINUX, platformVersion: 7.0, resetKeyboard: true, takesScreenshot: true, unicodeKeyboard: true, warnings: {}, webStorageEnabled: false}
Session ID: fa00c49e-1b5e-4cfe-9908-6e19feebc4d3
*** Element info: {Using=id, value=com.android.calculator2:id/digit_9}
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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:89)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:417)
at io.appium.java_client.DefaultGenericMobileDriver.findElementById(DefaultGenericMobileDriver.java:70)
at io.appium.java_client.AppiumDriver.findElementById(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementById(AndroidDriver.java:1)
at com.longze.testcase.setupTest.setUp(setupTest.java:67)
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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)