我已经使用Android Studio完成了Appium设置。我能够启动应用程序,但无法找到按钮或textview元素。
这是我的gradle文件。
testImplementation 'junit:junit:4.10'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'
implementation files('libs/byte-buddy-1.8.3.jar')
implementation files('libs/commons-codec-1.10.jar')
implementation files('libs/commons-exec-1.3.jar')
implementation files('libs/commons-logging-1.2.jar')
implementation files('libs/gson-2.8.2.jar')
implementation files('libs/guava-23.6-jre.jar')
implementation files('libs/httpclient-4.5.3.jar')
implementation files('libs/httpcore-4.4.6.jar')
implementation files('libs/okhttp-3.9.1.jar')
implementation files('libs/okio-1.13.0.jar')
implementation files('libs/client-combined-3.12.0-sources.jar')
implementation files('libs/client-combined-3.12.0.jar')
implementation files('libs/java-client-6.0.0.jar')
这是我的测试文件。
RemoteWebDriver driver;
@Before
public void setup() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "emulator-5554");
caps.setCapability(CapabilityType.BROWSER_NAME, "Android");
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "8.0.0");// Pixel
caps.setCapability("appPackage", "com.ebay.mobile");
caps.setCapability("appActivity", "com.ebay.mobile.activities.MainActivity");
// Instantiate Appium Driver
try {
driver = new RemoteWebDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
}
}
@Test
public void testIfEBaySearchAvailable() {
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
driver.findElement(By.name("SIGN IN")).click();
driver.findElement(By.name("Search for anything")).click();
}
}
这是我得到的错误。
F:\Android\Android_Setups\android-studio-ide-143.2790544-windows\android-studio\jre\bin\java -Didea.test.cyclic.buffer.size=1048576 -Didea.launcher.port=53087 -Didea.launcher.bin.path=F:\Android\Android_Setups\android-studio-ide-143.2790544-windows\android-studio\bin -Dfile.encoding=UTF-8 -classpath F:\Android\Android_Setups\android-studio-ide-143.2790544-windows\android-studio\lib\idea_rt.jar;F:\Android\Android_Setups\android-studio-ide-143.2790544-windows\android-studio\plugins\junit\lib\junit-rt.jar;F:\Android\Android_Setups\AndroidSdk\AndroidSdk\platforms\android-27\data\res;F:\Android\workspace\Appium_Assignment\app\build\intermediates\classes\test\debug;F:\Android\workspace\Appium_Assignment\app\build\intermediates\classes\debug;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-27.1.1.aar\97c0160fe173c1eb1e77c4b8fee42635\jars\classes.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\c905d0bd51dfa2168208e1142263aed1\res;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\c905d0bd51dfa2168208e1142263aed1\jars\classes.jar;C:\Users\sandy\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.1\860340562250678d1a344907ac75754e259cdb14\hamcrest-core-1.1.jar;F:\Android\workspace\Appium_Assignment\app\libs\byte-buddy-1.8.3.jar;C:\Users\sandy\.gradle\caches\modules-2\files-2.1\android.arch.core\common\1.1.0\8007981f7d7540d89cd18471b8e5dcd2b4f99167\common-1.1.0.jar;F:\Android\workspace\Appium_Assignment\app\libs\httpcore-4.4.6.jar;F:\Android\workspace\Appium_Assignment\app\libs\commons-codec-1.10.jar;F:\Android\workspace\Appium_Assignment\app\libs\commons-logging-1.2.jar;F:\Android\workspace\Appium_Assignment\app\libs\httpclient-4.5.3.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.0.aar\2ec91a3302209fd49d42034ca4133402\res;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.0.aar\2ec91a3302209fd49d42034ca4133402\jars\classes.jar;C:\Users\sandy\.gradle\caches\modules-2\files-2.1\com.android.support.constraint\constraint-layout-solver\1.1.0\931532e953a477f876f2de18c2e7f16eee01078f\constraint-layout-solver-1.1.0.jar;F:\Android\workspace\Appium_Assignment\app\libs\client-combined-3.12.0-sources.jar;F:\Android\workspace\Appium_Assignment\app\libs\commons-exec-1.3.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-27.1.1.aar\da7d1cfef6afb9defe897425aa321eed\jars\classes.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\cf9212e0fb1c62d642cd19dfeab937e0\jars\classes.jar;F:\Android\workspace\Appium_Assignment\app\libs\okhttp-3.9.1.jar;C:\Users\sandy\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common\1.1.0\edf3f7bfb84a7521d0599efa3b0113a0ee90f85\common-1.1.0.jar;C:\Users\sandy\.gradle\caches\modules-2\files-2.1\junit\junit\4.10\e4f1766ce7404a08f45d859fb9c226fc9e41a861\junit-4.10.jar;F:\Android\workspace\Appium_Assignment\app\libs\guava-23.6-jre.jar;F:\Android\workspace\Appium_Assignment\app\libs\java-client-6.0.0.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\90e51c77499731d2386df5f896a90684\jars\classes.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\90e51c77499731d2386df5f896a90684\res;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\730e0fd60e7316c1cd8988b484b608b6\res;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\730e0fd60e7316c1cd8988b484b608b6\jars\classes.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\viewmodel-1.1.0.aar\3eb742d286119fdd6f14adfe71e13ddd\jars\classes.jar;F:\Android\workspace\Appium_Assignment\app\libs\okio-1.13.0.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\livedata-core-1.1.0.aar\d107b006abfdde5437c7aa9690e097df\jars\classes.jar;F:\Android\workspace\Appium_Assignment\app\libs\gson-2.8.2.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\support-core-utils-27.1.1.aar\59acfae8dd42985675b0190ed39a72e9\jars\classes.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\support-fragment-27.1.1.aar\6987b3584a7fa4fc091439dfe90bd43e\jars\classes.jar;F:\Android\workspace\Appium_Assignment\app\libs\client-combined-3.12.0.jar;C:\Users\sandy\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\27.1.1\39ded76b5e1ce1c5b2688e1d25cdc20ecee32007\support-annotations-27.1.1.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\d027a275dfc22e0b4feb1e5305d6fd0a\jars\classes.jar;C:\Users\sandy\.gradle\caches\transforms-1\files-1.1\multidex-1.0.3.aar\f735cffb58b1a5290f5b402162f34341\jars\classes.jar;F:\Android\workspace\Appium_Assignment\app\build\intermediates\sourceFolderJavaResources\test\debug;F:\Android\workspace\Appium_Assignment\app\build\intermediates\sourceFolderJavaResources\debug;F:\Android\workspace\Appium_Assignment\app\build\generated\mockable-android-27.v3.jar com.intellij.rt.execution.application.AppMainV2 com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 sandy.com.appium_assignment.EBayAppiumTest,testIfEBaySearchAvailable
Jun 02, 2018 5:14:11 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
org.openqa.selenium.WebDriverException: Returned value cannot be converted to WebElement: {error=invalid selector, message=Locator Strategy 'name' is not supported for this session, stacktrace=InvalidSelectorError: Locator Strategy 'name' is not supported for this session
at AndroidDriver.validateLocatorStrategy (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\lib\basedriver\driver.js:328:13)
at AndroidDriver.callee$0$0$ (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\lib\basedriver\commands\find.js:19:8)
at tryCatch (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:100:21)
at invoke (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at enqueueResult (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:185:17)
at new Promise (<anonymous>)
at new F (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\core-js\library\modules\$.export.js:30:36)
at AsyncIterator.enqueue (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:184:12)
at AsyncIterator.prototype.(anonymous function) [as next] (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:100:21)
at Object.runtime.async (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:209:12)
at AndroidDriver.callee$0$0 [as findElOrElsWithProcessing] (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\build\lib\basedriver\commands\find.js:35:30)
at AndroidDriver.callee$0$0$ (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\lib\basedriver\commands\find.js:35:21)
at tryCatch (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:100:21)
at invoke (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:136:37)
at enqueueResult (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:185:17)
at new Promise (<anonymous>)
at new F (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\core-js\library\modules\$.export.js:30:36)
at AsyncIterator.enqueue (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:184:12)
at AsyncIterator.prototype.(anonymous function) [as next] (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:100:21)
at Object.runtime.async (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\babel-runtime\regenerator\runtime.js:209:12)
at AndroidDriver.callee$0$0 [as findElement] (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\build\lib\basedriver\commands\find.js:76:30)
at C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\lib\basedriver\driver.js:275:26
at tryCatcher (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\bluebird\js\main\util.js:26:23)
at Promise._settlePromiseFromHandler (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\bluebird\js\main\promise.js:510:31)
at Promise._settlePromiseAt (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\bluebird\js\main\promise.js:584:18)
at Promise._settlePromiseAtPostResolution (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\bluebird\js\main\promise.js:248:10)
at Async._drainQueue (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\bluebird\js\main\async.js:128:12)
at Async._drainQueues (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\bluebird\js\main\async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\sandy\AppData\Local\Programs\Appium\resources\app\node_modules\appium-base-driver\node_modules\bluebird\js\main\async.js:15:14)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)}
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:03.216Z'
System info: host: 'DESKTOP-A5G86II', ip: '192.168.0.104', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_152-release'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:324)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:395)
at org.openqa.selenium.By$ByName.findElement(By.java:284)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
at sandy.com.appium_assignment.EBayAppiumTest.testIfEBaySearchAvailable(EBayAppiumTest.java:61)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to org.openqa.selenium.WebElement
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:322)
... 33 more
Process finished with exit code -1
我能够启动应用程序,但mu测试用例因上述错误而失败。
以下是uiautomatorviewer的截图
答案 0 :(得分:1)
由于您使用的是Appium:
Name
不支持定位器策略,对于Appium-Android,您可以使用
MobileBy.id/By.id
属性resource-id
当元素具有MobileBy.AccessibilityId
属性content-desc
MobileBy.AndroidUIAutomator
使用native UIAutomator java-written locator By.xpath/By.className
在稳定性/性能方面是不好的选择,但仍然可以使用。在你的情况下,最好的选择显而易见:
driver.findElement(By.id("button_sign_in")
是最好的方法
答案 1 :(得分:0)
By.name()不起作用,请尝试使用findElementByAndroidUIAutomator:
public WebElement scrollByTextTo(String desc) {
return driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" +
".scrollIntoView(new UiSelector().text(" + "\"" + desc + "\"" + "));");
}
答案 2 :(得分:0)
使用appium在移动设备屏幕上查找元素不再使用策略By.name(&#34; -----&#34;)。
您可以使用&#39; resource-id&#39;作为uiautomator查看器屏幕中显示的定位器。所以你可以使用像登录&#39;登录&#39;点击
driver.findElement(By.id("com.ebay.mobile:id/button_sign_in")).click();
或使用XPATH
driver.findElement(By.xpath("//android.widget.Button[text()='SIGN IN']")).click();
您可以使用类似的策略来搜索任何内容&#39;元件。