appium 3-31603调试分析方法
1.Appium Log
清晰记录了所有的请求和结果
@Test
public void testDebug() throws InterruptedException,IOException{
MobileElement tiaoguo = (MobileElement) driver.findElementByXPath("//*[@text='跳过']");
FileUtils.copyFile(new File("tiaoguo.png"),tiaoguo.getScreenshotAs(OutputType.FILE));
Thread.sleep(15000);
}
IDE执行结果:提示方法没有实现
appium日志
通过log中可以获取更多的信息,如
在screenshot方法执行时,有URL链接,加上IP和port,访问就可以获取到更多信息。
127.0.0.1:4723/wd/hub/session/92fe82c7-02ac-4fca-a8da-eca08027f985/element/1/screenshot
可以看到/appium/node_modules/appium-base-driver/lib/basedriver/driver.js:249:13中方法未实现
{"value":{"error":"unknown method","message":"Method has not yet been implemented","stacktrace":"NotYetImplementedError: Method has not yet been implemented\n at AndroidDriver.executeCommand$ (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/driver.js:249:13)\n at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)\n at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)\n at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)\n at invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)\n at enqueueResult (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:185:17)\n at Promise (<anonymous>)\n at F (/usr/local/lib/node_modules/appium/node_modules/core-js/library/modules/$.export.js:30:36)\n at AsyncIterator.enqueue (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:184:12)\n at AsyncIterator.prototype.(anonymous function) [as next] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)\n at Object.runtime.async (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:209:12)\n at AndroidDriver.executeCommand (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/build/lib/basedriver/driver.js:271:34)\n at AppiumDriver.executeCommand$ (/usr/local/lib/node_modules/appium/lib/appium.js:377:50)\n at tryCatch (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)\n at GeneratorFunctionPrototype.invoke [as _invoke] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)\n at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)\n at GeneratorFunctionPrototype.invoke (/usr/local/lib/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)\n at <anonymous>\n at process._tickCallback (internal/process/next_tick.js:169:7)"}}
cat -n /usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/driver.js
2.getPageSource
界面的完整dom结构.xml文件
封装locate方法,加入异常处理,将获取的节点信息在编辑器中打开,检索控件。
public WebElement locate(String locate){
try {
if (locate.matches("\\/\\/.*")) {
return driver.findElementByXPath(locate);
} else {
return driver.findElementById(locate);
}
}catch (org.openqa.selenium.NoSuchElementException EX){
System.out.println(locate);
System.out.println(driver.getPageSource());
return null;//没有返回值,代码会报错
}
}
@Test
public void TestDriver() throws InterruptedException{
Thread.sleep(5000);
//driver.findElementByXPath("//*[@text=\"允许\"]").click();
locate("//*[@text=\"允许\"]").click();
Thread.sleep(2000);
}
因为locate中返回的null,click找不到会因找不到控件报ava.lang.NullPointerException错误
3.脚本内调试
利用xpath获取所有匹配的元素 driver.findElementByXpath("//*")
public WebElement locate(String locate) throws InterruptedException{
try {
if (locate.matches("\\/\\/.*")) {
return driver.findElementByXPath(locate);
} else {
return driver.findElementById(locate);
}
}catch (org.openqa.selenium.NoSuchElementException EX){
List<AndroidElement> lists = driver.findElementsByXPath("//*");
for(AndroidElement e:lists){
System.out.print("tag: "+e.getTagName()+"\t");
System.out.print("text: "+e.getText()+"\t");
System.out.println("id: "+e.getAttribute("resourceId"));
//System.out.println(e.getAttribute("contentDesc"));该属性有误,content-desc也不可用
Thread.sleep(500);
}
return null;
}
}
FAQ
1.tagName打印null
@Test
public void TestDriver() throws InterruptedException{
Thread.sleep(5000);
List<AndroidElement> lists66 = driver.findElementsByXPath("//*");
for(AndroidElement e66:lists66){
System.out.print("tag: "+e66.getTagName()+"\t");
System.out.print("text: "+e66.getText()+"\t");
System.out.println("id: "+e66.getAttribute("resourceId"));
Thread.sleep(500);
}
Thread.sleep(20000);
}