首页 文章

在Android上的Appium中无法使用findElement

提问于
浏览
1

我想点击“设置”应用中的“显示”按钮 . 但是“findElementBy ..”语句不起作用,有时我得到'nullpointer exception'或'没有这样的元素存在'异常,有时候测试过程没有单击Display按钮 .

Click to View Image

我已尝试过不同的应用程序,但仍然是findElementBy ...在Win10中不起作用 . 但是,当我在Win 7笔记本电脑的同一个真实设备上运行时,同样的代码正在运行 .

Project is using following jars:

  • gson-2.2.2.jar

  • java-client-4.1.2.jar

  • selenium-server-standalone-3.4.0.jar

  • Eclipse的Android开发工具(插件)

  • Android for Maven Eclipse 1.4.0(插件)

  • Appium Sever(版本:1.4.13.1)

Appium Code:

@Test
public void Script1() throws Exception{

    DesiredCapabilities capabilities = new DesiredCapabilities();

    capabilities.setCapability("automationName", "UiAutomator 2");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("platformVersion", "6.0.1");
    capabilities.setCapability("deviceName", "Redmi 3S");
    capabilities.setCapability("appPackage", "com.android.settings");
    capabilities.setCapability("appActivity", "com.android.settings.MainSettings");

    AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    Thread.sleep(5000); 
driver.findElementByAndroidUIAutomator("UiSelector().text(\"Display\")").click();
}

我已经尝试过不同类型的定位器(即ByID,ClassPath,xpath,UISelector等),但在win 10机器上没有任何效果 . 我不这么认为有定位器的问题,因为他们正在我的win7笔记本电脑上工作 .

Appium日志:

