public void takeScreenshot(){ View decorView = viewFetcher.getRecentDecorView(viewFetcher.getWindowDecorViews()); screenshotTaker.takeScreenshot(decorView, null);}
viewFetcher.getWindowDecorViews()用來獲取當(dāng)前屏幕上堆積的所有DecorView(可以把一個(gè)DecorView當(dāng)做是一個(gè)畫布,當(dāng)你打開好多Activity時(shí)赫段,相當(dāng)于有好幾個(gè)畫布疊在了一起婴程,每個(gè)畫布上畫著一個(gè)activity的畫面)拿诸。而viewFetcher.getRecentDecorView()就相當(dāng)于取最上一層的畫布。進(jìn)入ViewFetcher.java文件觀察viewFetcher.getRecentDecorView()的源碼,我發(fā)現(xiàn)它在排除了一些無用的view后管钳,實(shí)際返回的是ViewFetcher.java中的另一個(gè)函數(shù)的結(jié)果:getRecentContainer(decorViews)榜晦。Be patient, 讓我們看看getRecentContainer(decorViews)里賣的是什么藥:
private final View getRecentContainer(View[] views) { View container = null; long drawingTime = 0; View view; for(int i = 0; i < views.length; i++){ view = views[i]; if (view != null && view.isShown() && view.hasWindowFocus() && view.getDrawingTime() > drawingTime) { container = view; drawingTime = view.getDrawingTime(); } } return container;}
getRecentContainer(decorViews)函數(shù)判斷哪個(gè)view在最上層的方法,是判斷:
- view不為空
view可見 - view已經(jīng)被focus上(就是該view是否處于被選中的廓啊、被激活的窗口中)
- 渲染時(shí)間排在最后
public void takeScreenshot(){ View decorView = null;while(decorView == null){ sleep(500); View[] decorViews = viewFetcher.getWindowDecorViews(); decorView = viewFetcher.getRecentDecorView(decorViews);}screenshotTaker.takeScreenshot(decorView, null);}
https://code.google.com/p/robotium/issues/detail?id=434&colspec=ID%20Type%20Stars%20Status%20Priority%20Milestone%20Owner%20Summary
Solo.java中添加:
public void assertCurrentActivity(String message, String name, int timeout, boolean takeScreenshot){ if(takeScreenshot){ asserter.assertCurrentActivity(message, name, timeout, this); } else assertCurrentActivity(message, name, timeout);}Asserter.java中添加:
public void assertCurrentActivity(String message, String name, int timeout, Solo solo){ if(!waiter.waitForActivity(name, timeout)){ solo.takeScreenshot("test_" + message + "Failure"); Assert.assertTrue(message, false); } else solo.takeScreenshot("test" + message + "_Success"); }
****我曾經(jīng)在《結(jié)合HierarchyViewer和APK文件反編譯獲得APP元素id值》一文中非常SB地寫了出了如何通過HierarchyViewer里的id名獲得元素的id整型值』端眩現(xiàn)在,我知道了新的方法崖瞭,會(huì)結(jié)合HierarchyViewer來進(jìn)行自動(dòng)化測(cè)試的人民有福了:****Solo.java中添加:
public View getViewByHierarchyViewerId(String id){ Context currContext = instrumentation.getTargetContext(); String packageName = currContext.getPackageName(); int viewId = currContext.getResources().getIdentifier(id, "id", packageName); return getView(viewId);