tuneup_js icon indicating copy to clipboard operation
tuneup_js copied to clipboard

Extending uiautomation-ext.js for UIACollectionView with long pages, invisible cells()

Open grgcombs opened this issue 11 years ago • 1 comments


    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 + "'");
      }
    });

grgcombs avatar Oct 01 '13 21:10 grgcombs

Send me a pull-request and I'll take a look. Also if you could provide a little writeup of these functions and what problems they're solving that would be great.

On Oct 1, 2013, at 2:43 PM, Greg Combs [email protected] wrote:

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 + "'");
  }
});

— Reply to this email directly or view it on GitHub.

alexvollmer avatar Oct 02 '13 17:10 alexvollmer