使用以下命令启动Appium服务器:C:\ Program Files(x86)\ Appium \ node.exe lib \ server \ main.js --address 127.0.0.1 --port 4723 --platform-name Android --platform-version 23 --automation-name Appium --log-no-color警告:已弃用对节点<0.12版本的Appium支持,将在以后的版本中删除 . 请升级! info:欢迎来到Appium v1.4.13(REV c75d8adcb66a75818a542fe1891a34260c21f76a)信息:Appium REST http接口监听器在127.0.0.1:4723上启动信息:[debug]非默认服务器args:{“address”:“127.0.0.1”,“logNoColors “:true,”platformName“:”Android“,”platformVersion“:”23“,”automationName“:”Appium“} info:Console LogLevel:debug info: - > POST / wd / hub / session {”desiredCapabilities“ :{“appPackage”:“com.android.settings”,“appActivity”:“com.android.settings.MainSettings”,“platformVersion”:“6.0.1”,“automationName”:“UiAutomator 2”,“platformName” :“Android”,“deviceName”:“Redmi 3S”},“requiredCapabilities”:{},“capabilities”:{“desiredCapabilities”:{“appPackage”:“com.android.settings”,“appActivity”:“com .android.settings.MainSettings“,”platformVersion“:”6.0.1“,”automationName“:”UiAutomator 2“,”platformName“:”Android“,”deviceName“:”Redmi 3S“},”requiredCapabilities“:{ },“alwaysMatch”:{“platformName”:“Android”},“firstMatch”:[]}} info:客户端用户代理字符串:Apache-HttpClient / 4.5.3(Java / 1.8.0_144)信息:[调试]没有得到应用程序,但确实得到Android包,将尝试在设备信息上启动它:[debug]创建新的appium会话44f52f68-60a6-4469-9575-0c1bfe2d999d信息:启动android appium信息:[获取Java版本信息:Java版本是:1.8.0_144信息:[debug]检查adb是否存在信息:[debug]使用来自C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools的adb \ adb.exe警告:没有应用程序功能,无法解析包/活动信息:[debug]使用快速重置?真实信息:[debug]为会话信息准备设备:[debug]不检查app是否存在因为我们假设它已经在设备信息上:检索设备信息:[debug]试图找到连接的android设备信息:[debug ]获取连接的设备...信息:[debug]执行cmd:“C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe”设备信息:[debug] 1个设备连接信息:找到设备964aa907d030信息:[debug]将设备ID设置为964aa907d030信息:[debug]等待设备准备就绪并响应shell命令(timeout = 5)info:[debug]执行cmd:“C:\程序文件(x86)\ Android \ android-sdk \ platform-tools \ adb.exe“-s 964aa907d030等待设备信息:[debug]执行cmd:”C:\ Program Files(x86)\ Android \ android- sdk \ platform-tools \ adb.exe“-s 964aa907d030 shell”echo'ready'“info:[debug]启动logcat捕获信息:[debug]获取设备API级别信息:[debug]执行cmd:”C:\ Program文件(x86)\ Android \ android-sdk \ plat form-tools \ adb.exe“-s 964aa907d030 shell”getprop ro.build.version.sdk“info:[debug]设备处于API Level 23信息:设备API级别为:23 info:[debug]提取语言的字符串:默认信息:[debug] Apk本地不存在信息:[debug]无法获取字符串,但看起来我们有一个旧的字符串文件,所以忽略信息:[debug]执行cmd:“C:\ Program文件(x86)\ Android \ android-sdk \ platform-tools \ adb.exe“-s 964aa907d030 shell”rm -rf /data/local/tmp/strings.json“info:[debug]由于服务器未启动,因此未卸载应用程序with --full-reset info:[debug]跳过安装,因为我们推出了一个包而不是应用程序路径信息:[debug]转发系统:4724到设备:4724信息:[debug]执行cmd:“C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe “-s 964aa907d030 forward tcp:4724 tcp:4724 info:[debug]将appium bootstrap推送到设备... info:[debug]执行cmd:”C:\ Program Files(x86)\ Android \ android-sdk \ platform- tools \ adb.exe“-s 964aa907d030 push”C:\ Program Files(x86)\ Appium \ node_modules \ appium \ build \ android_bootstrap \ AppiumBootstrap.jar“/ data / local / tmp / info:[debug]推送设置apk到设备...信息:[debug]执行cmd:“C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe”-s 964aa907d030安装“C:\ Program Files(x86)\ Appium \ node_modules \ appium \ build \ settings_apk \ settings_apk-debug.apk“info:[debug]将解锁帮助应用程序推送到设备... info:[debug]执行cmd:”C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe“-s 964aa907d030安装”C:\ Program Files(x86)\ Appium \ node_modules \ appium \ build \ u nlock_apk \ unlock_apk-debug.apk“信息:启动应用信息:[debug]试图杀死所有'uiautomator'进程信息:[debug]使用'uiautomator'信息获取所有进程:[debug]执行cmd:”C:\ Program文件(x86)\ Android \ android-sdk \ platform-tools \ adb.exe“-s 964aa907d030 shell”ps'uiautomator'“info:[debug]找不到匹配的进程信息:[debug]运行bootstrap信息:[debug]产卵:C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe -s 964aa907d030 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.android .settings -e disableAndroidWatchers false info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:numtests = 1 info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:stream = info:[debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap . Bootstrap:info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:id = UiAutomatorTestRunner info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:test = testRunSe rver info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:class = io.appium.android.bootstrap.Bootstrap info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:current = 1 info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE:1 info :[debug] [BOOTSTRAP] [debug]在端口4724上打开套接字信息:[debug] [BOOTSTRAP] [debug] Appium Socket Server准备信息:[debug]唤醒设备,如果它不活动信息:[debug]推送命令到appium工作队列:[“wake”,{}] info:[debug] [BOOTSTRAP] [debug]加载json ... info:[debug] [BOOTSTRAP] [debug]已注册的崩溃观察者 . info:[debug] [BOOTSTRAP] [debug]客户端连接信息:[debug] [BOOTSTRAP] [debug]从客户端获取数据:{“cmd”:“action”,“action”:“wake”,“params”: {}} info:[debug] [BOOTSTRAP] [debug]获得类型ACTION信息的命令:[debug] [BOOTSTRAP] [debug]得到命令动作:唤醒信息:[debug] [BOOTSTRAP] [debug]返回结果:{ “status”:0,“value”:true} info:[debug]执行cmd:“C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe”-s 964aa907d030 shell“dumpsys窗口“信息:[调试]屏幕已解锁,继续 . info:[debug]将命令推送到appium工作队列:[“getDataDir”,{}] info:[debug] [BOOTSTRAP] [debug]从客户端获取数据:{“cmd”:“action”,“action”:“ getDataDir“,”params“:{}} info:[debug] [BOOTSTRAP] [debug]得到类型ACTION信息的命令:[debug] [BOOTSTRAP] [debug]得到命令动作:getDataDir info:[debug] [BOOTSTRAP] [debug]返回结果:{“status”:0,“value”:“/ data / local / tmp”} info:[debug] dataDir设置为:/ data / local / tmp info:[debug]将命令推送到appium work queue:[“compressedLayoutHierarchy”,{“compressLayout”:false}] info:[debug] [BOOTSTRAP] [debug]从客户端获取数据:{“cmd”:“action”,“action”:“compressedLayoutHierarchy”,“ params“:{”compressLayout“:false}} info:[debug] [BOOTSTRAP] [debug]得到类型ACTION信息的命令:[debug] [BOOTSTRAP] [debug]得到命令动作:compressedLayoutHierarchy info:[debug] [BOOTSTRAP ] [debug]返回结果:{“status”:0,“value”:false} info:[debug]获取设备API级别信息:[debug]执行cmd:“C:\ Program Files(x8) 6)\ Android \ android-sdk \ platform-tools \ adb.exe“-s 964aa907d030 shell”getprop ro.build.version.sdk“info:[debug]设备处于API Level 23信息:[debug]执行cmd: “C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe”-s 964aa907d030 shell“am start -S -a android.intent.action.MAIN -c android.intent.category . Launcher -f 0x10200000 -ncom.android.settings / com.android.settings.MainSettings“info:[debug]等待pkg”com.android.settings“和活动”com.android.settings.MainSettings“成为焦点信息:[debug]聚焦包和活动信息:[debug]执行cmd:“C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe”-s 964aa907d030 shell“dumpsys window windows”info:[debug] execution cmd:“C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe”-s 964aa907d030 shell“getprop ro.build.version.release”info:[debug]设备处于发布版本6.0.1 info:[debug]设备已启动!准备命令信息:[debug]将命令超时设置为默认值60秒信息:[debug] Appium会话以sessionId 44f52f68-60a6-4469-9575-0c1bfe2d999d信息开始:< - POST / wd / hub / session 303 6622.421 ms - 74 info: - > GET / wd / hub / session / 44f52f68-60a6-4469-9575-0c1bfe2d999d {} info:[debug]成功响应客户端:{ “状态”:0 “值”:{ “平台”: “LINUX”, “browserName”:“A ndroid “ ”platformVersion“: ”6.0.1“, ”webStorageEnabled“:假的, ”takesScreenshot“:真实的, ”javascriptEnabled“:真实的, ”databaseEnabled“:假的, ”networkConnectionEnabled“:真实的, ”locationContextEnabled“:假”警告 “:{},” 期望的 “:{” appPackage “:” com.android.settings”, “appActivity”: “com.android.settings.MainSettings”, “platformVersion”: “6.0.1”, “automationName” :“UiAutomator 2”,“platformName”:“Android”,“deviceName”:“Redmi 3S”},“appPackage”:“com.android.settings”,“appActivity”:“com.android.settings.MainSettings”, “automationName”:“UiAutomator 2”,“platformName”:“Android”,“deviceName”:“964aa907d030”},“sessionId”:“44f52f68-60a6-4469-9575-0c1bfe2d999d”}信息:< - GET / wd / hub / session / 44f52f68-60a6-4469-9575-0c1bfe2d999d 200 1.705 ms - 695 {“status”:0,“value”:{“platform”:“LINUX”,“browserName”:“Android”,“platformVersion” : “6.0.1”, “webStorageEnabled”:假的, “takesScreenshot”:真实的, “javascriptEnabled”:真实的, “databaseEnabled”:假的, “networkConnectionEnabled”:真实的, “locationContextEnabled”:假的, “警告”:{}, “期望的” :{“appPackage”:“com.android.settings”,“appActivity”:“com.android.settings.MainSettings”,“platformVersion”:“6.0.1”,“automationName”:“UiAutomator 2”,“platformName” :“Android”,“deviceName”:“Redmi 3S”},“appPackage”:“com.android.settings”,“appActivity”:“com.android.settings.MainSettings”,“automationName”:“UiAutomator 2”, “platformName”:“Android”,“deviceName”:“964aa907d030”},“sessionId”:“44f52f68-60a6-4469-9575-0c1bfe2d999d”}信息: - > POST / wd / hub / session / 44f52f68-60a6- 4469-9575-0c1bfe2d999d / element {“using”:“ - android uiautomator”,“value”:“UiSelector() . text(\”Display \“)”} info:[debug]等待条件信息最多0ms: [debug]将命令推送到appium工作队列:[“find”,{“strategy”:“ - android uiautomator”,“selector”:“UiSelector() . text(\”Display \“)”,“context”:“ “,”“multiple”:false}] info:[debug] [BOOTSTRAP] [debug]从客户端获取数据:{“cmd”:“action”,“action”:“find”,“params”:{“strategy” :“ - android uiautomator”,“selector”:“UiSelector() . text(\”Display \“)”,“context”:“”,“multiple”:false}} inf o:[debug] [BOOTSTRAP] [debug]获得类型ACTION信息的命令:[debug] [BOOTSTRAP] [debug]得到命令动作:查找信息:[debug] [BOOTSTRAP] [debug]查找UiSelector() . text( “显示”)使用ANDROID_UIAUTOMATOR和contextId:multiple:false info:[debug] [BOOTSTRAP] [debug]解析选择器:UiSelector() . text(“Display”)info:[debug] [BOOTSTRAP] [debug] UiSelector强制类型:类java.lang.String arg:“显示”信息:[debug] [BOOTSTRAP] [debug]使用:UiSelector [TEXT =显示]信息:[debug] [BOOTSTRAP] [debug]返回结果:{“status” :0,“value”:{“ELEMENT”:“1”}} info:[debug]成功响应客户端:{“status”:0,“value”:{“ELEMENT”:“1”},“ sessionId“:”44f52f68-60a6-4469-9575-0c1bfe2d999d“} info:< - POST / wd / hub / session / 44f52f68-60a6-4469-9575-0c1bfe2d999d / element 200 82.340 ms - 87 {”status“:0 ,“value”:{“ELEMENT”:“1”},“sessionId”:“44f52f68-60a6-4469-9575-0c1bfe2d999d”}信息: - > POST / wd / hub / session / 44f52f68-60a6-4469- 9575-0c1bfe2d999d / element / 1 /点击{“id”:“1”} inf o:[debug]将命令推送到appium工作队列:[“element:click”,{“elementId”:“1”}] info:[debug] [BOOTSTRAP] [debug]从客户端获取数据:{“cmd”: “action”,“action”:“element:click”,“params”:{“elementId”:“1”}} info:[debug] [BOOTSTRAP] [debug]获得类型ACTION信息的命令:[debug] [ BOOTSTRAP] [debug]获得命令操作:单击信息:[debug]成功响应客户端:{“status”:0,“value”:true,“sessionId”:“44f52f68-60a6-4469-9575-0c1bfe2d999d”}信息:< - POST/ wd / hub / session / 44f52f68-60a6-4469-9575-0c1bfe2d999d / element / 1 / click 200 40.169 ms - 76 {“status”:0,“value”:true,“sessionId”:“44f52f68-60a6-4469 -9575-0c1bfe2d999d“} info:[debug] [BOOTSTRAP] [debug]返回结果:{”status“:0,”value“:true} info:[debug] 60秒内没有得到新命令,关闭down ... info:关闭appium会话信息:[debug]按HOME按钮信息:[debug]执行cmd:“C:\ Program Files(x86)\ Android \ android-sdk \ platform-tools \ adb.exe “-s 964aa907d030 shell”输入keyevent 3“info:[debug]停止logcat捕获信息:[debug] Logcat终止,代码为null,信号SIGTERM信息:[debug] [BOOTSTRAP] [debug]从客户端获取数据:{”cmd “:”shutdown“} info:[debug] [BOOTSTRAP] [debug]得到类型SHUTDOWN的命令信息:[debug] [BOOTSTRAP] [debug]返回结果:{”status“:0,”value“:”好的,关闭“} info:[debug]发送shutdown命令,等待UiAutomator停止... info:[debug] [BOOTSTRAP] [debug]封闭客户端连接信息:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:numtests = 1 info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:stream = . info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:id = UiAutomatorTestRunner info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:test = testRunServer info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:class = io.appium.android.bootstrap.Bootstrap info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:current = 1 info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE:0 info:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS:stream = info:[debug] [UIAUTOMATOR STDOUT]测试结果对于WatcherResultPrinter = . info:[debug] [UIAUTOMATOR STDOUT]时间:67.859信息:[debug] [UIAUTOMATOR STDOUT] OK(1测试)信息:[debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE:-1 info:[debug] UiAutomator正常关闭信息: [debug]清理android对象信息:[debug]清理appium会话信息:[debug]我们关闭因为没有新的命令进来

2 回答

  • 0

    你试试吧: driver.findElement(By.ByName("Display"));

    要么

    driver.findElement(By.xpath("//android.widget.TextView[@text='Display']"));
    
  • 0

    尝试

    . driver.findElementByName( “显示”)点击();

    要么

    . driver.findElementByxpath( “// * [@ id中= '机器人:ID / Headers '] [@文本= '显示']”)点击();

    它应该工作

相关问题