UIACollectionView单元vs visibleCells

我正在尝试在xcode 4.5中使用自动化编写一个testing脚本。

我有一个UICollectionView ,我想点击一些目前不可见的单元格。

根据文档 ,我应该期望cells返回集合视图中的所有单元格,而visibleCells只返回当前可见的单元格。

相反,我所看到的是单元格只返回当前可见的单元格,并调用visibleCells停止脚本'undefined' is not a function (evaluating 'collection.visibleCells()')

 var target = UIATarget.localTarget(); var collection = target.frontMostApp().mainWindow().collectionViews()[0]; UIALogger.logMessage("Looking in collection: " + collection); UIALogger.logMessage("Cells: " + collection.cells() + " length " + collection.cells().length); UIALogger.logMessage("Visible cells: " + collection.visibleCells()); 

上面的代码返回右边的UICollectionView ,第二个日志行打印:

 Cells: [object UIAElementArray] length 12 

尽pipe我在集合视图中有100个项目,第三个日志行崩溃脚本。

这是一个文档/ UIACollectionView错误?

任何想法如何告诉自动滚动,直到它看到名为“我的单元格”的单元格? 我试过使用someCell.scrollToVisible ,但我需要有单元格来做到这一点,我没有,因为我不能从单元格中获取它。

编辑:

正如乔纳森build议,我已经实现了一个滚动直到find函数。 这是一个具体的实现,所以你可能需要调整isCellWithName 。 我还希望添加一个中断,以防在while循环中找不到所需的单元格,如果有人有想法,请随时编辑它。

 function isCellWithName(cell, name) { return (cell.staticTexts()[0].name() == name); } function getCellWithName(array, name) { for (var i = 0; i < array.length; i++) { if (isCellWithName(array[i], name)) { return array[i]; } } return false; } function scrollToName(collection, name) { var found = getCellWithName(collection.cells(), name); while (found === false) { collection.dragInsideWithOptions({startOffset:{x:0.2, y:0.99}, endOffset:{x:0.2, y:0},duration:1.0}); found = getCellWithName(collection.cells(), name); } return found; } 

该文件显然是不正确的。 UIACollectionView上没有visibleCells()方法。 我想通过遍历所有集合视图元素属性并打印出他们的名字:

 var target = UIATarget.localTarget(); var window = target.frontMostApp().mainWindow(); var collectionView = window.collectionViews()[0]; for (var i in collectionView) { UIALogger.logMessage(i); } 

另一方面,表格视图元素会使用cells()方法列出所有单元cells() 。 我想知道,如果他们select不这样做,因为收集意见更复杂的性质。 实际获取所有集合视图单元格,构build其表示forms和框架,并返回元素(如果有很多元素)可能会非常昂贵。 这就是UI Automation为所有单元格提供表格视图时所要做的。 他们都必须被实例化和计算才能得到元素表示。

但是,要回答你更大的问题,如何滚动到特定的单元格。 你能一直用滑动手势将它滚动到视图中吗? 这不是最方便的方法,而且我们被滚动到不可见的表格视图元素的能力“宠坏了”。 但从用户行为testing的angular度来看,滑动一定量就是用户不得不做的事情。 testing的结构是否可以反映这一点,是否可以满足您的需求?

我无法得到@marmor dragInsideWithOptions()位以通用的方式工作。 相反,我使用collectionView的value()函数来获取当前页面与最后一页的索引,如“第3页,共11页”所示。 然后,我使用collectionView的scrollUp()和scrollDown()方法遍历页面,直到find所需的内容。 我为TuneUp的uiautomation-ext.js写了一个扩展,这个扩展看起来有效,还有更多:

 function animationDelay() { UIATarget.localTarget().delay(.2); } extend(UIACollectionView.prototype, { /** * Apple's bug in UIACollectionView.cells() -- only returns *visible* cells */ pageCount: function() { var pageStatus = this.value(); var words = pageStatus.split(" "); var lastPage = words[3]; return lastPage; }, currentPage: function() { var pageStatus = this.value(); var words = pageStatus.split(" "); var currentPage = words[1]; //var lastPage = words[3]; return currentPage; }, scrollToTop: function() { var current = this.currentPage(); while (current != 1) { this.scrollUp(); animationDelay(); current = this.currentPage(); } }, scrollToBottom: function() { var current = this.currentPage(); var lastPage = this.pageCount(); while (current != lastPage) { this.scrollDown(); animationDelay(); current = this.currentPage(); } }, cellCount: function() { this.scrollToTop(); var current = this.currentPage(); var lastPage = this.pageCount(); var cellCount = this.cells().length; while (current != lastPage) { this.scrollDown(); animationDelay(); current = this.currentPage(); cellCount += this.cells().length; } return cellCount; }, currentPageCellNamed: function(name) { var array = this.cells(); for (var i = 0; i < array.length; i++) { var cell = array[i]; if (cell.name() == name) { return cell; } } return false; }, cellNamed: function(name) { // for performance, look on the current page first var foundCell = this.currentPageCellNamed(name); if (foundCell != false) { return foundCell; } if (this.currentPage() != 1) { // scroll up and check out the first page before we iterate this.scrollToTop(); foundCell = this.currentPageCellNamed(name); if (foundCell != false) { return foundCell; } } var current = this.currentPage(); var lastPage = this.pageCount(); while (current != lastPage) { this.scrollDown(); animationDelay(); current = this.currentPage(); foundCell = this.currentPageCellNamed(name); if (foundCell != false) { return foundCell; } } return false; }, /** * Asserts that this collection view has a cell with the name (accessibility identifier) * matching the given +name+ argument. */ assertCellNamed: function(name) { assertNotNull(this.cellNamed(name), "No collection cell found named '" + name + "'"); } });